Starting Selenium Server with Nightwatch.js Starting Selenium Server with Nightwatch.js selenium selenium

Starting Selenium Server with Nightwatch.js


As suggested by this great guide to Nightwatch, dwyl-learn-nightwatch, you can replace your nightwatch.json file with a .js file to add features like variables, globals and even requiring in Selenium so Nightwatch can see it and run it.

Here's a simple example I modified from that GitHub source to start selenium with its tests. Make sure to install the dependencies in the project, first:

npm install --save-dev nightwatch chromedriver selenium-server

Then replace that JSON file with a .js one, perhaps named nightwatch.conf.js and notice the config options under the selenium key in the config file:

nightwatch.conf.js

const seleniumServer = require("selenium-server");const chromedriver = require("chromedriver");const SCREENSHOT_PATH = "./screenshots/";module.exports = {  "src_folders": [    "tests/e2e"  ],  "output_folder": "./reports",  "selenium": {    "start_process": true,                // tells nightwatch to start/stop the selenium process    "server_path": seleniumServer.path,    "host": "127.0.0.1",    "port": 4444,                         // standard selenium port    "cli_args": {      "webdriver.chrome.driver" : chromedriver.path    }  },  "test_settings": {    "default": {      "screenshots": {        "enabled": true,                  // if you want to keep screenshots        "path": SCREENSHOT_PATH           // save screenshots here      },      "globals": {        "waitForConditionTimeout": 5000   // set a (default) timeout period, maybe 5s      },      "desiredCapabilities": {            // use Chrome as the default browser for tests        "browserName": "chrome"      }    },    "chrome": {      "desiredCapabilities": {        "browserName": "chrome",        "javascriptEnabled": true      }    }  }}function padLeft (count) { // theregister.co.uk/2016/03/23/npm_left_pad_chaos/  return count < 10 ? '0' + count : count.toString();}var FILECOUNT = 0; // "global" screenshot file count/** * The default is to save screenshots to the root of your project even though * there is a screenshots path in the config object above! ... so we need a * function that returns the correct path for storing our screenshots. * While we're at it, we are adding some meta-data to the filename, specifically * the Platform/Browser where the test was run and the test (file) name. */function imgpath (browser) {  var a = browser.options.desiredCapabilities;  var meta = [a.platform];  meta.push(a.browserName ? a.browserName : 'any');  meta.push(a.version ? a.version : 'any');  meta.push(a.name); // this is the test filename so always exists.  var metadata = meta.join('~').toLowerCase().replace(/ /g, '');  return SCREENSHOT_PATH + metadata + '_' + padLeft(FILECOUNT++) + '_';}module.exports.imgpath = imgpath;module.exports.SCREENSHOT_PATH = SCREENSHOT_PATH;

And the command I use to run this is this, using the locally installed nightwatch version:

nightwatch --config nightwatch.conf.js 

Hope that helps! Goodluck and good on your for testing your code.