使用NightwatchJS模拟文件上传(DropzoneJS)

时间:2017-08-29 13:00:22

标签: javascript file-upload dropzone.js nightwatch.js

我正在尝试使用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在测试失败时制作屏幕截图,屏幕截图显示我没有上传任何内容,这就是为什么最后一个断言失败。

2 个答案:

答案 0 :(得分:0)

将其更改为:

<input type="file" multiple="multiple" style="position: absolute; left: -9999px;">

输入将被视为可见,但它实际上在屏幕上不可见。

答案 1 :(得分:0)

使用Google Chrome驱动程序上传文件正在运行!只需使用:

 .setValue("input[type=file]",`/path/to/file`)   
相关问题