Nightwatch js Example

These Nightwatch js examples includes integration with a Gridlastic selenium grid.

The Gridlastic selenium grid hub credentials, YOUR_SUBDOMAIN, USERNAME, ACCESS_KEY and the video url used in this code example is displayed in your dashboard after launching your Gridlastic selenium grid (free).

NOTE: If you are using selenium version 3.5+ then you need to use nightwatch version 1.0.14+ in order to test with Firefox. You also need to change how you specify the Firefox version to test with, see examples below.

Install Nightwatch js


npm install -g nightwatch@1.0.14


Nightwatch js Test Script

test.js


// EXAMPLE NOTE: To preserve the session on test failure and to get session id in afterEach
// add "end_session_on_fail" : false to test_settings in the nightwatch.json.
// Always call .end() to close the session.
// In nightwatch.json you also need to add "request_timeout_options" set to a timeout of at least 10 minutes in order for new grid nodes to be launched if not present.

module.exports = {
afterEach : function(browser, done) {
console.log("VIDEO_URL: https://s3-us-west-1.amazonaws.com/......./play.html?"+browser.sessionId);
  browser.sessionLog(function(result) {
    console.log(result);
  }).end(function() {
    done();
  });
},
  'Test Google' : function (browser) {
    browser
      .url('http://www.google.com')
      .pause(5000);
  }
};


Nightwatch Chrome Example

nightwatch.json (any selenium version)

{
  "src_folders" : ["tests/"],
  "output_folder": "reports",

  "selenium" : {
    "start_process" : false,
    "host" : "YOUR_SUBDOMAIN.gridlastic.com",
    "port" : 80
  },

  "test_settings" : {
    "default" : {
      "launch_url" : "http://YOUR_SUBDOMAIN.gridlastic.com",
      "selenium_port"  : 80,
      "selenium_host"  : "YOUR_SUBDOMAIN.gridlastic.com",
      "log_path": "reports",
      "end_session_on_fail" : false,
      "silent": true,
      "screenshots" : {
        "enabled" : false,
        "path" : ""
      },
      "request_timeout_options": {
        "timeout": 600000,
        "retry_attempts": 2
      },
      "desiredCapabilities": {
        "browserName": "chrome",
        "platform":"VISTA",
        "version":"latest",
        "video":"True",
        "javascriptEnabled": true,
        "acceptSslCerts": true,
        "gridlasticUser": "USERNAME",
        "gridlasticKey": "ACCESS_KEY"
      }
    }
  }
}


Nightwatch Internet Explorer Example

nightwatch.json (selenium version 3.9+ requires "platformName":"windows" to be added to capabilities.)

{
  "src_folders" : ["tests/"],
  "output_folder": "reports",

  "selenium" : {
    "start_process" : false,
    "host" : "YOUR_SUBDOMAIN.gridlastic.com",
    "port" : 80
  },

  "test_settings" : {
    "default" : {
      "launch_url" : "http://YOUR_SUBDOMAIN.gridlastic.com",
      "selenium_port"  : 80,
      "selenium_host"  : "YOUR_SUBDOMAIN.gridlastic.com",
      "log_path": "reports",
      "end_session_on_fail" : false,
      "silent": true,
      "screenshots" : {
        "enabled" : false,
        "path" : ""
      },
      "request_timeout_options": {
        "timeout": 600000,
        "retry_attempts": 2
      },
      "desiredCapabilities": {
        "browserName": "internet explorer",
        "platform":"WIN10",
        "platformName":"windows"
        "version":"11",
        "video":"True",
        "javascriptEnabled": true,
        "acceptSslCerts": true,
        "gridlasticUser": "USERNAME",
        "gridlasticKey": "ACCESS_KEY"
      }
    }
  }
}


Nightwatch Firefox Examples

nightwatch.json (selenium version 3.5+) on WINDOWS

