3

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')
nicholas
  • 2,581
  • 14
  • 66
  • 104
  • Some examples of what you have tried already and some more details about what you are trying to accomplish would be extremely helpful. – tehbeardedone Jan 03 '18 at 18:28
  • I'm beginner in javascript thus i don't have any examples yet. – nicholas Jan 04 '18 at 01:06
  • Ok, what kind of dynamic test data are you trying to create? Usernames, passwords, random numbers, credit card numbers, phone numbers? Give me some more details of what you are trying to do. Without that it's hard to give you a suggestion on how to make it work. – tehbeardedone Jan 04 '18 at 13:54
  • A dynamic test data for login page such as username and passwords. – nicholas Jan 05 '18 at 01:22

4 Answers4

5

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 :)

TheBay0r
  • 375
  • 3
  • 11
3

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

Ivana
  • 85
  • 6
0

TheBayOr answered the question concisely regarding the use of files. Just to add that if you don't literally mean a non 'code' file but simply using a different location to store the values then the most common approach is using globals.

You can place an array of values in either your nightwatch.json...

"test_settings" : {
    "default" : {
        "selenium_port"  : 4444,
        "selenium_host"  : "localhost",
        "silent": true,
        "globals" : {
            "VARIABLE_1" : "i'm a variable",
            "VARIABLE_2" : "i'm a variable too"
        },      
        "desiredCapabilities": {
           "browserName": "chrome",
           "javascriptEnabled": true,
           "acceptSslCerts": true
       }
    },
    "other_environment" : {
        "globals" : {
            "VARIABLE_1" : "i'm a different variable",
            "VARIABLE_2" : "i'm a different variable too"

You can use them in tests with something like....

.url(browser.globals.VARIABLE_1)

Notice in the above you can have sets of globals under different environments. This has the advantage of meaning you can have multiple sets and use the one you want by running nightwatch -e 'my desired environment'.

Similarly this can be achieved by putting your array of data in a globals file e.g. globals.js and referencing it in your 'globals.path'.

If you want to get really into it you can even store your variables in global.js then use the 'fs' library to write the values to a file, then have your tests read from there. I'd recommend a new question if thats what you intend.

Hopefully that adds something :)

Asoc
  • 51
  • 2
0

In my case I just created a function which read variables , data ,etc

more details here: https://stackoverflow.com/a/64616920/3957754

JRichardsz
  • 14,356
  • 6
  • 59
  • 94