If you already have Node.js installed and a few browsers installed and you're eager to get right down to it, we have just the thing you need. As this short tutorial will show you, it doesn't take more than 2 to 10 minutes to be up and running with Nightwatch.

This guide starts on the premise that you are not currently using Nightwatch in your project. If you do, please rename your config file so a new fresh one will be created.

Step 0: create your new project

Let's create a new empty project where we'll install Nightwatch.

mkdir nightwatch-test-project
cd nightwatch-test-project
npm init -y

1. Install Nightwatch from NPM

Nightwatch requires Node.js 10 or higher and the NPM tool installed on your machine. This will install Nightwatch as a dependency and add the nightwatch executable in your project.

npm install nightwatch --save-dev

2. Install ChromeDriver and GeckoDriver from NPM

To be able to run end-to-end tests in Chrome and Firefox on your local machine, we need these two additional packages installed in your project.

npm install geckodriver chromedriver --save-dev
MacOS users

If you'd wish to run tests against Safari, you need to run this one-time CLI command in order to enable automation via WebDriver on your local machine.

safaridriver --enable

3. Run a sample test

Nightwatch comes with a few bundled example tests which you can use to quickly see it in action. Once you have completed the previous steps, you can now see some actual results.

The below commands runs an example test which 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".

By default, Nightwatch will start Firefox. Use the bundled npx tool from NPM to quickly run the nightwatch command:

npx nightwatch node_modules/nightwatch/examples/tests/ecosia.js

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)
Running in Chrome

To run the same test in the Chrome browser, run the same command and pass the --env chrome argument:

npx nightwatch node_modules/nightwatch/examples/tests/ecosia.js --env chrome
Running in Safari

If you're using a Mac, you can run the the tests in Safari as well, simply pass the --env safari argument:

npx nightwatch node_modules/nightwatch/examples/tests/ecosia.js --env safari
Running in Chrome, Firefox, and Safari in parallel

You can also run in all available browsers in parallel using one command. You only have to separate the environments by a comma (","), no spaces.

npx nightwatch node_modules/nightwatch/examples/tests/ecosia.js --env firefox,chrome,safari

That's about it for now. Hope you head a great first experience with Nightwatch. Let us know your thoughts on Twitter or you can chat with us on Gitter.

Next Steps:

Improve this article