Install Nightwatch
Getting started with Nightwatch takes only a few short minutes. You can perform the following types of testing with Nightwatch
- End to end testing of web apps on desktop & mobile browsers
- Component testing with top frameworks such as React, Vue, Storybook & Angular
- Mobile app testing on Android & iOS
- API testing
- Visual regression testing (VRT)
- Accessibility testing
For all types of testing, you have to start by installing Nightwatch itself. Let’s begin!
Prerequisites
Make sure Node is installed on the system.
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
Answer the simple set of questions to complete the Nightwatch installation. In case you need more info around these questions please refer to the guide below. Otherwise please skip to Run your end to end tests section post completion of installation.
Preferences
Testing Types
This will setup Nightwatch along with all the required dependencies for the type of testing selected. Nightwatch base version will be installed irrespective of the choice.
Test runner & Language
Nightwatch also supports other test runners. You can pick Mocha or Cucumber JS as a test runner apart from Nightwatch.
Nightwatch supports TypeScript for test files after v1.6.0. So you can choose to have the test setup in Javascript or Typescript.
Browser Selection
You can pick the browsers you'll be testing on, and the config will be automatically created for them.
Test Folder
Next you can name the folder where you want the tests to reside. The default value is tests.
Base URL
This is a very important configuration that should be used as a variable in your tests so that you can switch between different testing environment & URLs with a simple config change. This preference will default to http://localhost
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.
Anonymous Metrics
Allow Nightwatch to collect anonymous metrics. The preference will default to no
as we respect our user’s privacy.
Run tests on Mobile web
Nightwatch supports running of tests on real and virtual mobile devices. Nightwatch will also take care of the setup of all underlying SDK, libraries & virtual devices.
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 your first end-to-end test
Once your setup is done, you can run example tests with this command
npx nightwatch ./nightwatch/examples
The output should look similar to this:
View the report
Simply, copy-paste the HTML path at the end of the output in your browser address bar to view the report
Congratulations! You have installed Nightwatch, run a test & viewed the HTML report.
Recommended Next Steps
Now that you have installed Nightwatch