我正在尝试使用 NightwatchJS 上传图像,但将值设置为输入不起作用。我在上传部分使用 DropzoneJS。
我已经尝试过以下解决方案: NIghtwatch.js File Upload with Dropzone.js
HTML
<input type="file" multiple="multiple" style="visibility: hidden;
position: absolute; top: 0px; left: 0px; height: 0px; width: 0px;">
JavaScript
module.exports = {
"Upload test": function(browser) {
browser
.url('localhost:8080')
.assert.elementPresent('form > div:nth-of-type(2) > div > div:nth-of-type(3) > div:nth-of-type(2) > div > input')
.execute('document.querySelectorAll("input[type=file]")[0].style.display = "block";')
.setValue('form > div:nth-of-type(2) > div > div:nth-of-type(3) > div:nth-of-type(2) > div > input', require('path').resolve(__dirname + '/img.jpg'))
.assert.valueContains('form > div:nth-of-type(2) > div > div:nth-of-type(3) > div:nth-of-type(2) > div > input', require('path').resolve(__dirname + '/img.jpg'))
.end();
}
}
Nightwatch 在测试失败时进行截图,截图显示我没有上传任何内容,这就是最后一个断言失败的原因。
最佳答案
将其更改为:
<input type="file" multiple="multiple" style="position: absolute; left: -9999px;">
输入将被视为可见,但实际上在屏幕上不可见。
关于javascript - 使用 NightwatchJS 模拟文件上传 (DropzoneJS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45939862/