首页 > 解决方案 > 使用量角器上传文件不起作用

问题描述

我正在尝试使用量角器在 e2e 测试中将文件上传到 Web 表单。文件未上传,输入字段为空。

这是代码:

it('should uploaded', ()=>{
        const fileToUpload = '../src/files/somefile.csv';
        const path = require('path');
        const remote = require('selenium-webdriver/remote');
        browser.setFileDetector(new remote.FileDetector());
        const absolutePath =  path.resolve(__dirname, fileToUpload);
        const fileElem = await element.all(by.css('input[type="file"]')).get(0);
        fileElem.sendKeys(absolutePath);
        // except input file to be not empty
        });

路径正确且文件存在于该路径中。没有错误发生。我只是在浏览器上看到输入字段为空。怎么了?为什么没有上传?

标签: angularprotractorangular-e2e

解决方案


最后,我找到了解决方案。问题是输入字段需要一些时间才能出现在屏幕上(有一个动画来完成)。所以,为了让它正常工作,我添加了这个:

const EC = protractor.ExpectedConditions;
browser.wait(this.EC.presenceOf(fileElem.get(0)), 5000);


推荐阅读