In this quickstart, you will learn how to create and run your first Nightwatch test from scratch.

Prerequisites

Make sure Node is installed on the system. The version used for this tutorial is v16.14.2

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

You need one or more WebDriver packages installed depending on which browsers you want to target (make sure you have the browser installed and updated). Run one of the following commands to install:

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: write a test

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

mkdir tests

Then create a new file called ecosia.js inside the tests folder and add the following code:

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 3: run the test

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

Firefox
npx nightwatch tests/ecosia.js --env firefox
Chrome
npx nightwatch tests/ecosia.js --env chrome
Safari
npx nightwatch tests/ecosia.js --env 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)

For additional help you can jump on to our Discord Server.

Improve this article