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');
async function runTestWithCaps (capabilities) {
  let driver = new webdriver.Builder()
    .usingServer('http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub')
    .withCapabilities(capabilities)
    .build();
  await driver.get("http://www.google.com");
  const inputField = await driver.findElement(webdriver.By.name("q"));
  await inputField.sendKeys("BrowserStack", webdriver.Key.ENTER); // this submits on desktop browsers
  try {
    await driver.wait(webdriver.until.titleMatches(/BrowserStack/i), 5000);
  } catch (e) {
    await inputField.submit(); // this helps in mobile browsers
  }
  try {
    await driver.wait(webdriver.until.titleMatches(/BrowserStack/i), 5000);
    console.log(await driver.getTitle());
    await driver.executeScript(
      'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "Title contains BrowserStack!"}}'
    );
  } catch (e) {
    await driver.executeScript(
      'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "Page could not load in time"}}'
    );
  }
  await driver.quit();
}
const capabilities1 = {
  'browserName': 'chrome',
  'browser_version': 'latest',
  'os': 'Windows',
  'os_version': '10',
  'build': 'BStack-[NodeJS] Sample Build',
  'name': 'Parallel test 1'
}
const capabilities2 = {
	'browserName': 'firefox',
  'browser_version': 'latest-beta',
  'os': 'Windows',
  'os_version': '10',
  'build': 'BStack-[NodeJS] Sample Build',
  'name': 'Parallel test 2'
}
const capabilities3 = {
	'device': 'iPhone 12 Pro',
  'browserName': 'iPhone',
  'os_version': '14',
  'real_mobile': 'true',
  'build': 'BStack-[NodeJS] Sample Build',
  'name': 'Parallel test 3'
}
const capabilities4 = {
	'device': 'Samsung Galaxy S20',
  'browserName': 'Android',
  'os_version': '11.0',
  'real_mobile': 'true',
  'build': 'BStack-[NodeJS] Sample Build',
  'name': 'Parallel test 4'
}
const capabilities5 = {
	'browserName': 'Safari',
  'browser_version': 'latest',
  'os': 'OS X',
  'os_version': 'Big Sur',
  'build': 'BStack-[NodeJS] Sample Build',
  'name': 'Parallel test 5'
}
// The following code invokes the test function 5 times in parallel with separate capabilities as defined above
runTestWithCaps(capabilities1);
runTestWithCaps(capabilities2);
runTestWithCaps(capabilities3);
runTestWithCaps(capabilities4);
runTestWithCaps(capabilities5);

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 utilise 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.

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