Create and run a test with Selenium Server
In this quickstart, you will learn how to create and run a Nightwatch test using Selenium Server
Prerequisites
- Make sure Node is installed on the system. The version used for this tutorial is v16.14.2
- Make sure Java is installed on the system. The version used for this tutorial is openjdk 11.0.14
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
Depending on your target browsers (make sure you have the browser installed and updated), you will need one or more specific WebDriver packages. Dependenging on you browser run one of the following:
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: install Selenium Server
java -version
from the command line.The easiest way to install the Selenium Server is from NPM using the @nightwatch/selenium-server
package which is maintained by the Nightwatch team. Nightwatch automatic configuration is already prepared for usage with this package against Chrome, Firefox, and Internet Explorer.
npm install @nightwatch/selenium-server
Step 3: write a test
Create a new folder called tests
inside nightwatch-project
folder.
mkdir tests
Then create a file called ecosia.js
inside the tests folder and put the following code in that file:
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 4: run the test
Use the bundled npx
tool from NPM to quickly run the nightwatch
command:
Firefox
npx nightwatch tests/ecosia.js --env selenium.firefox
Chrome
npx nightwatch tests/ecosia.js --env selenium.chrome
Safari
npx nightwatch tests/ecosia.js --env selenium.safari
The output should look similar to this:
Improve this article