How to setup Nighwatch in a multi-OS configuration

Mai 26, 2016
Sébastien Berten
Introduction

This article is a follow up of http://blog.js-republic.com/introduction-to-nightwatch-for-lightning-acceptance-tests/

Nighwatch in a team with multiple Operating systems

In the real world, you may encounter a team running several OSes : Windows, MacOS, Linux, etc. When you want to run an acceptance test, it must be runnable under each OS.
Nightwatch uses Selenium as an underlying browser runner, however, the browser driver has to be different under each OS : you do not want to run a Windows version of Chrome whenever Nighwatch runs under MacOS, and vice-versa.
Fun fact : you do NOT need the following setup if you are only running acceptance tests under Firefox (MacOS, or Windows). Only Chrome requires the following setup.

The setup

First, you need to grab a Selenium Chrome driver for windows. The latest driver is available here. Put this under (your_preferred_nighwatch_path)/lib/chromedriver.exe : the other chromedriver for MacOS/Linux is already available under our node_modules folder.
Second, you need to create a nightwath.conf.js file, this is where you will split the drivers :
[code lang=”js”]module.exports = (function (settings) {
//Setting chromedriver path at runtime to run on different architectures
if (process.platform === "darwin") {
settings.selenium.cli_args["webdriver.chrome.driver"] = "./node_modules/chromedriver/bin/chromedriver";
}
else if (process.platform === "win32" || process.platform === "win64") {
settings.selenium.cli_args["webdriver.chrome.driver"] = "nw/lib/chromedriver.exe";
}
return settings;
})(require(‘./nightwatch.json’));
[/code] Third, you need to create a nightwatch.js file. This is a runner, and this is mandatory if you want to run tests under Windows, runner.js is already bundled with Nightwatch :
[code lang=”js”]require(‘./node_modules/nightwatch/bin/runner.js’);[/code]

Wrapping up

Now that we have split our drivers, you can get rid of the webdriver.chrome.driver definition under your nightwatch.json selenium configuration, because it will be automatically completed :
[code lang=”js”]"selenium" : {
"start_process" : true,
"server_path" : "nw/lib/selenium-server-standalone-2.53.0.jar",
"log_path" : "nw/logs",
"host" : "127.0.0.1",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "", // empty !
"webdriver.ie.driver" : ""
}
}
[/code] Ultimately, go back to your package.json, update the scripts field and tell Nightwatch to use our newest splitter :
[code lang=”js”]"scripts": {
"test": "node nightwatch.js -c nightwatch.conf.js"
},
[/code] We are done, just run it :

npm test

Under MacOS/Linux, ./node_modules/chromedriver/bin/chromedriver will run, under Windows, nw/lib/chromedriver.exe will run instead.
Happy testing !
Laurent, Développeur Javascript  @JS-Republic

JS-REPUBLIC est une société de services spécialisée dans le développement JavaScript. Nous sommes centre de formation agréé. Retrouvez toutes nos formations techniques sur notre site partenaire dédié au Training

Tags: