Re: [nightwatchjs] Allowing custom command line variables which can be used within conditionals in tests & setting data

2,821 views
Skip to first unread message

Andrei Rusu

unread,
Sep 23, 2014, 10:54:37 AM9/23/14
to nightw...@googlegroups.com
You can do that by creating multiple environments:
http://nightwatchjs.org/guide#config-test-settings.

On Tue, Sep 23, 2014 at 4:43 PM, Aaron Briel <aaron...@gmail.com> wrote:
> Hello! I am testing sites that have the same code base with different
> branding and only slightly different logic. I thus have a need to pass in
> custom command line variables such as site, which will then determine global
> data to be used such as URL, and also which can be accessed within test
> cases for site-specific conditionals to allow for the same test cases to be
> used across the different sites. I've determined that I can add a command
> line parameter to runner.js (as follows below), but I'm unsure of how to
> implement beyond this. Any ideas would be great!!
>
> cli.command('site')
> .description('Site to test: site1 staging (site1_stg), site1 development
> (site1_dev), site2 staging (site2_stg), site2 development (site2_dev)')
> .alias('s')
>
> --
> You received this message because you are subscribed to the Google Groups
> "NightwatchJs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nightwatchjs...@googlegroups.com.
> To post to this group, send email to nightw...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nightwatchjs/66d24ae0-11d8-4fcc-8eca-065667751b5c%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Aaron Briel

unread,
Sep 23, 2014, 12:06:05 PM9/23/14
to nightw...@googlegroups.com
Right, but I also want to be able to pass in specific browser and do test runs on say site1 staging on chrome, site1 development on firefox, etc.  It seems I would then have to have environments like site1_staging_firefox, site1_staging_chrome, site1_development_firefox, etc - repeating browser settings for each.  This seems less than optimal with respect to code-reuse unless I'm missing something?

Andrei Rusu

unread,
Sep 23, 2014, 3:32:07 PM9/23/14
to nightw...@googlegroups.com
Hmm, ok. You can also have a nightwatch.conf.js file which can be used
instead of nightwatch.json and you can do. I'll think about the custom
cli options. Feel free to submit an issue on Github.
> https://groups.google.com/d/msgid/nightwatchjs/46f184ee-66e8-43a9-9805-079b1ebd2769%40googlegroups.com.

Aaron Briel

unread,
Sep 23, 2014, 4:51:06 PM9/23/14
to nightw...@googlegroups.com
I'll look into nightwatch.conf.js.  I submitted issue #279 for this.

Aaron Briel

unread,
Oct 2, 2014, 11:35:29 AM10/2/14
to nightw...@googlegroups.com
I as able to achieve code reuse for desired capabilities by using nightwatch.conf.js to allow for moduleexports.  Thanks!!!!

Aaron Briel

unread,
Oct 3, 2014, 2:02:23 PM10/3/14
to nightw...@googlegroups.com
For others information, what I did to achieve this is as follows:
1. Create nightwatch.conf.js under bin and copy all text from nightwatch.json into this file and start it with "module.exports = "
2. Update runner.js path to configuration file to point to nightwatch.conf.js instead of nightwatch.json
3. In nightwatch.conf.js, one can then create variables prior to module.exports such as follows:
var firefox_dcap = {
    "browserName": "firefox",
    "javascriptEnabled": true,
    "acceptSslCerts": true
};

4. Within module.exports, one can then create a custom environment that uses the desired capabilities variables created prior. For example:
    "site1_stg_firefox" : {
        "desiredCapabilities": firefox_dcap,
        "globals" : { "environment" : "site1_stg_firefox" }
    },

The global environment variable can then be used in tests for site-specific conditionals, or to extract data stored in a globals file. 

Lacy Morrow

unread,
Oct 3, 2014, 8:10:03 PM10/3/14
to nightw...@googlegroups.com
Excellent! thank you!

Andrei Rusu

unread,
Oct 4, 2014, 2:13:00 PM10/4/14
to nightw...@googlegroups.com
You can skip step 2. nightwatch.conf.js will be loaded automatically and takes precedence over nightwatch.json.
--
You received this message because you are subscribed to the Google Groups "NightwatchJs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nightwatchjs...@googlegroups.com.
To post to this group, send email to nightw...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nightwatchjs/1f8a8ade-735d-4549-beae-01c65bfffaca%40googlegroups.com.

James Eisenhauer_work

unread,
Apr 25, 2015, 1:29:48 PM4/25/15
to nightw...@googlegroups.com
Aaron could you post your entire nightwatch.conf.js file.  I am trying to follow this but think I must be missing something 

thanks

Jim

James Eisenhauer_work

unread,
Apr 25, 2015, 2:02:39 PM4/25/15
to nightw...@googlegroups.com
I figured it out.  Trying to set chromeOptions must send a json object not the string.


var ext_var = "args": ["--load-extension=.full/path/to/chrome_ext"]}';
settings.test_settings.default.desiredCapabilities.chromeOptions = JSON.parse(ext_var);

GrayedFox

unread,
Jul 20, 2015, 9:19:14 AM7/20/15
to nightw...@googlegroups.com
Hey there, just wondering how you actually called the globals variable "environment"? My nightwatch.conf.js file has a part like this:

      "globals": {
        "theThing": "HOLY POO"
      }

And from a given test file, I have tried: 
browser.globals.theThing
and
browser.theThing
and even
browser.globals.globals.theThing

But I always get undefined! (or cannot read property of undefined, for the last one, as expected).

The globals part is used in the default settings (so firefox) which is what I'm testing this with (I'm not using chrome and defining it in firefox!).

Kaj Magnus Lindberg

unread,
Oct 1, 2015, 11:06:46 PM10/1/15
to NightwatchJs
Hi GrayedFox,

I don't know, but this works instead: Create a globals.js file and reference it in nightwatch.conf.js (or .json) via globals_path.
In that globals.js file, you can read command line args, I do it like so:  (perhaps this (e.g. argv[2]) works only because I use gulp-nightwatch)

var argsJsonString = process.argv[2];
var args = JSON.parse(argsJsonString);
var secure = args.secure;
var host = args.host || (secure ? 'localhost:9443' : 'localhost:9000');
var self = module.exports = {
  host: host,
  ...
}

And then in the e2e test files, you can access the variables via browser.globals.host for example.

So, I read command line args in  globals.js    instead of nightwatch.conf.js.  (because I too don't know how to pass settings from conf.js on to the actual tests).

Best regards, KajMagnus

GrayedFox

unread,
Oct 2, 2015, 4:58:06 AM10/2/15
to NightwatchJs
Hey there Kaj,

Thanks for the info. I ended finding another solution (haven't done any developing in ages - stuck in QA mode - so I forget!) but will post here once I find it.
Reply all
Reply to author
Forward
0 new messages