Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
Introduction to Nighwatch.js
Features of Nightwatch.js
How Nightwatch.js works?
Prerequisites of Nightwatch.js
Last Updated: Mar 27, 2024

What is Nightwatch.js framework?

0 upvote
Create a resume that lands you SDE interviews at MAANG
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM


Nightwatch.js is a test automation framework that never fails to take the spotlight when you mention the use of Selenium and JavaScript. Currently, Selenium is one of the top tools for automated testing of browsers. Selenium is compatible with a wide range of automated testing frameworks used for multiple programming languages. The fact that Selenium is open-source highly contributes to its popularity.

Nightwatch.js coordinates with Selenium and JavaScript for performing automated browser tests. The installation process and set-up process are very crucial, as there are several configuration updates in multiple files and folders. We usually run scripts in the Nightwatch.js for triggering tests. Although the framework is very efficient and interactive, there are some common bugs that you may encounter.

Manually testing applications is a redundant task that annoys developers. For manual testing, there has to be an assurance team, as the report from one developer is not sufficient to prove the efficiency of the software product. To overcome this high expenditure on testing resources and teams, front-end tests can be made fully automated using Nightwatch.js. It is a simple, yet powerful automation testing framework. Most modern web browsers support this framework and it is compatible with both Windows and Mac operating systems.

Introduction to Nighwatch.js

Nightwatch.js is an open-source automated testing framework that works with Selenium or JavaScript to find bugs in web-based applications, websites, or browser applications. Nightwatch.js is powered by Node.js and focuses on providing complete E2E (end-to-end) solutions for machine-based testing.

The entire Nightwatch.js framework is dependent on Selenium.Nightwatch.js comes with a wide range of commands and assertions for performing operations on DOM elements. The internal configuration of Nightwatch.js consists of the impactful W3C Web Driver API or the Selenium Web Driver API.

The use of APIs reduces the effort required for writing end-to-end automated tests in Node.js and provides an easy set-up for Continuous Integration. This reduces the human resources required for a project and makes the project scalable. Nightwatch.js detects the efficiency of browser-based applications or websites, by simulating the request and calculating the ‘to and fro’ timing of the scheduled request created in its environment.

Nightwatch.js is customizable, therefore, you can append changes or functionalities to meet the requirements of your project. The use of JavaScript makes it easier for web developers to catch up with the Nightwatch.js framework, as JavaScript is one of the most popular languages in the web developers’ community. Efficient testing helps in assuring that the project covers all the corner case, and real-time scenarios and is analogous to the SRS document verified by the client.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Features of Nightwatch.js

  • Browser Automation
    Nightwatch.js is an easy to use End-to-End testing solution for web-browser applications and websites. It is very easy for beginners as it is written in Node.js. It uses the W3C Web Driver API or Selenium APIs to drive browsers. For issuing commands and assertions on DOM elements it uses a wide range of APIs from this set.
  • Distinct Syntax
    The syntax of Nightwatch.js is simple yet powerful. This helps developers to write tests in a very short span of time. Developers may choose their preferred programming language from JavaScript (Node.js), CSS or Xpath selectors for writing tests.
  • Cloud Testing Support
    Nightwatch.js resolves storage and computing issues, as it is accompanied by (TBA) – a cloud testing platform.Nightwatch.js is compatible with other cloud testing providers. For instance,  SauceLabs and BrowserStack.
  • Page Objects Support
    The framework is easier and more fluent for the beginner as it provides Page Object Model support. It is compatible with both CSS and XPath selectors. This is employed for organizing element and sections in a well-structured manner.
  • Default Test Runner
    The Built-in command-line test runner is highly efficient. Apart from the traditional sequential testing paradigm, it also allows us to run parallel tests. Implicitly it uses retire and wait functions, wherever needed. In fact, Nightwatch.js also supports encapsulation of test suites and tags for segregation as well as binding. This helps in the modularisation of the test suites.
  • Web Driver Service
    It has the power to automatically handle Selenium or alternate Web Driver services, such as Chrome Driver, Gecko Driver, Edge, and Safari, in a distinct child process.
  • Easily Extendible
    The interface provides a very high rate of flexibility in case of command and assertions. This makes it very easy to add custom commands and assertions, to increase the functionality and features of the framework. You can add as many commands you want to meet the client’s requirements.
  • Continuous Integration
    It comes in with built-in support for JUnit XML reporting. This allows developers to integrate test with build process in the system, as in, Teamcity, Hudson, Jenkins and so on.

How Nightwatch.js works?

