【发布时间】:2014-09-18 18:18:33
【问题描述】:
我想重新提出 here 和 here 提出的关于在使用 selenium 的 Nightwatch.js 中测试文件上传的问题。
两个链接都有将文件输入元素的值设置为 url 的推荐解决方案。在我的用例中,我无法让它工作。即使在守夜人之外手动设置 type="file" 输入的值标签,也不会更改 url。我已经在开发工具中的 Chrome、Firefox 和 IE10 上尝试过这个。
我研究过的另一种解决方案是尝试模拟整个文件上传过程中的击键。这将遵循单击文件上传按钮、输入路径和输入回车的路径。这将通过.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
<!-- My input tag -->
<input id="fileUpload" type="file" name="textfile"/>
【问题讨论】: