我想重新打开这里和这里提出的关于在使用selenium的Nightwatch.js中测试文件上传的问题.
两个链接都有建议的解决方案,即将文件输入元素的值设置为url.在我的用例中,我一直无法使用它.甚至在夜间监视之外手动设置值标签的输入,其中type="file",不会更改网址.我在开发工具中尝试过Chrome,Firefox和IE10.
我看过的另一种解决方案是尝试模拟整个文件上传过程的击键.这将遵循单击文件上载按钮,键入路径和键入enter的路径.这将通过.click和.key方法完成.但是,您将失去实际文件上载窗口的焦点,这会延迟按键直到该窗口关闭.其他开发人员似乎能够使用java中的.findElement和.sendKeys方法直接在selenium中修复此解决方案,但我无法弄清楚如何在javascript和nightwatch本身中执行此操作.
有任何想法吗?
// My test
module.exports = {
"Standard File Upload" : function (browser) {
browser
.url("http://localhost:3000")
.waitForElementVisible('body', 1000)
.waitForElementVisible('input[type="file"]', 1000)
.setValue('input[type="file"]','http://localhost:3000/testfile.txt')
.click('#submit')
.pause(1000)
.assert.containsText('h3', 'File Uploaded Successfully')
.end();
}
};
// http://localhost:3000/testfile.txt was tested manually in the file upload window and worked successfully
Run Code Online (Sandbox Code Playgroud)
<!-- My input tag -->
<input id="fileUpload" type="file" name="textfile"/>
Run Code Online (Sandbox Code Playgroud)