Automated testing with nightwatch js

Automation Testing using NightWatchJS

In this blogpost, we will take a dive into a step by step NightWatchJS tutorial to perform automation testing for browsers. We will also explore on how to install and setup NightWatchJS and look into details about its configuration. We can also have a look at executing our first NightWatchJS script.

Introduction

Nightwatchjs, is an open source automated testing framework that aims at providing end to end solution to automated testing with Selenium javascript for web based applications. Framework internally uses the powerful W3C WebDriver API or the selenium WebDriver and simplifies writing automated test cases in node js and can be used in Continous integration pipelines

Nightwatch JS has certainly a lot of features out of box like In built test runner, support for different test strategy, support for cloud services such as Experitest and offers good support to many continuous integration tools like Teamcity, Jenkins etc.

Installation

Node JS is one of the prerequisites for installing Nightwatch JS. Once Node js is installed we can install nightwatch js from NPM using the following command:

$ npm install --save-dev nightwatch

One might also want to install the chrome driver, which is a standalone server that implements the W3C web driver wire protocol for chromium.

Configuration

Now that we have managed to install NightWatch JS, it’s time to dive into automation testing using javascript. As mentioned earlier, nightwatch js comes in with in built test runner which expects Json configuration file. The default configuration is usually in a file named “nightwatch.json“, however one can also choose to use “nightwatch.conf.js” for configuration. However if both files are present, then nightwatch.conf.js would be give precedence over nightwatch.json.

The configuration would look something like this:

As you would have noticed in the configuration json file, there are various settings and configurations. The directory of tests, reports, test workers property which will help in understanding to run tests in parallel or not, and most importantly test settings which helps us define test parameters, configurations and different environments too.

Now I have my test runner configured, next part is to write my automation tests, so here is one of my sample test where I would launch google.com and search for nightwatch js on google.

Now that we have added our first script, there is one last change to make in our source folder, open package.json file and add the following lines to the file

scripts": {
    "test": "./node_modules/.bin/nightwatch -e default"
  },

This lets the test runner understand it has to run test against default configuration.

Finally, to execute the tests, enter the following command in terminal,

npm test

This command will then check for all the dependencies and then execute the test suite.

Feel free to drop in a comment to this post or you can drop an email to naik899@gmail.com