Create and run a Nightwatch test
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
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:
Press y
when you see the prompt to install create-nightwatch
This installs Nightwatch, asks your preferences and sets up the nightwatch.conf.js
file based on your preferences as shown below
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.
Run on Local/Remote (Cloud)
You can configure Nightwatch to run locally on your machine, remotely on a cloud machine or 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.
Test Folder Name
Next you can name the folder where you want the tests to reside. The default value is tests.
Base URL
Add the base URL that the tests will run against. This preference will default to 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.
Run a test
Once your setup is done, you can run tests with this command
npx nightwatch tests/ecosia.js --env safari
The output should look similar to this:
For additional help you can jump on to our Discord Server.
Improve this article