如何在angularjs e2e量角器测试中上传文件 [英] How to upload file in angularjs e2e protractor testing
本文介绍了如何在angularjs e2e量角器测试中上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 angularjs e2e 测试来测试文件上传.你如何在 e2e 测试中做到这一点?我通过 grunt karma 运行我的测试脚本.
I want to test file uploading using an angularjs e2e test. How do you do this in e2e tests? I run my test script through grunt karma.
推荐答案
我是这样做的:
var path = require('path');
it('should upload a file', function() {
var fileToUpload = '../some/path/foo.txt',
absolutePath = path.resolve(__dirname, fileToUpload);
element(by.css('input[type="file"]')).sendKeys(absolutePath);
element(by.id('uploadButton')).click();
});
- 使用
path
模块解析您要上传的文件的完整路径. - 设置 input type="file" 元素的路径.
- 点击上传按钮.
- Use the
path
module to resolve the full path of the file that you want to upload. - Set the path to the input type="file" element.
- Click on the upload button.
这不适用于 Firefox.量角器会抱怨,因为该元素不可见.要在 Firefox 中上传,您需要使输入可见.这就是我所做的:
This will not work on firefox. Protractor will complain because the element is not visible. To upload in firefox you need to make the input visible. This is what I do:
browser.executeAsyncScript(function(callback) {
// You can use any other selector
document.querySelectorAll('#input-file-element')[0]
.style.display = 'inline';
callback();
});
// Now you can upload.
$('input[type="file"]').sendKeys(absolutePath);
$('#uploadButton').click();
这篇关于如何在angularjs e2e量角器测试中上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文