Nightwatch can also be used for distributed cross-browser end-to-end testing at scale together with the Selenium Server (also known as Selenium Grid). Selenium Server is an open-source project written in Java that manages a network of WebDriver nodes.

It aims to provide an easy way to run tests in parallel on multiple machines.

Selenium Server Operation

Prerequisites

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

Setup Nightwatch

Create a new directory and initiate a Nightwatch project there:

mkdir <directory-name>
cd <directory-name>
npm init nightwatch@latest

or directly create the project in a new directory with a single command:

npm init nightwatch <directory-name>

Press y when you see the prompt to install create-nightwatch

❯ npm init nightwatch
Need to install the following packages:
  create-nightwatch
Ok to proceed? (y)

This installs Nightwatch, asks your preferences and sets up the nightwatch.conf.js file based on your preferences as shown below

Nightwatch setup using CLI Utility

Preferences

Test Runner

Nightwatch also supports other test runners. You can also pick Mocha or Cucumber JS as a test runner apart from Nightwatch.

Language - Javascript/Typescript

Nightwatch supports typescript for test files after v1.6.0. So you can choose to have the test setup in Javascript or Typescript.

? What is your Language - Test Runner setup? (Use arrow keys)
❯ JavaScript - Nightwatch Test Runner
  JavaScript - Mocha Test Runner
  JavaScript - CucumberJS Test Runner
  TypeScript - Nightwatch Test Runner
  TypeScript - Mocha Test Runner

Run on Local/Remote (Cloud)

You can configure Nightwatch to run locally on your machine, remotely on a cloud machine or both

? Where do you want to run your e2e tests? (Use arrow keys)
❯ On my local machine
  On a remote machine (cloud)
  Both

For remote testing, host and port details will be automatically added in case you select BrowserStack or Sauce Labs. However if you select to run on your own remote selenium server or any other cloud provider, you will have to manually configure the host & port details in the nightwatch.conf.js file.

Browser Selection

You can pick the browsers you'll be testing on, and the config will be automatically created for them. We provide a multi-selection option so you can pick as many browsers you want to test on. You can also use the selenium-server when testing on the local machine.

? Where you'll be testing on? (Press <space> to select, <a> to toggle all, <i> to invert selection,
 and <enter< to proceed)
❯◯ Firefox
 ◯ Chrome
 ◯ Edge
 ◯ Safari
 ◯ Local selenium-server

Check the Local selenium-server option from the list on top of the selected browsers.

Test Folder Name

Next you can name the folder where you want the tests to reside. The default value is tests.

? Where do you plan to keep your end-to-end tests? (tests)

Base URL

Add the base URL that the tests will run against. This preference will default to http://localhost

? What is the base_url of your project? (http://localhost)

Once you select this preference, Nightwatch setup will begin. It will also generate sample tests for you to get started.

If you are running from a Mac, safaridriver is present by default but must be enabled. You will be presented with the following option.

? Enable safaridriver (requires sudo password)? (Use arrow keys)
 ❯Yes
  No, I'll do that later.

Run a test

Once your setup is done, you can run tests with this command

Firefox
npx nightwatch tests/specs/basic/ecosia.js --env selenium.firefox
Chrome
npx nightwatch tests/specs/basic/ecosia.js --env selenium.chrome
Safari
npx nightwatch tests/specs/basic/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 <input[type=search]> is visible (51ms)
✔ Testing if element <button[type=submit]> 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.