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:

  1. Adding the configuration for Firefox.
     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
            firefox: {
                desiredCapabilities: {
                    browserName: 'firefox'
                },
                webdriver: {
                    start_process: true,
                    port: 4446,
                    server_path: require('geckodriver').path
                }
            }
  2. Adding the configuration for Safari.
     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
            safari: {
                desiredCapabilities: {
                    browserName: 'safari',
                    alwaysMatch: {
                        acceptInsecureCerts: false
                    }
                },
                webdriver: {
                    port: 4445,
                    start_process: true,
                    server_path: '/usr/bin/safaridriver'
                }
            }
  3. 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
    4
            test_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:

Nightwatch js parallel execution terminal

Do check out 🙂

Github: https://github.com/alapanme/NightwatchJS
All Nightwatch JS Articles: https://testersdock.com/nightwatch-js-tutorial/