【问题标题】:Simulate a file upload (DropzoneJS) with NightwatchJS使用 NightwatchJS 模拟文件上传 (DropzoneJS)
【发布时间】:2017-08-29 13:00:22
【问题描述】:

我正在尝试使用 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 在测试失败时进行截图,截图显示我没有上传任何东西,这就是最后一个断言失败的原因。

【问题讨论】:

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


    【解决方案1】:

    改成:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-21
        • 2013-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-28
        相关资源
        最近更新 更多