In this article, we will explore how we can execute test cases simultaneously on multiple browsers. Parallel execution is an inbuilt feature in Nightwatch and we don’t require any external plugins/libraries to achieve this.
We will be executing, TC001_WikiSearch.js in Chrome, Firefox, and Safari browsers. You can add as many browsers you like.
To achieve parallel execution, we need to make changes into the nightwatch.conf.js file. In the first article, we configured the tests for chrome and it currently looks like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | module.exports = { src_folders: ["tests"], webdriver: { start_process: true, port: 4444, server_path: require('chromedriver').path, cli_args: [ ] }, test_settings: { default: { launch_url: 'https://nightwatchjs.org', desiredCapabilities: { browserName: 'chrome', } } } }; |
There are specifically three changes that we will be making to the nightwatch.conf.js file:
- Adding the configuration for Firefox.
1
2
3
4
5
6
7
8
9
10firefox: {
desiredCapabilities: {
browserName: 'firefox'
},
webdriver: {
start_process: true,
port: 4446,
server_path: require('geckodriver').path
}
} - Adding the configuration for Safari.
1
2
3
4
5
6
7
8
9
10
11
12
13safari: {
desiredCapabilities: {
browserName: 'safari',
alwaysMatch: {
acceptInsecureCerts: false
}
},
webdriver: {
port: 4445,
start_process: true,
server_path: '/usr/bin/safaridriver'
}
} - The third and the most important is the test_Worker configuration, which allows the tests to be run in parallel. When this is enabled the test runner will launch a configurable number of child processes and then distribute the loaded tests over to run in parallel.
The workers option configures how many child processes can run concurrently.
a) “auto” – determined by the number of CPUs e.g. 4 CPUs means 4 workers
b) {number} – specifies an exact number of workers
1
2
3
4test_workers: {
enabled: true,
workers: 'auto'
}
After making all the changes, the nightwatch.conf.js file should look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | { module.exports = { src_folders: ["tests"], test_settings: { default: { desiredCapabilities: { browserName: 'chrome' }, webdriver: { start_process: true, port: 4444, server_path: require('chromedriver').path, } }, test_workers: { enabled: true, workers: 'auto' }, safari: { desiredCapabilities: { browserName: 'safari', alwaysMatch: { acceptInsecureCerts: false } }, webdriver: { port: 4445, start_process: true, server_path: '/usr/bin/safaridriver' } }, firefox: { desiredCapabilities: { browserName: 'firefox' }, webdriver: { start_process: true, port: 4446, server_path: require('geckodriver').path } } } } |
For Test Execution, we will use the command:
1 | npx nightwatch tests/TC001_WikiSearch.js -e default,firefox,safari |
Here we are using the keyword ‘default’ instead of chrome because the chrome configurations are written inside default.
Upon successful Execution, the terminal should look like this:
Do check out 🙂
Github:Â https://github.com/alapanme/NightwatchJS
All Nightwatch JS Articles: https://testersdock.com/nightwatch-js-tutorial/
Hi,
So let’s say you’ve 10 tests (non-dependents on each other) and and each test takes ~1 minute to run – so the 10 tests take 10 minutes to run (just as example).
So in this example, you’ve executed all 10 tests, three times in parallel – one full set on each worker/browser. Meaning you’ve executed 30 tests (total) and instead taking ~30 minutes, you did it in ~10 minutes. I got it.
But, what if I want to run the same 10 tests – just a single time each, in parallel in the same browser environment using 3 workers. My goal was to reduce the time spent in the full set of tests from ~10 minutes into ~3.5~4 minutes.
Is that possible? NW does support such feature? I was not able to find any example for my case.
Hey Jean,
Thanks for asking this question, because I haven’t tried this scenario before. But on googling a bit I found this – https://github.com/nightwatchjs/nightwatch-docs/blob/master/guide/running-tests/run-parallel.md
So Nightwatch also has workers, which is set to ‘auto’ (in Nightwatch.conf.js fie)for our article, but we can set it to any number depending on the number of cores your machine has and it will run the tests parallelly for one browser.
do you have any code for refernce on git
https://github.com/alapanme/NightwatchJs
I’m curious tо find out what blog platform you’re utilizing?
I’m having some minor security issues with my latest webÑ•ite and I would
lікe tߋ find something more secure. Do you have any rеcommendations?
Currently, I am using WordPress, but I am planning to move to static site generators like Hugo, Jekyll, etc in the near future.