File Upload Testing in Nightwatch.js

前端 未结 3 1137
旧巷少年郎
旧巷少年郎 2020-12-05 19:22

I\'d like to reopen the question posed here and here about testing file uploading within Nightwatch.js which uses selenium.

Both links have the recommended solutio

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 19:51

    There were two seperate issues with my setValue() method implementation.

    1. Using the --verbose tag in the nightwatch command led me to an issue where it was not actually finding the input tag during the setValue(), however it was during the waitForElementVisible(). Changing input[type="file"] to input#fileUpload solved this issue.

    2. Secondly, the following ways of describing the path were not working...

      • 'textfile.txt'
      • 'http://localhost:3000/testfile.txt' (Will work if typed manually into file upload window)


      What did work was using require('path').resolve(__dirname + '/testfile.txt')


    Take a look here to see the discussion that led to the fix. Thanks goes out to richard-flosi.

    The working code:

    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();
      }
    };
    

提交回复
热议问题