小编典典

Nightwatch.js中的文件上传测试

selenium

我想重新讨论提出的有关在使用selenium的Nightwatch.js中测试文件上传的问题。

推荐使用两个链接将文件输入元素的值设置为url的解决方案。在我的用例中,我无法使其正常工作。即使在夜视之外,在的输入中手动设置value标签type="file",也不会更改url。我已经在开发工具中的Chrome,Firefox和IE10上进行了尝试。

我看过的另一种解决方案是尝试模拟整个文件上传过程的击键。这将遵循以下路径:单击文件上传按钮,键入路径,然后键入Enter。这可以通过.click.key方法完成。但是,您将失去对实际文件上传窗口的关注,这会延迟击键,直到关闭该窗口。其他开发人员似乎能够使用Java中的.findElement.sendKeys方法直接在selenium中修复此解决方案,但我不知道如何在javascript和守夜本身中执行此操作。

有任何想法吗?

// 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"/>

阅读 459

收藏
2020-06-26

共1个答案

小编典典

我的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')

在这里
看一看以了解导致该修复程序的讨论。感谢Richardard-
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();
  }
};
2020-06-26