Nightwatch: globals_path

Created on 5 Aug 2015  Â·  3Comments  Â·  Source: nightwatchjs/nightwatch

Hello,

I am trying to implement nightwatch with 3 different environments. One for each server (local, test, deployment).

I have variables that changes the values on each environment on the data folder with one file for each environment.

my nightwatch.json is something like:

{
"src_folders" : ["tests"],
"output_folder" : "./reports",
"custom_commands_path" : "./commands",
"custom_assertions_path" : "./assertions",
"page_objects_path" : "./testpages",
"globals_path" : "./data/local.js",
"selenium" : {
"start_process" : true,
"server_path" : "./node_modules/bin/selenium-server-standalone-2.45.0.jar",
"log_path" : "./reports/logs",
"host" : "127.0.0.1",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "./node_modules/bin/chromedriver",
"webdriver.ie.driver" : ""
}
},

"test_settings" : {
"default" : {
"globals_path" : "./data/local.js",
"launch_url" : "",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : "./reports/screenshots"
},
"desiredCapabilities": {
"browserName": "phantomjs",
"javascriptEnabled": true,
"acceptSslCerts": true,
"phantomjs.binary.path" : "./node_modules/.bin/phantomjs"
}
},

"test": {
  "globals_path" : "./data/test.js",
  "desiredCapabilities": {
    "browserName": "chrome",
    "javascriptEnabled": true,
    "acceptSslCerts": true,
    "phantomjs.binary.path" : "./node_modules/.bin/phantomjs"
  }
},

"production": {
  "screenshots": {
    "enabled": true,
    "path": "./reports/screenshots"
  },
  "globals_path" : "./data/production.js",
  "desiredCapabilities": {
    "browserName": "chrome",
    "javascriptEnabled": true,
    "acceptSslCerts": true,
    "phantomjs.binary.path" : "./node_modules/.bin/phantomjs"
  }
}

}
}

So, I would expect that using

nightwatch --env=production --test=tests/TESTNAME

Nightwatch would use the "globals_path" : "./data/production.js", from the production environment.

But NO! It is always using the "globals_path" : "./data/local.js" from the beginning.

If I remove the "globals_path" : "./data/local.js", from the beginning I get an error because night watch did not loaded neither data file.

Am I missing something, or there is a bug here?

According to night watch documentation:

globals_path
Optional since v0.4.8 string none
Location of an external globals module which will be loaded and made available to the test as a property globals on the main client instance.
Globals can also be defined/overwritten inside a test_settings environment.

So, why it is not working!? Any suggestion?

Thanks

Most helpful comment

@beatfactor Thanks for the info!

I was a bit confused that I could set globals in test_settings.default but globals_path was not working when defining it in test_settings.default. So I took the road you proposed and defined globals_path in the root of my nightwatch.json file:

{
  "custom_assertions_path": "",
  "custom_commands_path": "",
  "globals_path": "data/globals.js", // here it is!
  "output_folder": "reports",
  "page_objects_path": "",
  "selenium": {"start_process": true...},
  "src_folders": [...],
  "test_settings": {...}
}

And then I defined the environments (used in my test_settings) within the globals.js file:

data/globals.js

module.exports = {
  "default": {
    email: '[email protected]'
  }
};

Afterwards I was able to use the data within mit tests:

module.exports = {

  'Enter login credentials': function (browser) {
    var data = browser.globals;

    browser
      .url(browser.launch_url)
      .setValue("//*[@data-uie-name='enter-email']", data.email)
  },

  'Submit login credentials': function (browser) {
    browser
      .click("//*[@data-uie-name='do-sign-in']")
      .end();
  }
};

All 3 comments

The documentation is not very clear. You inly need one globals file. If you
want to have globals defined inside an environment use the "globals"
property or inside the globals file create a sub-object using the
environment as the key, like so:

{
"deployment": {
"something" : "some value"
},
"common" : "global value"
}

On Wednesday, August 5, 2015, sgleonardoopitz [email protected]
wrote:

Hello,

I am trying to implement nightwatch with 3 different environments. One for
each server (local, test, deployment).

I have variables that changes the values on each environment on the data
folder with one file for each environment.

my nightwatch.json is something like:

{
"src_folders" : ["tests"],
"output_folder" : "./reports",
"custom_commands_path" : "./commands",
"custom_assertions_path" : "./assertions",
"page_objects_path" : "./testpages",
"globals_path" : "./data/local.js",
"selenium" : {
"start_process" : true,
"server_path" : "./node_modules/bin/selenium-server-standalone-2.45.0.jar",
"log_path" : "./reports/logs",
"host" : "127.0.0.1",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "./node_modules/bin/chromedriver",
"webdriver.ie.driver" : ""
}
},

"test_settings" : {
"default" : {
"globals_path" : "./data/local.js",
"launch_url" : "",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : "./reports/screenshots"
},
"desiredCapabilities": {
"browserName": "phantomjs",
"javascriptEnabled": true,
"acceptSslCerts": true,
"phantomjs.binary.path" : "./node_modules/.bin/phantomjs"
}
},

"test": {
"globals_path" : "./data/test.js",
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true,
"phantomjs.binary.path" : "./node_modules/.bin/phantomjs"
}
},

"production": {
"screenshots": {
"enabled": true,
"path": "./reports/screenshots"
},
"globals_path" : "./data/production.js",
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true,
"phantomjs.binary.path" : "./node_modules/.bin/phantomjs"
}
}

}
}

So, I would expect that using

nightwatch --env=production --test=tests/TESTNAME

Nightwatch would use the "globals_path" : "./data/production.js", from the
production environment.

But NO! It is always using the "globals_path" : "./data/local.js" from the
beginning.

If I remove the "globals_path" : "./data/local.js", from the beginning I
get an error because night watch did not loaded neither data file.

Am I missing something, or there is a bug here?

According to night watch documentation:

globals_path
Optional since v0.4.8 string none

Location of an external globals module which will be loaded and made
available to the test as a property globals on the main client instance.
Globals can also be defined/overwritten inside a test_settings environment.

So, why it is not working!? Any suggestion?

Thanks

—
Reply to this email directly or view it on GitHub
https://github.com/nightwatchjs/nightwatch/issues/586.

@beatfactor Thanks for the info!

I was a bit confused that I could set globals in test_settings.default but globals_path was not working when defining it in test_settings.default. So I took the road you proposed and defined globals_path in the root of my nightwatch.json file:

{
  "custom_assertions_path": "",
  "custom_commands_path": "",
  "globals_path": "data/globals.js", // here it is!
  "output_folder": "reports",
  "page_objects_path": "",
  "selenium": {"start_process": true...},
  "src_folders": [...],
  "test_settings": {...}
}

And then I defined the environments (used in my test_settings) within the globals.js file:

data/globals.js

module.exports = {
  "default": {
    email: '[email protected]'
  }
};

Afterwards I was able to use the data within mit tests:

module.exports = {

  'Enter login credentials': function (browser) {
    var data = browser.globals;

    browser
      .url(browser.launch_url)
      .setValue("//*[@data-uie-name='enter-email']", data.email)
  },

  'Submit login credentials': function (browser) {
    browser
      .click("//*[@data-uie-name='do-sign-in']")
      .end();
  }
};

@bennyn Hi I am trying the same as you mentioned, but its not working. Do you specify anything in test_setting of nightwatch.conf.js file?

Was this page helpful?
0 / 5 - 0 ratings