Using Selenium Server with Nightwatch
Nightwatch can also be used for distributed cross-browser end-to-end testing at scale together with the Selenium Server (also known as Selenium Grid). Selenium Server is an open-source project written in Java that manages a network of WebDriver nodes.
It aims to provide an easy way to run tests in parallel on multiple machines.
Prerequisites
- Make sure Node is installed on the system. The version used for this guide is v16.14.2
- Make sure Java is installed on the system. The version used for this guide is openjdk 11.0.14
Setup Nightwatch
Create a new directory and initiate a Nightwatch project there:
or directly create the project in a new directory with a single command:
npm init nightwatch <directory-name>
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.
Check the Local selenium-server
option from the list on top of the selected browsers.
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
Firefox
npx nightwatch tests/specs/basic/ecosia.js --env selenium.firefox
Chrome
npx nightwatch tests/specs/basic/ecosia.js --env selenium.chrome
Safari
npx nightwatch tests/specs/basic/ecosia.js --env selenium.safari
The output should look similar to this: