简体   繁体   中英

Nightwatch.js not able to run browser in loop

I am writing a test in nightwatch to read a json data and run the browser with all sets of values in my json file.

For example, I have a json file which contains login passwords of multiple users. I want to launch a web browser for every user mentioned in json file and do a login and close it.

My json file content is as follows

{
"total_rows": 3,
"offset": 0,
"rows": [{
        "id": "1",
        "doc": {"username": "username1","password": "password1"}
    },{
        "id": "2",
        "doc": {"username": "username2","password": "password3"}
    },{
        "id": "3",
        "doc": {"username": "username3","password": "password3"}
    }]
}

And below is my nightwatch test

var fs = require('fs');

module.exports = {

'read CSV contents' : function (browser) {
    var contents = fs.readFileSync("./config/testdata.json");
    var jsonContent = JSON.parse(contents);

    Object.keys(jsonContent.rows).forEach(element => {
        console.log('##########')
        console.log(jsonContent.rows[element].doc.password);
       browser
       .url('//some url')
       .useXpath()
       .setValue('//input[@id=\'username\']',jsonContent.rows[element].doc.username)
       .setValue('//input[@id=\'password\']',jsonContent.rows[element].doc.password)
       .pause(1000)
       .end()
        });
    }
}

The above code is printing all three passwords in json file on the console and then launching a browser only one time. I want to launch browser and login for each set of password in json file. I went through a couple of forums and tutorials and they suggested to use callbacks or promises. Can anyone please suggest a correct approach to do it? It would be very helpful if you post a sample code for the same.

Thanks

First of all, after running end() within a single test run nightwatch will consider test finished immediately and you won't be able to restore browser session.

Since you want to open and close browser for each username and password - the solution could be to define multiple test run functions within your test file.

However this requires some hacking, but should work fine:

var fs = require('fs');

var contents = fs.readFileSync("./config/testdata.json");
var jsonContent = JSON.parse(contents);
var exports = {};

jsonContent.rows.forEach(element => {
  // Create a single test for each row passing "doc" object as the first parameter
  exports['Testing row #' + element.id] = runTest.bind(null, element.doc);
});

function runTest(doc, client) {
  client
    .url('//some url')
    // ... YOUR CODE GOES HERE
    .end();
}

module.exports = exports;

Here you basically read your JSON file, go over its records and create a separate test run function for each record, passing doc variable to function as a first parameter with use of bind() .

Thus, for nightwatch this test would look like:

module.exports = {
  'Testing row #1': function(client) {},
  'Testing row #2': function(client) {},
  'Testing row #3': function(client) {}
};

The only thing you are missing is to wrap your code in a browser.perform() . Although I'm not 100% if you can leave the .end() inside the perform . Give it a try and let me know.

var fs = require('fs');

module.exports = {
  'read CSV contents' : function (browser) {
    var contents = fs.readFileSync("./config/testdata.json");
    var jsonContent = JSON.parse(contents);
    browser.perform(function(){
      Object.keys(jsonContent.rows).forEach(element => {
        console.log('##########')
          console.log(jsonContent.rows[element].doc.password);
          browser
            .url('//some url')
            .useXpath()
            .setValue('//input[@id=\'username\']',jsonContent.rows[element].doc.username)
            .setValue('//input[@id=\'password\']',jsonContent.rows[element].doc.password)
            .pause(1000)
            .end();
      });
    })
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM