Overview

Nightwatch provides test environments to maintain a better structure of configuration for large-scale projects.

You can define multiple sections (environments) under the test_settings dictionary so you could overwrite specific values per environment.

A "default" environment is required. All the other environments are inheriting from default and can overwrite settings as needed.

An environment inherits all the base settings and all the settings defined under the "default" environment.

Example

In the example below, there are two environments:

  • default (always required)
  • integration
nightwatch.json
{
  "src_folders": ["./tests"],
  
"test_settings" : { "default" : { "launch_url" : "http://localhost", "globals" : { "myGlobalVar" : "some value", "otherGlobal" : "some other value" } }, "integration" : { "launch_url" : "http://staging.host", "globals" : { "myGlobalVar" : "other value" } } } }

Usage

Test environments can be referenced by its key in the command-line test runner, as the --env argument:

nightwatch --env integration

This can be useful if you need to have different settings for your local machine and the Continuous Integration server.

Built-in environments

The configuration file which is auto-generated by Nightwatch (nightwatch.conf.js) contains several pre-defined test environments for running tests against several browsers (Firefox, Chrome, Edge, Safari), and also for running tests using Selenium Server or popular cloud testing providers Browserstack and Saucelabs.

Here’s an extract from the nightwatch.conf.js config file available as part of the nightwatch-examples project on Github:

github.com/nightwatchjs/nightwatch-examples/.../nightwatch.conf.js
module.exports = {
  src_folders: ['tests'],

  test_settings: {
    default: {
      webdriver: {
        start_process: true,
        server_path: ''
      }
    },

    safari: {
      desiredCapabilities : {
        browserName : 'safari',
        alwaysMatch: {
          acceptInsecureCerts: false
        }
      },
      webdriver: {
        port: 4445
      }
    },
    
    firefox: {
      desiredCapabilities : {
        browserName : 'firefox'
      },
      
      webdriver: {
        start_process: true,
        port: 4444
      }
    }
  }
}