Nightwatch.js – Get started with automation

It sometimes feels like the robots are coming to the QA world, with automation and automated testing being such a hot topic at the moment.

The inevitable fear for those with no programming experience is how they can adopt the benefits of automation, without the stress of learning to be the next Lead Developer in their organisation.

Introducing Nightwatch.js

Nightwatch.js is a testing solution that lets you use JavaScript (an easy-to-learn scripting language) to write browser-based, end-to-end tests. Whilst you may not be well versed in JavaScript, Nightwatch is put together in a way that makes testing a breeze.

In this article I’ll show you how to get Nightwatch installed and running tests in Chrome.

Preparation

These installation steps assume you’re using a Mac or Linux machine. The steps are very similar if you’re using Windows, but if you get stuck just contact us.

Installing NodeJS

The first piece of software you’ll need installed on your machine is NodeJS. Node allows you to run JavaScript programs on any machine. To do this:

Prepare your workspace

You’re going to need a working folder, within which we’ll place our tests, and the various bits and pieces we need to get up and running.

cd ~
mkdir automation
cd automation
mkdir bin
mkdir tests

Selenium Server

Nightwatch.js uses Selenium to perform tests, so you’re going to need a copy of Selenium Server:

Chrome Driver

I’m going to assume you have Chrome installed on your machine, but there are drivers for all major browsers if you don’t.

This driver allows Selenium to control Chrome for you. The steps to fetch this are very similar to the Selenium Server steps above:

Installing Nightwatch.js

Create a Nightwatch configuration file

Assuming you’ve followed the steps above, you should have a folder called automation, that contains a bin folder with a copy of Selenium Standalone Server and Chrome Driver in it, and an empty tests folder.

The next thing we’re going to do is create a configuration file for Nightwatch.js to refer to. I won’t go into the details on each of the settings in this article, for now you can refer to configuration documentation on the Nightwatch.js site.

{
"src_folders" : ["tests"],
"output_folder" : "reports",

"selenium" : {
  "start_process" : true,
  "server_path" : "bin/selenium-server-standalone-3.0.1.jar",
  "log_path" : "",
  "port" : 4444,

  "cli_args" : {
    "webdriver.chrome.driver" : "bin/chromedriver"
  }
},

"test_settings" : {
  "default" : {
    "launch_url" : "http://localhost",
    "selenium_port"  : 4444,
    "selenium_host"  : "localhost",
    "silent": true,

    "screenshots" : {
      "enabled" : false,
      "path" : ""
    },

    "desiredCapabilities": {
      "browserName": "chrome"
    }
  }
}
}

Install Nightwatch

It’s important to note that you can install nightwatch globally on your machine by typing npm install -g nightwatch. This has the benefit of allowing you to run nightwatch from any folder, however I favour a local install myself.

Your first test

module.exports = {
  'Basic Google search' : function (browser) {
    browser
      .url('https://www.google.com')
      .waitForElementVisible('input[name="q"]', 1000)
      .setValue('input[name="q"]', 'Guinea pig existential crisis')
      .submitForm('form')
      .waitForElementVisible('#resultStats', 1000)
      .assert.urlContains('q=Guinea+pig')
      .end();
  }
};

That’s it!

You now have your very own automation engine ready and waiting at your command!

Check out the Nightwatch.js website for a list of the different commands and assertions that are available to you, and let us know in the comments if there is something you’d like us to cover next.