Most of the top-notch web browsers come with a categorical WebDriver implementation, known as a driver, which is internally used by Nightwatch.js for communicating with the browser.Nightwatch.js relays between the various browser WebDrivers, such as ChromeDriver for Chrome or GeckoDriver for Firefox, and so on. For establishing the communication and relaying requests Nightwatch.js employs a restful HTTP API with the help of an HTTP protocol laid down by the W3C WebDriver API and extracted from JSONWire protocol.

Nightwatch.js issues dual requests to the WebDriver server, for carrying out any browser interaction. The request can be issued with the help of a command, an assertion, or an action, available on page objects of the web application.

  • The first request is sent to the Selenium server, for creating a session with the browser. It also locates the target element, on which action is to be performed, with the help of CSS or XPath selector of the object.

Image Source:

  • The second request is to trigger the action, on the extracted web element, as a result of the prior web request.

Image Source:

Prerequisites of Nightwatch.js

  • Node.js: As we already know the Nightwatch.js framework is built on top of Node.js. This clearly implies that we need to install Node.js, for working with Nightwatch.js
  • Node Package Manager (npm): After Node.js is successfully installed, the node’s package manager i.e. npm is installed. The npm is referred to as the widest ecosystem of packages.

Now, the below command is executed to install the latest version using the npm command-line tool, (here ‘g’ indicates installing globally):
$ npm install -g nightwatch

To move the Nightwatch executable (‘–save-dev’) in our ./node_modules/.bin folder, run the below command:
$ npm install –save-dev nightwatch

As we already know that Nightwatch.js framework is built on top of a Node.js. This clearly implies that we need to install Node.js, for working with Nightwatch.js system.

  • Java – SDK: Well, Java SDK is an indirect yet mandatory requirement of Nightwatch.js framework. Java is required for using remote Selenium Server. As Nightwatch.js is completely dependent on the Selenium Web Driver API and also requires a Selenium Web Driver Server. Therefore, you need to install the Java Development Kit (JDK 7+) on your system and configure the JAVA environment.
  • Selenium Server: It needs the Selenium standalone server package JAR, which is available for download at the Selenium site. After successful download, place it in the bin folder of the project and start the selenium server using the command: selenium-server-standalone-{version}.jar
  • Chrome Driver: Finally, it needs a Chrome Driver. It is a standalone server. Chrome Driver works on the W3C Web Driver wire protocol, in case of Chromium. Place this executable inside the same bin folder.

Use the following command for installation: $ npm install –save-dev chromedriver</pre>


Given is a code snippet that depicts a test script for automation testing in Nightwatch.js. It will execute the search for Nightwatch on Google and then validate the Nightwatch JS documentation on the website.

module.exports = {
‘NW test on Google’ : function (client) {
.waitForElementVisible(‘body’, 1000)
.setValue(‘input[type=text]’, ‘Nightwatch JS’)
.waitForElementVisible(‘button[name=btnG]’, 1000)
.assert.containsText(‘ol#rso li:first-child’,
‘Nightwatch.js | Node.js powered End-to-End testing framework’)

You are allowed to modify the nightwatch.json configuration file and the global module file i.e. nightwatch.globals.js according to your requirements but it should be analogous to the snippet mentioned-below:

“src_folders” : [“./tests”],
“output_folder” : “./reports”,
“globals_path” : “nightwatch.globals.js”,
“test_workers”: {
“enabled”: true,
“workers”: “auto”
“selenium” : {
“start_process” : true,
“server_path” : “./node_modules/selenium-server-standalone-jar/jar/selenium-server-standalone-3.141.59.jar”,
“log_path” : “nw/logs”,
“host” : “”,
“port” : 4444,
“cli_args” : {
“” : “./node_modules/chromedriver/bin/chromedriver”,
“” : “”
“test_settings” : {
“default” : {
“launch_url” : “”,
“selenium_port” : 4444,
“selenium_host” : “localhost”,
“silent”: true,
“screenshots” : {
“enabled” : true,
“path” : “”
“desiredCapabilities”: {
“browserName”: “chrome”,
“javascriptEnabled”: true,
“acceptSslCerts”: true
“french” : {
“launch_url” : “”,
“desiredCapabilities”: {
“browserName”: “firefox”,
“javascriptEnabled”: true,
“acceptSslCerts”: true

Do not forget to add the below section in the package.json file for executing the tests folder for Nightwatch testing:

scripts”: {
“test”: “./node_modules/.bin/nightwatch -e firefox,edge,safari test”

Finally, you are required to trigger Nightwatch testing from the base directory of the project with the help of the following command: npm test

This command is used for validating the tests and dependencies. After successful validation, it launches the test suite. This opens up the Chrome browser and then Goggles the given search string.

Previous article
Best JavaScript Editor
Next article
5 Useful JavaScript Tips and Tricks
Live masterclass