Skip to main content
Looking to test on every commit or PR? Introducing plans for high-scale testing on Chrome and Firefox. Read more

Run Tests in Parallel

Run Node.js tests in parallel on BrowserStack to achieve faster build durations

On BrowserStack, you can run multiple Selenium Webdriver 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.

You can start testing in parallel using one of the popular test frameworks which work with Node.js and Selenium or refer the section for running tests in parallel without any framework in particular. We have provided getting started guides on some of the popular frameworks below:

Run tests in parallel without a framework

Testing frameworks make it easier to run and manage tests in parallel. But that does not mean you cannot achieve test parallelisation without using a test framework. This section will guide you to run cross-browser tests in BrowserStack using plain Node.js without using any test framework.

Here are 2 ways to achieve the same but this is not an exhaustive list:

  1. Using a single script to run the same test across multiple OS/browser combinations
  2. Multiple scripts run in parallel using a shell script

Single test script to execute across multiple combinations

The following sample Node.js script has one async function which takes in capabilities as a parameter and runs the test case on the environment based on the passed capabilities:

sample-parallel-test.js
  const webdriver = require('selenium-webdriver');
  const { By } = require('selenium-webdriver');
  const assert = require('assert');

  async function runTestWithCaps (capabilities) {
    let driver = new webdriver.Builder().usingServer(`https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub.browserstack.com/wd/hub`).withCapabilities(capabilities).build();
    try{
      await driver.get("https://bstackdemo.com/");
      await driver.wait(webdriver.until.titleMatches(/StackDemo/i), 10000);

      // locating product on webpage
      const productOnScreen = await driver.wait(webdriver.until.elementLocated(By.xpath('//*[@id="1"]/p')), 10000)
      // getting name of the product when the product is visible
      const productText =  await driver.wait(webdriver.until.elementIsVisible(productOnScreen, 10000)).getText();
      // clicking the 'Add to cart' button
      await driver.wait(webdriver.until.elementIsVisible(driver.findElement(By.xpath('//*[@id="1"]/div[4]'), 10000))).click();
      // waiting until the Cart pane has been displayed on the webpage
      await driver.wait(webdriver.until.elementIsVisible(driver.findElement(By.className('float-cart__content'), 10000)));
      // locating product in cart
      const productInCart = await driver.wait(webdriver.until.elementLocated(By.xpath('//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]')), 10000);
      // getting name of the product in cart if the product is visible on web page
      const productCartText =  await driver.wait(webdriver.until.elementIsVisible(productInCart, 10000)).getText();
      // checking whether product has been added to cart by comparing product name
      assert(productText === productCartText);
      //marking the test as Passed if product has been added to the cart
      await driver.executeScript(
        'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "Product has been successfully added to the cart!"}}'
      );
    } catch(e) {
      //marking the test as Failed if product has not been added to the cart
      await driver.executeScript(
        'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "Some elements failed to load"}}'
      );
    }
    await driver.quit();
  }

  // input capabilities
  const capabilities = [{
  'os_version': '10',
  'browserName': 'Chrome',
  'browser_version': 'latest',
  'os': 'Windows',
  'build': 'BStack-[NodeJS] Sample Build',
  'name': 'Parallel test 1'
  },
  {
    'os_version': 'Monterey',
    'browserName': 'Chrome',
    'browser_version': 'latest',
    'os': 'OS X',
    'build': 'BStack-[NodeJS] Sample Build',
    'name': 'Parallel test 2'
  },
  {
    'os_version' : 'Big Sur',
    'browserName' : 'Safari',
    'os' : 'OS X',
    'build': 'BStack-[NodeJS] Sample Build',
    'name': 'Parallel test 3'
  },
  {
    'browserName': 'Android',
    'device': 'Samsung Galaxy S20',
    'realMobile': 'true',
    'build': 'BStack-[NodeJS] Sample Build',
    'name': 'Parallel test 4'
  },
  {
    'browserName': 'iPhone',
    'device': 'iPhone 12 Pro Max',
    'realMobile': 'true',
    'build': 'BStack-[NodeJS] Sample Build',
    'name': 'Parallel test 5'
  }];

  // The following code runs the test function as many times the capabilities are defined
  capabilities.map(async (caps) => {
    await runTestWithCaps(caps);
    });

The sample script shown above, runs the same test across 5 different OS/browser combinations in parallel. Alternatively, you can also run different test scripts across same or different OS/browser combinations using the same approach.

Run the sample script for parallel testing

Run the following command to run the script shown above:

Command Line
node sample-parallel-test.js
Protip: You can also use:

Run tests in parallel using a shell script

You can also write separate *.js scripts for each test that you wish to run. Then you can write a shell script that will invoke the scripts in parallel in the way you want. A sample shell script is shown below:

Shell script to execute 5 test scripts in parallel
# All 5 scripts will be run in parallel in different processes
node test_script_1.js &
node test_script_2.js &
node test_script_3.js &
node test_script_4.js &
node test_script_5.js &

Conclusion

If you are using one of the Node.js based test frameworks, then it is best to utilize the functionality of the framework to implement cross-browser testing or testing in parallel, in general. The first section of the document gives links to documentation for different popular frameworks.

And, if you are not using any framework, even then you can run tests in parallel. 2 of the techniques are shown above but it is not an exhaustive list. You may incorporate any method you like, to run your tests in parallel across the several device/browser combinations that are available on BrowserStack.

Next steps

After you have successfully run your first parallel test using BrowserStack, you can explore the following section:

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 an Expert
Talk to an Expert