{
  "src_folders" : ["tests/"],
  "output_folder": "reports",

  "selenium" : {
    "start_process" : false,
    "host" : "YOUR_SUBDOMAIN.gridlastic.com",
    "port" : 80
  },

  "test_settings" : {
    "default" : {
      "launch_url" : "http://YOUR_SUBDOMAIN.gridlastic.com",
      "selenium_port"  : 80,
      "selenium_host"  : "YOUR_SUBDOMAIN.gridlastic.com",
      "log_path": "reports",
      "end_session_on_fail" : false,
      "silent": true,
      "screenshots" : {
        "enabled" : false,
        "path" : ""
      },
      "request_timeout_options": {
        "timeout": 600000,
        "retry_attempts": 2
      },
      "desiredCapabilities": {
        "browserName": "firefox",
        "platform":"WIN10",
        "platformName":"windows"
        "version":"57",
        "moz:firefoxOptions": {
           "binary": "C:\\Program Files (x86)\\Mozilla Firefox\\firefox57\\firefox.exe"
        },
        "video":"True",
        "javascriptEnabled": true,
        "acceptSslCerts": true,
        "gridlasticUser": "USERNAME",
        "gridlasticKey": "ACCESS_KEY"
      }
    }
  }
}


nightwatch.json (selenium version 3.5+) on LINUX

{
  "src_folders" : ["tests/"],
  "output_folder": "reports",

  "selenium" : {
    "start_process" : false,
    "host" : "YOUR_SUBDOMAIN.gridlastic.com",
    "port" : 80
  },

  "test_settings" : {
    "default" : {
      "launch_url" : "http://YOUR_SUBDOMAIN.gridlastic.com",
      "selenium_port"  : 80,
      "selenium_host"  : "YOUR_SUBDOMAIN.gridlastic.com",
      "log_path": "reports",
      "end_session_on_fail" : false,
      "silent": true,
      "screenshots" : {
        "enabled" : false,
        "path" : ""
      },
      "request_timeout_options": {
        "timeout": 600000,
        "retry_attempts": 2
      },
      "desiredCapabilities": {
        "browserName": "firefox",
        "platform":"LINUX",
        "version":"57",
        "moz:firefoxOptions": {
           "binary": "/home/ubuntu/Downloads/firefox57/firefox"
        },
        "video":"True",
        "javascriptEnabled": true,
        "acceptSslCerts": true,
        "gridlasticUser": "USERNAME",
        "gridlasticKey": "ACCESS_KEY"
      }
    }
  }
}


nightwatch.json (selenium version before 3.5)

{
  "src_folders" : ["tests/"],
  "output_folder": "reports",

  "selenium" : {
    "start_process" : false,
    "host" : "YOUR_SUBDOMAIN.gridlastic.com",
    "port" : 80
  },

  "test_settings" : {
    "default" : {
      "launch_url" : "http://YOUR_SUBDOMAIN.gridlastic.com",
      "selenium_port"  : 80,
      "selenium_host"  : "YOUR_SUBDOMAIN.gridlastic.com",
      "log_path": "reports",
      "end_session_on_fail" : false,
      "silent": true,
      "screenshots" : {
        "enabled" : false,
        "path" : ""
      },
      "request_timeout_options": {
        "timeout": 600000,
        "retry_attempts": 2
      },
      "desiredCapabilities": {
        "browserName": "firefox",
        "platform":"WIN10",
        "version":"57",
        "video":"True",
        "javascriptEnabled": true,
        "acceptSslCerts": true,
        "gridlasticUser": "USERNAME",
        "gridlasticKey": "ACCESS_KEY"
      }
    }
  }
}


Run Nightwatch Test Example


nightwatch -t /tests/test.js -c nightwatch.json


NOTE: Gridlastic auto scaling requires all 3 test environment parameters platform, browser and browser version to be specified in each request in order to launch test nodes to fulfill test demand. Video recording is optional. See test environments for capabilities options.
It is important to ensure that ".end()" is always called for proper test execution and creation of video recordings of failed tests.