Run Nightwatch Tests in Parallel
On BrowserStack, you can run multiple Nightwatch tests at the same time across various browser, device and OS combinations. This is called Parallel Testing. Parallel Testing gives you the same benefits as running a multi-threaded application.
With Parallel Testing, you can run the same test on different browser/device combinations i.e. cross-browser testing, or run different tests on the same or different browser/device combinations. Parallel Testing will help you reduce the run time of your test suite, resulting in faster build times and faster releases.
This guide will help you:
- Run sample test in parallel
- Understand your parallel test
- Understand the Nightwatch configuration file
- Understand the command to run parallel tests
- Run test on mobile devices
Prerequisites
- BrowserStack Username and Access key, which you can find in your account settings. If you have not created an account yet, you can sign up for a Free Trial or purchase a plan.
- Node.js installed on your machine.
- Git installed on your machine.
Run sample test in parallel
- Clone the nightwatch-browserstack sample project, available on GitHub, if not already done:
git clone https://github.com/browserstack/nightwatch-browserstack.git
- Open the cloned project in IDE of your choice and install the required dependencies by running the following command in your command-line::
npm install
- Set your BrowserStack credentials in the project by running the following command:
export BROWSERSTACK_USER=YOUR_USERNAME && export BROWSERSTACK_KEY=YOUR_ACCESS_KEY
Note: It is recommended that the BrowserStack credentials are added to the env variables to use these environment variables every time you run this project.
- Run a parallel test by running the following command:
npm run parallel
This command generates a
nightwatch.conf.js
file in your project and runs your tests on multiple browsers such as Chrome, Safari, and Firefox, and Internet Explorer. - View your tests on BrowserStack by visiting your Automate Dashboard.
- Run your test on real mobile devices by adding the
device
,realMobile
, andos_version
capabilities under thedesiredCapabilities
section in thenightwatch.conf.js
file. Refer to the Run test on mobile devices section to learn more. - Achieve your test coverage and build execution time goals by using our calculator to understand how many parallel sessions you need.
Understand your parallel test
When you run the npm run parallel
command, the single_test.js
file within the nightwatch-browserstack/tests/single/
directory is executed on four different browsers. This test script searches for the string BrowserStack
on google.com, and verifies if the title of the resulting page is BrowserStack - Google Search
:
module.exports = {
"Google's Search Functionality": function (browser) {
browser
.url("https://www.google.com")
.setValue("input[name=q]", ["BrowserStack", browser.Keys.ENTER]) // this submits on desktop browsers
.pause(1000)
.title((result) => {
if (!/BrowserStack/i.test(result.value)) {
browser.submitForm("input[name=q]"); // this helps in mobile browsers
}
}).expect.title().to.match(/BrowserStack/i);
browser.end();
},
};
Understand the Nightwatch configuration file
The following configuration file is auto-generated after you run the npm run parallel
command. It contains a set of capabilities that are preloaded.
As seen in the configuration file given below, there are preloaded capabilities for every browser:
-
browserstack.chrome
: Capability set to run BrowserStack test on Chrome. -
browserstack.firefox
: Capability set to run BrowserStack test on Firefox. -
browserstack.safari
: Capability set to run BrowserStack test on Safari. -
browserstack.ie
: Capability set to run BrowserStack test on Internet Explorer.
Within these capabilities set, as there are no desktop machines or mobile devices defined, the test runs on default desktop machines. Tests defined to run on the Chrome, Firefox, and Internet Explorer browsers choose a Windows OS machine, while tests defined to run on the Safari browser choose a macOS machine.
browserstack: {
selenium: {
// setting the host name
host: 'hub-cloud.browserstack.com',
port: 443
},
// More info on configuring capabilities can be found on:
// https://www.browserstack.com/automate/capabilities?tag=selenium-4
desiredCapabilities: {
'bstack:options' : {
// sets BrowserStack's credentials via environment variables
userName: '${BROWSERSTACK_USER}',
accessKey: '${BROWSERSTACK_KEY}',
}
},
// other capabilities
// capabilities to run test on Chrome
'browserstack.chrome': {
extends: 'browserstack',
desiredCapabilities: {
browserName: 'chrome',
// add other capabilities you want
chromeOptions : {
w3c: false
}
}
},
// capabilities to run test on Firefox
'browserstack.firefox': {
extends: 'browserstack',
desiredCapabilities: {
browserName: 'firefox'
// add other capabilities you want
}
},
// capabilities to run test on Internet Explorer
'browserstack.ie': {
extends: 'browserstack',
desiredCapabilities: {
browserName: 'internet explorer',
browserVersion: '11.0'
// add other capabilities you want
}
},
// capabilities to run test on Safari
'browserstack.safari': {
extends: 'browserstack',
desiredCapabilities: {
browserName: 'safari',
// add other capabilities you want
}
},
// ...
Understand the command to run parallel tests
The test file and capabilities to consider for running parallel tests is specified in the package.json
file, as shown below:
"scripts": {
...
"parallel": "nightwatch --test ./tests/single/single_test.js --env browserstack.chrome,browserstack.ie,browserstack.safari,browserstack.firefox",
...
},
This statement instructs the npm run parallel
command to run the single_test.js
file by using capabilities specified within browserstack.chrome
, browserstack.ie
, browserstack.safari
, and browserstack.firefox
sections of the nightwatch.conf.js
file for running parallel tests on Chrome, Internet Explorer, Safari, and Firefox respectively.
Run test on mobile devices
The auto-generation nightwatch.conf.js
configuration file is preloaded with capabilities for running tests on desktop machines only. To run tests on mobile devices, complete the following steps:
Update the configuration file
In the nightwatch.conf.js
file, add the following code snippet where other environments such as browserstack.chrome
or browserstack.ie
are defined.
You can update the browserName
, device
, and os_version
capabilities to run your test on any other Android or iOS devices.
//other environments
// environment to run tests on Android devices
'browserstack.android': {
extends: 'browserstack',
desiredCapabilities: {
os_version : '10.0',
device : 'Samsung Galaxy S20',
real_mobile : 'true',
browserName: 'chrome',
chromeOptions : {
w3c: false
}
}
},
// environment to run tests on iOS devices
'browserstack.ios': {
extends: 'browserstack',
desiredCapabilities: {
os_version : '14',
device : 'iPhone 12',
real_mobile : 'true',
browserName: 'safari',
chromeOptions : {
w3c: false
}
}
},
// other environments
'browserstack.firefox': {
//...
Update the command to run test
In the package.json
file, update the parallel
command by setting the environment to browserstack.android
and browserstack.ios
for running tests on Android and iOS devices respectively, as shown below:
"scripts": {
...
"parallel": "nightwatch --test ./tests/single/single_test.js --env browserstack.android, browserstack.ios",
...
},
Next steps
After you have successfully run your local test on BrowserStack, you might want to do the following:
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
Thank you for your valuable feedback!