Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Supply Test Data into Nightwatch

I tried to supply test data to nightwatch but i don't know how. How to supply any dynamic test data to Nightwatch testing?

I don't want to hardcoded the value into the code. I want to supply it from file.

EDIT:

.setValue('selector', 'DEBBIE A/P EKU')
like image 755
nicholas Avatar asked Dec 13 '25 09:12

nicholas


2 Answers

Since you mentioned it in one of your comments you want to read the values from a file, I recommend you doing it via pseudo-JSON (actually .js). Also a solution I applied in my company.

I have multiple json files which contain certain test data that I didn't want to have in the code. The basic structure of those looks like this:

module.exports = {
  WHATEVER_IDENTIFIER_I_WANT: 'Some shiny value'
}

My Page Object contains a line like this:

const STATIC = require('path/to/static/file')
…
.setValue('selector', STATIC.WHATEVER_IDENTIFIER_I_WANT)

And yea, it is not highly sophisticated but it fulfils the purpose.

If you don't want to use module.exports and .js you can still use some node methods to load and parse a JSON. E.g.

  • fs.readFileSync / fs.readFile (to load the JSON file)
    const file = fs.readFileSync('path/to/file')
  • JSON.parse() (to retrieve a JavaScript Object)
    const STATIC = JSON.parse(file)

Hope this is useful for you :)

like image 110
TheBay0r Avatar answered Dec 15 '25 17:12

TheBay0r


I've been through the same issue. At the moment my set up is like this: Raw data are in the excel sheet. I use node.js to convert excel sheet into json file. Then use json data in nightwatch.

Here is the code to read the json file in nightwatch:

module.exports = {
    tags: ['loginpage'],
    // if not regular size logout button is not visible

    'Login' : function (client) {
        var credentials;
        try{
            credentials = require('./path/to/inputJsonData.json');
        } catch(err) {
            console.log(err);
            console.log ('Couldn\'t load the inputJsonData file. Please ensure that ' +
                'you have the inputJsonData.json in subfolder ./path/to ' +
                'in the same folder as the tests');
            process.exit();
        }

Here is the code that use data from it:

    client
        .url(credentials.url)
        .waitForElementVisible('body', 1000)
        .assert.title('Sign In Home Page')
        .login(credentials.username,credentials.password)
        // some more steps here 
        .logout()
        .end();
    }
};

inputJsonData.json data

{
    "url": "http://path/to/my/input/credentials/file",
    "username": "yourUserName",
    "password": "yourPassword"
}

My problem/question: How to find the count of elements read into the json object from a file when the file has following format?:

[
 {
  ....
 },
 {
  ....
 },
 .....
 {
  ....
 }
]

My failed attempt to get the number of elements: JSON.parse(company).count where company is another json read file like credentials in above code.

Answer: use standard javascript array property length company.length

like image 33
Ivana Avatar answered Dec 15 '25 15:12

Ivana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!