tmacarthur
tmacarthur

Reputation: 141

How to make a globally accessible variable?

How can I make a globally accessible variable in nightwatch.js? I'm using a variable to store a customized url (dependent on which store is loaded in our online product), but I need it to be accessible across several javascript functions. It appears the value of it resets after each function ends, despite it being declared outside of the function at the head of the file.

Upvotes: 10

Views: 19499

Answers (6)

JRichardsz
JRichardsz

Reputation: 16544

An alternative of globals.json if you need read data with complex procedure, is just to create a function in the same test file.

In the following example, I needed simple values and data from csv.
So I created getData() function and I can invoke directly from inside:

let csvToJson = require('convert-csv-to-json');

function getData(){
  let users = csvToJson.getJsonFromCsv("/../users.csv");
  return {
    "users:": users,
    "wordToSearch":"JRichardsz"
  }
}

module.exports = {
    
    "login": function(browser) {

      //data is loading here
      var data = getData();

      browser
           .url('https://www.google.com')
           .waitForElementVisible('input[name="q"]', 4000)
           .setValue('input[name="q"]', data.wordToSearch)
           .keys(browser.Keys.ENTER)
           .waitForElementVisible('#result-stats', 4000)
           .end();
    }
};

Upvotes: 0

GrayedFox
GrayedFox

Reputation: 2563

It's been some time since you asked your question and support for what you requested might not have been (natively) available before. Now it is.

In the developer guide two methods are provided for creating global variables accessible from any given test, depending on your needs. See here for good reading.

Method 1: For truly global globals, that is, for all tests and all environments. Define an object, or pass a file, at the "globals_path" section of your nightwatch.json file, i.e.

"globals_path": "./lib/globals.js",

You will need to export the variables, however, so brushing up on Node is a good idea. Here is a basic globals.js file example:

var userNames = {
  basicAuth: 'chicken',
  clientEmail: '[email protected]',
  adminEmail: '[email protected]',
};

module.exports = {
  userNames: userNames
}

This object/file will be used for all of your tests, no matter the environment, unless you specify a different file/object as seen in method 2 below.

To access the variables from your test suite, use the mandatory browser/client variable passed to every function (test), i.e:

 'Account Log In': function accLogin(client) {
    var user = client.globals.userNames.clientEmail;

    client
      .url(yourUrl)
      .waitForElementVisible('yourUserNameField', 1000)
      .setValue('yourUserNameField', user)
      .end();
}

Method 2: For environment based globals, which change depending on the environment you specify. Define an object, or pass a file, at the "globals" section of your nightwatch.json file, nested under your required environment. I.e.

"test_settings" : {
    "default" : {
      "launch_url" : "http://localhost",
      "selenium_port"  : 4444,
      "selenium_host"  : "localhost",
      "globals": {
        "myGlobal" : "some_required_global"
      }
    }
}

Please note that at the time of writing, there seems to be a bug in nightwatch and thus passing a file using Method 2 does not work (at least in my environment). More info about said bug can be found here.

Upvotes: 18

QualiT
QualiT

Reputation: 1955

I'll probably get down-voted for this, but another option that I have been using successfully to store and retrieve objects and data is to do a file write as key value pairs to an existing file.

This allows me to, at the end of a test run, see any data that was randomly created. I create this file in my first test script using all of the data I will use to create the various accounts for the test. In this way, if I see a whole lot of failures, I can take a look at the file and see what data was used, then say, log in as that user and go to that location manually.

In custom commands I have a file that exports the following function:

saveToFile : function(path, filename, data) {
    this.yfs = fs;
    buffer = new Buffer(data);

    console.log("Note: About to update the configuration with test data" )

    fs.open(path, 'w', function(err, fd) {
        if (err) {
            throw 'error opening file: ' + err;
        }

        fs.write(fd, buffer, 0, buffer.length, null, function(err) {
            if (err) throw 'error writing file: ' + err;
             return fs.close(fd, function() {
                console.log('File write: ' +  path + ' has been updated.' );
            })
        });
    })
},

In this file, 'data' is key value pairs like "username" : "[email protected]". As a result I can use that data in later scripts, if so desired.

This being true, I'll be exploring GrayedFox's answer immediately.

Upvotes: 1

imiric
imiric

Reputation: 9030

To expand on Tricote's answer, Nightwatch has built-in support for this. See the documentation.

You can either specify it in the nightwatch.json file as "globals": {"myvar": "whatever"} or in a globals.js file that you reference within nightwatch.json with "globals": "path/to/globals.js". In the latter case, globals.js could have:

module.exports = {
  myvar: 'whatever'
};

In either case, you can access the variable within your tests as Tricote mentioned:

module.exports = {
  "test": function(browser) {
    console.log(browser.globals.myvar); // "whatever"
  }
};

Upvotes: 4

Tricote
Tricote

Reputation: 1508

Not sure it's the best way, but here is how I do it : you can define a variable in the browser.globals and access it in your different tests

For instance :

 module.exports = {
  before: function(browser) {
    console.log("Setting up...");

    // initialize global variable state
    browser.globals.state = {};
  },

  "first test": function(browser) {
    var settings = browser.globals,
        state = browser.globals.state;

    state.my_shared_var = "something";

    browser.
      // ...
      // use a shared variable
      .setValue('input#id', state.my_shared_var)
      // ...

      // ...
      // save something from the page in a variable 
      .getText("#result", function(result) {
        state.my_shared_result = result.value;
      })
      // ...
  },

  "second test": function(browser) {
    var settings = browser.globals,
        state = browser.globals.state;

    browser.
      // ...
      // use the variables
      .url("http://example.com/" + state.my_shared_result + "/show")
      .assert.containsText('body', state.my_shared_var)
      // ...
  }
}

Upvotes: 0

vodolaz095
vodolaz095

Reputation: 6986

generaly it is a bad practice, but you can assign it as field of window class.

window.someGlobalVar = 'http://example.org/'

and window object is accessible globally

Upvotes: -3

Related Questions