Unleash the Test Automation Power Through Nightwatch.JS
Kushan Shalindra Amarasiri
Director Quality Engineering at Social Catfish
During the past weeks I have looked and research on the new test automation tools emerged in the test automation industry. These findings and how to learn such test automation tool are articulated to disseminate knowledge among my colleagues in the global software testing industry.
There are two fantastic tools that I have discovered which uses Java Scripting technology and which also supports on AngularJS web applications. Namely they are Cypress.io and Protractor. They are simply awesome when it comes to AngularJS support. Cypress.io, has achieved a lot still being an infant to the industry. They must improve a lot in terms of data configuration and re-usability. In terms of execution speeds its fantastic. Protractor on the other hand has matured a lot, but it’s a bit slow as it relies on WebDriver.
Today, in this article I'm going to cover another fantastic tool called "NightwatchJS". NightwatchJS is also a Java Scripting based test automation tool. It uses the WebDriver to execute its commands. Following picture was taken from NightwatchJS official website (nightwatchjs.org) that shows how a command on the browser is executed via WebDriver.
As NightwatchJS lies on top of WebDriver, it supports all the browsers that WebDriver supports. Additionally, NightwatchJS scripts can be also executed in Cloud Testing tools such as Sauce Labs and BrowserStack.
Let’s look at how we can jump start with NightwatchJS.
First of all, we have to install NodeJS in our machine.
Next let’s go to command prompt and install the Nightwatch package in our computer. To install type the following command.
npm install -g nightwatch
This will install the NightwatchJS test automation tool in your system.
Next let open up notepad and copy & paste the following JASON file contents. Save the file as nightwatch.jason to npm\node_modules\nightwatch\bin folder.
{
"src_folders" : ["./examples/tests"],
"output_folder" : "./examples/reports",
"custom_commands_path" : "./examples/custom-commands",
"page_objects_path" : "./examples/pages",
"custom_assertions_path" : "",
"globals_path" : "",
"live_output" : false,
"parallel_process_delay" : 10,
"disable_colors": false,
"test_workers" : false,
"selenium" : {
"start_process" : false,
"server_path" : "",
"log_path" : "",
"host" : "https://www.dhirubhai.net/redir/invalid-link-page?url=127%2e0%2e0%2e1",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "",
"webdriver.ie.driver" : "",
"webdriver.firefox.profile" : ""
}
},
"test_settings" : {
"default" : {
"launch_url" : "https://localhost",
"selenium_host" : "https://www.dhirubhai.net/redir/invalid-link-page?url=127%2e0%2e0%2e1",
"selenium_port" : 4444,
"silent" : true,
"disable_colors": false,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities" : {
"browserName" : "chrome",
"javascriptEnabled" : true,
"acceptSslCerts" : true
}
},
"chrome" : {
"desiredCapabilities": {
"browserName": "chrome"
}
},
"saucelabs" : {
"selenium_host" : "ondemand.saucelabs.com",
"selenium_port" : 80,
"username" : "${SAUCE_USERNAME}",
"access_key" : "${SAUCE_ACCESS_KEY}",
"use_ssl" : false,
"silent" : true,
"output" : true,
"screenshots" : {
"enabled" : false,
"on_failure" : true,
"path" : ""
},
"desiredCapabilities": {
"name" : "test-example",
"browserName": "firefox"
},
"globals" : {
"myGlobal" : "some_sauce_global"
},
"selenium" : {
"start_process" : false
}
},
"phantomjs" : {
"desiredCapabilities" : {
"browserName" : "phantomjs",
"javascriptEnabled" : true,
"acceptSslCerts" : true,
"phantomjs.binary.path" : "/path/to/phantomjs"
}
},
"browserstack" : {
"selenium" : {
"start_process" : false
},
"selenium_host" : "hub.browserstack.com",
"selenium_port" : 80,
"silent" : true,
"desiredCapabilities": {
"name" : "test-example",
"browserName": "firefox",
"browserstack.user" : "...",
"browserstack.key" : "..."
}
},
"testingbot" : {
"selenium_host" : "hub.testingbot.com",
"selenium_port" : 80,
"apiKey" : "${TB_KEY}",
"apiSecret" : "${TB_SECRET}",
"silent" : true,
"output" : true,
"screenshots" : {
"enabled" : false,
"on_failure" : true,
"path" : ""
},
"desiredCapabilities": {
"name" : "test-example",
"browserName": "firefox"
},
"selenium" : {
"start_process" : false
}
}
}
}
Lets download the latest Selenium standalone server from the following Selenium web link.
Next start the server from command prompt refering the location you have saved the file with the following command.
java -jar c:\Tools\selenium-server-standalone-3.8.1.jar
Since we are going to execute the script in Chrome, download the Chrome Driver to a desired location. Specify that location in the webdriver.chrome.driver variable in your jason file.
Next lest create our first script searching a the keyword NightwatchJS.
module.exports = {
'Demo test Google' : function (client) {
client
.url('https://www.google.lk/')
.waitForElementVisible('body', 1000)
.assert.title('Google')
.assert.visible('input[type=text]')
.setValue('input[type=text]', 'Wikipedia')
.click('img[id=hplogo]')
.waitForElementVisible('input[name=btnK]', 2000)
.click('input[name=btnK]')
.pause(1000)
.end()
}
}
Save the script in your tests folder of NightwatchJS installation as google.js file.
Now execute your test script via the following command.
C:\Users\ksamarasiri>nightwatch C:\Users\ksamarasiri\AppData\Roaming\npm\node_modules\nightwatch\bin\examples\tests\google.js
But the file paths may vary according to your NightwatchJS installations. Pleas also note that you have to manually bring up the selenium server.
Hope this article gave a jump start you test automation on NightwatchJS. Lets looks at more advance techniques like Page Object support in an upcoming article.
Test Automation Architect at Allan Gray Proprietary Limited
7 年No need for separate selenium grid, run standalone and automatically start selenium before the test run. https://github.com/cape-town-testing/tooling/tree/master/nightwatchjs
Test Automation Lead / Crowtester
7 年Does it included on serenity or cucumber based framework ?
Developing Tools for Software Productivity and Enablement
7 年Good one, but whats the basic difference between Protractor and NightwatchJS then?