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
Step 0: create a new project
Create a new directory and initiate a node project there.
mkdir nightwatch-project
cd nightwatch-project
npm init
Step 1: install Nightwatch and dependencies
Install the latest version of Nightwatch:
npm install nightwatch --save-dev
Browser Drivers
You need one or more WebDriver packages installed depending on which browsers you want to target (make sure you have the browser installed and updated). Run one of the following commands to install:
Firefox
npm install geckodriver --save-dev
Chrome
npm install chromedriver --save-dev
Microsoft Edge Driver
Follow the Download Microsoft Edge Driver section on the official Microsoft Edge documentation page to download the Edge Driver.
SafariDriver
The safaridriver
binary is already installed on recent versions of Mac OS, however some manual configuration is needed before tests can be run against Safari.
You will need to run the following once, before using the safaridriver
:
safaridriver --enable
Step 2: write a test
Create a new folder called tests
inside nightwatch-project
folder.
mkdir tests
Then create a new file called ecosia.js
inside the tests folder and add the following code:
describe('Ecosia.org Demo', function() {
before(browser => browser.navigateTo('https://www.ecosia.org/'));
it('Demo test ecosia.org', function(browser) {
browser
.waitForElementVisible('body')
.assert.titleContains('Ecosia')
.assert.visible('input[type=search]')
.setValue('input[type=search]', 'nightwatch')
.assert.visible('button[type=submit]')
.click('button[type=submit]')
.assert.textContains('.layout__content', 'Nightwatch.js');
});
after(browser => browser.end());
});
Step 3: run the test
Use the bundled npx
tool from NPM to quickly run the nightwatch
command:
Firefox
npx nightwatch tests/ecosia.js --env firefox
Chrome
npx nightwatch tests/ecosia.js --env chrome
Safari
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