Getting started with Nightwatch takes only a few short minutes. Here's how to be up and running with your first end-to-end test.

Prerequisites

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

Setup Nightwatch

Nightwatch can be installed with just one command line, either as a new project or in an existing location.

1. As a new project:

To setup Nightwatch as a new project, simply run:

npm init nightwatch <directory-name>
2. In an existing project:
cd <directory-name> 
npm init nightwatch


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

Test Folder

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 your first end-to-end test

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

npx nightwatch tests/specs/basic/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)

Use the Google Chrome DevTools Recorder

Alternatively, Nightwatch provides tools to help you get started by recording your test actions on screen and generate Nightwatch test scripts automatically. See Create Nightwatch test using Google Chrome DevTools Recorder for more information.

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