In this blog, we are going to take a look at how you can intsall and configure Nightwatch JS, and its integration with Jenkins. Before that let's find out what Nightwatch JS is and what are its features.
Nightwatch.js is an easy to use Node.js based end-to-end (E2E) testing solution for browser based apps and websites. It uses the powerful W3C WebDriver API to perform commands and assertions on DOM elements.
Nightwatch works by communicating over a restful HTTP api with a WebDriver server (typically the Selenium server). The restful API protocol is defined by the W3C WebDriver API. See below for an example workflow for browser initialization.
Most of the times, Nightwatch needs to send at least 2 requests to the WebDriver server in order to perform a command or assertion:
Assuming you have nodejs installed in your system, here’s how to go about installing Nightwatch JS
The test runner expects a configuration file to be passed, using a nightwatch.json file from the current directory by default, if present. A nightwatch.conf.js file will also be loaded by default, if found.
Let's create the nightwatch.json in the project's root folder and add this inside:
{
"src_folders" : ["tests"],
"output_folder" : "reports",
"custom_commands_path" : "",
"custom_assertions_path" : "",
"page_objects_path" : "",
"globals_path" : "",
"selenium" : {
"start_process" : false,
"server_path" : "",
"log_path" : "",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "",
"webdriver.gecko.driver" : "",
"webdriver.edge.driver" : ""
}
},
"test_settings" : {
"default" : {
"launch_url" : "http://localhost",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities": {
"browserName": "firefox",
"marionette": true
}
},
"chrome" : {
"desiredCapabilities": {
"browserName": "chrome"
}
},
"edge" : {
"desiredCapabilities": {
"browserName": "MicrosoftEdge"
We basically define your tests within a Node module:
module.exports = {
"My test case": function(browser){
// control the browser
}
}
Jenkins is an open-source continuous integration server written in Java. It is by far the most widely used tool for managing continuous integration builds and delivery pipelines.
In order to integrate Jenkins with Nighwatch JS, you first need to have the same installation setup on the server where Jenkins is running:
So that's how you can install and started working with Nightwatch JS. If you found this blog useful, read more about PhantomJS and ChimpJS. Also watch our webinar on Eliminating JavaScript Codesmells.