In this quickstart, you will learn how to create and run a Nightwatch test using Selenium Server

Selenium Server allows the execution of WebDriver scripts on remote machines by routing commands sent by the client to remote browser instances. It aims to provide an easy way to run tests in parallel on multiple machines.

Prerequisites

  1. Make sure Node is installed on the system. The version used for this tutorial is v16.14.2
  2. Make sure Java is installed on the system. The version used for this tutorial is openjdk 11.0.14

Step 0: create a new project

Create a new directory and initiate a node project there.

mkdir nightwatch-project
cd nightwatch-project
npm init

Step 1: install Nightwatch and dependencies

Install the latest version of nightwatch

npm install nightwatch --save-dev

Browser Drivers

Depending on your target browsers (make sure you have the browser installed and updated), you will need one or more specific WebDriver packages. Dependenging on you browser run one of the following:

Firefox

npm install geckodriver --save-dev

Chrome

npm install chromedriver --save-dev

Microsoft Edge Driver

Follow the Download Microsoft Edge Driver section on the official Microsoft Edge documentation page to download the Edge Driver.

SafariDriver

The safaridriver binary is already installed on recent versions of Mac OS, however some manual configuration is needed before tests can be run against Safari.

You will need to run the following once, before using the safaridriver:

safaridriver --enable

Step 2: install Selenium Server

Selenium Server is a Java application, which means you will also need to have the Java Development Kit (JDK) installed, minimum required version is 7. You can check this by running java -version from the command line.

The easiest way to install the Selenium Server is from NPM using the @nightwatch/selenium-server package which is maintained by the Nightwatch team. Nightwatch automatic configuration is already prepared for usage with this package against Chrome, Firefox, and Internet Explorer.

npm install @nightwatch/selenium-server

Step 3: write a test

Create a new folder called tests inside nightwatch-project folder.

mkdir tests

Then create a file called ecosia.js inside the tests folder and put the following code in that file:

tests/ecosia.js
describe('Ecosia.org Demo', function() {

  before(browser => browser.navigateTo('https://www.ecosia.org/'));

  it('Demo test ecosia.org', function(browser) {
    browser
      .waitForElementVisible('body')
      .assert.titleContains('Ecosia')
      .assert.visible('input[type=search]')
      .setValue('input[type=search]', 'nightwatch')
      .assert.visible('button[type=submit]')
      .click('button[type=submit]')
      .assert.textContains('.layout__content', 'Nightwatch.js');
  });

  after(browser => browser.end());
});
The above test opens the search engine Ecosia.org, types the term "nightwatch" into the search input field, then verifies if the results page contains the text "Nightwatch.js".

Step 4: run the test

Use the bundled npx tool from NPM to quickly run the nightwatch command:

Firefox
npx nightwatch tests/ecosia.js --env selenium.firefox
Chrome
npx nightwatch tests/ecosia.js --env selenium.chrome
Safari
npx nightwatch tests/ecosia.js --env selenium.safari

The output should look similar to this:


[Ecosia.org Demo] Test Suite
============================
β„Ή Connected to localhost on port 4444 (2153ms).
  Using: firefox (94.0.1) on mac 20.6.0 platform.

βœ” Running Demo test ecosia.org:

βœ” Element <body> was visible after 24 milliseconds.
βœ” Testing if the page title contains 'Ecosia' (10ms)
βœ” Testing if element  is visible (51ms)
βœ” Testing if element  is visible (12ms)
βœ” Testing if element <.mainline-results> contains text 'Nightwatch.js' (197ms)

OK. 5 assertions passed. (1.838s)
Improve this article