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.

Run sample test in parallel

  1. Clone the nightwatch-browserstack sample repo on GitHub, if not already done:

    git clone https://github.com/browserstack/nightwatch-browserstack.git
    cd nightwatch-browserstack
    
  2. Install the dependencies using npm install
  3. Setup your credentials and browser/devices where you want to run your test, in the nightwatch-browserstack/conf/parallel.conf.js file as shown below:

    parallel.conf.js
    nightwatch_config = {
      src_folders : [ "tests/single" ],  // This test script will run across the multiple browser combinations given below
    
      // Configuring to use BrowserStack hub as the Selenium Hub
      selenium : {
        "start_process" : false,
        "host" : "hub-cloud.browserstack.com",
        "port" : 443,
        "proxy": "http://PROXY_USERNAME:PROXY_PASSWORD@proxy-host:proxy-port"  // If you are behind a proxy
      },  
    
      // These common capabilities will be same across all the environments where the test would run
      common_capabilities: {
        'build': 'nightwatch-browserstack',
        'browserstack.user': 'YOUR_USERNAME',
        'browserstack.key': 'YOUR_ACCESS_KEY',
        'browserstack.debug': true
      },
    
      // The following are the environments where your test would run.
      // You can configure additional capabilities specific to the environment using https://www.browserstack.com/automate/capabilities
      test_settings: {
        default: {},
        chrome: {
          desiredCapabilities: {
            browser: "chrome"
          }
        },
        firefox: {
          desiredCapabilities: {
            browser: "firefox"
          }
        },
        safari: {
          desiredCapabilities: {
            browser: "safari"
          }
        },
        ie: {
          desiredCapabilities: {
            browser: "internet explorer"
          }
        }
      }
    };
    
    // Code to support common capabilities
    for(var i in nightwatch_config.test_settings){
      var config = nightwatch_config.test_settings[i];
      config['selenium_host'] = nightwatch_config.selenium.host;
      config['selenium_port'] = nightwatch_config.selenium.port;
      config['desiredCapabilities'] = config['desiredCapabilities'] || {};
      for(var j in nightwatch_config.common_capabilities){
        config['desiredCapabilities'][j] = config['desiredCapabilities'][j] || nightwatch_config.common_capabilities[j];
      }
    }
    module.exports = nightwatch_config;
    
  4. Run your first test using the following command:
    ./node_modules/.bin/nightwatch -c conf/parallel.conf.js  -e chrome,firefox,safari,ie
    
  5. You can also run you test in other environments by specifying that in the test_settings variable as shown below:

    test_settings: {
        default: {},
        chrome: {
          desiredCapabilities: {
            "browserName": "chrome",
            "browser_version": "latest",
            "os": "Windows",
            "os_version": "10"
          }
        },
        iPhone: {
          desiredCapabilities: {
            "device": "iPhone 12 Pro",
            "browserName": "iPhone",
            "os_version": "14"
          }
        },
        safari: {
          desiredCapabilities: {
            "browser": "safari",
            "browser_version": "latest",
            "os": "OS X",
            "os_version": "Big Sur"
          }
        },
        samsung: {
          desiredCapabilities: {
            "device": "Samsung Galaxy S20 Ultra",
            "browser": "Android",
            "os_version": "10.0"
          }
        }
     }
    

    And you can invoke the parallel tests using the command below:

    ./node_modules/.bin/nightwatch -c conf/parallel.conf.js  -e chrome,iPhone,safari,samsung
    

You can visit BrowserStack Automate Dashboard and see your test there once it has successfully completed.

Note:
  • You can run your test on real mobile devices by declaring browserName: 'iPhone' or browserName: 'android' under the desiredCapabilities section.
  • Test scripts should usually be different for mobile devices because most websites render differently in mobile browsers
  • Generate a list of capabilities that you want to use in the desiredCapabilities section of parallel.conf.js file.

Note: Achieve your test coverage and build execution time goals by using our calculator to understand how many parallel sessions you need.

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

Is this page helping you?

Yes
No

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!

Talk to automation expert