What is Nightwatch.js?

Nightwatch.js is an automated testing framework for web applications written in Node.js and utilizing the W3C WebDriver API (formerly Selenium WebDriver). It uses the powerful W3C WebDriver API to perform commands and assertions on DOM elements.

It is a fully (End-to-End) browser testing solution which aims to simplify the process of establishing a Continue Integration and writing automated tests. Nightwatch can be used to write Node.js unit tests. Nightwatch gives the flexibility of utilizing JavaScript to automate the required scenarios Similar to using WebDriver.

Mode of Operation

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. Visually perceive the following diagram to understand the Nightwatch workflow.

Nightwatch is shipped with the following features:

Installing Nightwatch

Step 1: Download java (1.7 onwards)

Step 2: Download Node js

Step 3: Download and Open Visual Studio code

  1. Create a folder for nightwatch
  2. Create a file called ‘nightwatch.js’ in added folder

Step 4: Add below command in added file nightwatch.js

require(‘./node_modules/nightwatch/bin/runner.js’);

Step 5: Now open terminal from view tab and Initialize npm inside folder

Step 6: After initialize the npm, ‘package.json’ file is added in folder

Step 7: Add Nightwatch

**You can add Nightwatch to your project simply by running ‘npm install nightwatch –save.’

This places the Nightwatch executable in your ./node_modules/.bin folder, so you don’t have to install it globally**

Step 8: Download Selenium

Selenium is a suite of implements to automate web browsers across many platforms. The Selenium server is a Java application which is used by Nightwatch to connect to various browsers.

Step 9: Download Chromedriver

ChromeDriver is a standalone server which implements the W3C WebDriver wire protocol for Chromium.

Step 10: Configuring Nightwatch.json

{

    “src_folders” : [“tests”],

    “output_folder” : “reports/XMLReports”,

    “custom_commands_path” : “”,

    “custom_assertions_path” : “”,

    “page_objects_path” : “pages”,

    “globals_path” : “”,

    “selenium” : {

      “start_process” : true,

      “server_path” : “./lib/drivers/selenium-server-standalone-3.14.0.jar”,

      “start_session” : true,

      “log_path” : “log/”,

      “host”: “127.0.0.1”,

      “port” : 4444,

      “cli_args” : {

        “webdriver.chrome.driver” : “lib/drivers/chromedriver.exe”

      }

    },

    “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”

        }

      }

    }

Writing your first Nightwatch Test

Let’s add a new file in the tests folder, call demo.js

module.exports = {

    ‘Demo test Google’ : function (client) {

      client

        .url(‘http://www.google.com’)

        .waitForElementVisible(‘body’, 5000)

        .assert.title(‘Google’)

        .assert.visible(‘input[type=text]’)

        .setValue(‘input[type=text]’, ‘rembrandt van rijn’)

        .waitForElementVisible(‘button[name=btnG]’, 5000)

        .click(‘button[name=btnG]’)

        .pause(1000)

        .assert.containsText(‘ol#rso li:first-child’,

          ‘Rembrandt – Wikipedia’)

        .end();

    }

  };

Author : Shivam Sharma

SHARE THIS

Get A Free Quote

Leave a Reply

Your email address will not be published. Required fields are marked *

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