Nightwatch.js set testing environment via config file

Simon picture Simon · Oct 21, 2015 · Viewed 10.4k times · Source

Noob Node Warning: How do you programmatically set what config object to use when running a test?

Have been looking pretty hard for the definitive answer.

Setup:

/e2e-tests
    |-globals.js
    |-product.page.notify.stock.js
|-nightwatch.json 
|-nightwatch 

I have seen this method here by MateuszJeziorski but omits the means to get the process arguments. The examples provided with nightwatch don't answer this question either. I think the end result of the command would look something like this:

nightwatch -somekindofparametertosetenvironment -t e2e-tests/product.page.notify.stock

Answer

curtwphillips picture curtwphillips · Dec 13, 2015

It sounds like you may be able to get what you need with multiple environments in the nightwatch.json file.

You can set up your test environments with something like this in nightwatch.json:

"test_settings" : {
        "default" : {
            "launch_url" : "some_url",
            "selenium_port"  : 4444,
            "selenium_host"  : "localhost",
            "silent": true,
            "screenshots" : {
            "globals" : {
                "site_url" : "some_site"
            },      
            "desiredCapabilities": {
               "browserName": "chrome",
               "javascriptEnabled": true,
               "acceptSslCerts": true
           }
        },
        "other_environment" : {
            "globals" : {
                "site_url" : "some_other_site"
            }
        },
        "one_more_environment" : {
            "globals" : {
                "site_url" : "one_other_site",
                "other_var" : "this env needs a different variable"
            }
        }
    }

Nightwatch will let you pass in an environment with --env. Each environment can have unique global variables.

The 'default' properties are used in every environment unless they are specifically overridden.

Run a specific environment with a command like nightwatch --env "other_environment". The environment will start up with the globals listed in nightwatch.json.