Nightwatch.js Tutorial | Node.js powered testing framework

Nightwatch.js is yet another open-source project automated test framework written in node.js. Its main aim is to simplify End-to-End testing and CI integration. Although it can also be used for node.js unit and integration testing. The name ‘Nightwatch’ inspired by the painting ‘The Night Watch’ by Rembrandt van Rijn.

Prerequisites for Nightwatch.js

Node.js

You need to have node.js installed on your system before installing Nightwatch.js. To check if you have node.js installed on your system simply type:

node -v

and you see the version of the node.js.

This should also install npm ( node package manager). To check it;

npm -v

and npm version should be prompted.

nightwatch.js

WebDriver

Nightwatch.js supports 4 major browsers ( i.e Chrome, Firefox, Internet Explorer and Safari). For your Nightwatch.js tests to run on the browser of your choice you need to get the webdriver for that specific browser. You can install the browser either through the web pages or through npm.

For running your tests on multiple browsers you may use selenium grid.

Setting Up the Project

Nightwatch.js tests are written in mocha. If you want to know more about mocha, we also have a tutorial about mocha here.

Let’s create our project on the desired folder with the default package.json.

npm init -y

Now let’s install nightwatch to our project and save it as dev-dependencies.

npm install nightwatch –save-dev

This command should add node-modules in the project directory and Nightwatch as devDependencies in package.json.

Once we installed Nightwatch we may replace the test script to nightwatch command in package.json. This is necessary to run Nightwatch tests using npm test command.

“scripts”: {
  “test”: “nightwatch”
}

Please note that if we run npm test  now it will spit out an error since Nightwatch requires nightwatch.json or nightwatch.conf.json.

All Nightwatch projects require this configuration file which consists of basic configurations. Nightwatch configuration file must have 2 base settings, webdriver, and test settings.

So let’s create nightwatch.json under the project directory. We can copy-paste a basic configuration to get a simple test running from the Nightwatch documentation.

{
  "src_folders" : ["tests"],

  "webdriver" : {
    "start_process": true,
    "server_path": "node_modules/.bin/chromedriver",
    "port": 9515
  },

  "test_settings" : {
    "default" : {
      "desiredCapabilities": {
        "browserName": "chrome"
      }
    }
  }
}

Here we have test folder directory ( i.e tests, the name can be anything ), webdriver properties ( like path, port etc.), and desired capabilities with a default value.

Let’s create tests folder inside the project since Nightwatch will look for that folder. And we need to add a basic test inside this folder. I will name it basicTests.js If everything went smooth running npm test will open a chrome browser and close it down since our test does not do anything yet.

Writing Nightwatch.js Tests

There are plenty of test examples on Nightwatch repository which will give you an idea about writing tests for nightwatch. You may also follow official documentation for writing tests with nightwatch.

Each file inside our test directory will be considered as a test suite by the Nightwatch test runner. Inside each file, the test runner will look for different keys that are exported.

So let’s start by module.export.

module.exports = {
	
}

As I mentioned before test runner will look for keys which are basically names of the test cases. So for our first test;

module.exports = {
‘Get URL and title’ : function(browser) {
	browser
		.url(‘http://www.google.com’)
   }
}

Now we have created a test case that navigates to google.com. Please note that the browser object is the object used by Nightwatch to manipulate the browser. You may see the full list of actions you can do with it here.

We need assertion to understand if we have really navigated to the google homepage. To do that, I am going to assert the url and title of the homepage.

module.exports = {
‘Get URL and title’ : function(browser) {
	browser
		.url(‘http://www.google.com’)
		.assert.urlContains(‘google’)
		.assert.title(‘Google’);
   }
}

Now run npm test again and our test will run 2 assertions will pass.

nightwatch.js test result

Thanks.
Ege Aksoz

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.