在Nightwatch.js中进行文件上传测试

tim*_*maw 15 selenium nightwatch.js

我想重新打开这里这里提出的关于在使用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)

tim*_*maw 21

我的setValue()方法实现有两个独立的问题.

  1. 在nightwatch命令中使用--verbose标签让我遇到了一个问题,即它在实际上并没有找到输入标签 setValue(),而是在它期间waitForElementVisible().更改input[type="file"]input#fileUpload解决这个问题.

  2. 其次,以下描述路径的方式不起作用......

    • 'textfile.txt'
    • 'http://localhost:3000/testfile.txt' (如果手动输入文件上传窗口,则可以工作)


    工作正在使用什么 require('path').resolve(__dirname + '/testfile.txt')


看看这里导致修复的讨论.谢谢richard-flosi.

工作代码:

module.exports = {
  "Standard File Upload" : function (browser) {
    browser
      .url("http://localhost:3000")
      .waitForElementVisible('body', 1000)
      .waitForElementVisible('input#fileUpload', 1000)
      .pause(1000)
      .setValue('input#fileUpload', require('path').resolve(__dirname + '/testfile.txt')) // Works
//      .setValue('input#fileUpload', "testfile.txt") // Will not work
//      .setValue('input#fileUpload', "http://localhost:3000/testfile.txt") // Will not work
//      .setValue('input[type="file"]', require('path').resolve(__dirname + '/testfile.txt')) // Will not work
      .click('#submit')
      .pause(1000)
      .assert.containsText('h3', 'File Uploaded Successfully')
      .end();
  }
};
Run Code Online (Sandbox Code Playgroud)