Nightwatch.js – how to automatically download the latest Selenium Server and Chromedriver for your operating system

When running Nightwatch.js browser automated tests against Chrome, you will need to download and run Selenium Standalone Server and a matching version of ChromeDriver for your operating system.

One way to do this is to detect the operating system in the nightwatch.conf.js file and then point to an installed matching version of ChromeDriver for your operating system. This works, but has the disadvantage that you need to store in your repository multiple static versions of ChromeDriver for each operating system that you use. Plus later, as versions change, you will need to update your repository with newer versions of ChromeDriver for each operating system.

Another approach is to use the “groupon/selenium-download” npm package. With one file addition, this package allows you to simply run an “npm install” against a clone of your repository which then downloads the latest matching version of Selenium Standalone Sever and ChromeDriver for your operating system.

From there, you just run your tests – the selenium-standalone-server.jar server starts (if “start_process” is set to “true” in your nightwatch.json file) and the correct version of ChromeDriver launches (as specified in your nightwatch.json file).

The trick is adding a Javascript file that runs as part of the “npm install”. For example, consider this “install-selenium-chromedriver.js” file:

var chalk = require("chalk");
var selenium = require('selenium-download');
var path = require("path");
var binPath = path.join(__dirname, '..', 'bin');

selenium.ensure(binPath, function (error) {
  if (error){
    console.error(error.stack);
  } else {
     console.log( chalk.green('√ Selenium & Chromedriver downloaded to:', binPath) );
  }  
  process.exit(0);
});

This file is a slightly modified version of the example listed on the “groupon/selenium-download” Github page. First, I added a “chalk” requirement just because I like to see colored output when steps succeed (chalk.green, in this case.) 🙂

Second, I use the “path.join” function to create a path back to “<current directory>\..\bin” for Windows or “<current directory>/../bin” for Mac or Linux. That’s the advantage of using the path.join function – it creates the correct forward or backward “slashes” for the operating system you are using. Other than that, the “selenium-download” functionality is pretty much the same as the example on groupon/selenium-download’s Github site.

Next, in the “package.json” file, you add the “selenium-download” module to the dependencies section plus a “postinstall” script that runs the above install-selenium-chromedriver.js file . As you can see in the npm-scripts documentation, “postinstall” scripts run after the package is installed. So, in this case, all npm dependencies install, then Selenium Standalone Server and ChromeDriver install, like this:

{
  "name": "nightwatch-selenium-download",
  "version": "1.0.0",
  "main": "nightwatch.js",
  "directories": {
    "test": "tests"
  },
  "dependencies": {
    "nightwatch": "^0.9.8",
    "selenium-download": "^2.0.6"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "postinstall": "node ./lib/install-selenium-chromedriver.js"
  },
  "author": "John Antony",
  "license": "ISC",
  "description": "automatically run Nightwatch.js tests with latest Selenium Server and ChromeDriver for your operating system"
}

Finally, you reference selenium.jar and chromedriver in the “./bin” directory as specified in the “selenium” section of your nightwatch.json file, like this (excerpt):

  "selenium" : {
    "start_process" : true,
    "server_path" : "bin/selenium.jar",
    "log_path" : "log",
    "host" : "127.0.0.1",
    "port" : 4444,
    "cli_args" : {
      "webdriver.chrome.driver" : "bin/chromedriver",
      "webdriver.ie.driver" : ""
    }
  }

Lastly, it’s handy to add a “nightwatch.js” file at your project’s root level directory that references Nightwatch.js’ test runner file. This makes it easy to run tests on Windows systems as well:

require('./node_modules/nightwatch/bin/runner.js');

Installation & Running Tests

On Windows in a command prompt window (cmd), run:

npm install
node nightwatch.js -t tests\google.js

or on Mac and Linux, in a terminal window, run:

npm install
node nightwatch.js -t tests/google.js

Code

This basic project can be found and cloned from here:

https://github.com/jantonypdx/nightwatch-selenium-download

git clone https://github.com/jantonypdx/nightwatch-selenium-download

Note that the google.js test found in the tests directory is pretty basic. It just opens Google and searches for “nightwatch.js”. I added it as a simple demonstration for this project.

Software automation engineer in Portland, OR. LinkedIn: jantonypdx, Twitter: jantony_pdx
Posts created 10

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top