Skip to main content
Introducing the Automate SDK! Get your entire test suite running on BrowserStack in minutes! Learn More.
No Result Found

Run Cucumber Tests in Parallel

Run automated tests on multiple browsers in parallel with Cucumber Java using BrowserStack Automate.

Note: Sample test scripts are available in the cucumber-java-browserstack repository.

The sample test script in this section is compatible with JSON wire protocol-based client bindings. Check out our W3C-based scripts in the selenium-4 branch of the repository.

Introduction

On BrowserStack, you can run multiple Cucumber 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, namely cross-browser testing, or run different tests on the same or different browser/device combinations. Parallel Testing helps you reduce the run time of your test suite, resulting in faster build times and faster releases.

This guide will help you:

  1. Run your first parallel test
  2. Understand your test runner

Prerequisites

If you have already run your first test, you can skip the prerequisites.

  • BrowserStack Username and Access key, which you can find in your account settings. If you have not created an account yet, you can sign up for a Free Trial or purchase a plan.
  • Maven is installed on your machine, Maven environment variables are set, and Maven bin is added to system path, $PATH. Check out the official website to download the latest version of Maven.
  • Git installed on your machine.
  • Java installed on your machine.

Run your first parallel test

  1. Clone the cucumber-java-browserstack sample repo on GitHub using the following command:

    git clone https://github.com/browserstack/cucumber-java-browserstack.git
    cd cucumber-java-browserstack
    
  2. Install the required dependencies by running the following command in your command-line:

     mvn install
    
  3. Set your BrowserStack credentials and browser/devices where you want to run your test in the cucumber-java-browserstack/src/test/resources/conf/parallel.conf.json file as follows:

    parallel.conf.json
     {
       "server": "hub.browserstack.com",
       "user": "YOUR_USERNAME",
       "key": "YOUR_ACCESS_KEY",
    
       "capabilities": {
         "build": "cucumber-java-browserstack",
         "name": "parallel_test",
         "browserstack.debug": true
       },
    
       "environments": [
         {
           "browser": "chrome"
         },
         {
           "browser": "firefox"
         },
         {
           "browser": "safari"
         }
       ]
     }
    
  4. Run your tests in parallel on BrowserStack using the following command:

     mvn test -P parallel
    
  5. View your tests on BrowserStack on the BrowserStack Automate Dashboard.
Protip: You can use our capability builder and select from a wide range of custom capabilities that BrowserStack supports.

Understand your test runner

The RunWebDriverCucumberTests.java file in the cucumber-java-browserstack/src/test/java/com/browserstack directory includes the logic that helps run test in parallel on BrowserStack.

RunWebDriverCucumberTests.java

@CucumberOptions(
    glue = "com.browserstack.stepdefs",
    plugin = {
      "pretty",
      "html:reports/tests/cucumber/html",
      "timeline:reports/tests/cucumber/timeline",
      "junit:reports/tests/cucumber/junit/cucumber.xml",
      "testng:reports/tests/cucumber/testng/cucumber.xml",
      "json:reports/tests/cucumber/json/cucumber.json"
    }
)
public class RunWebDriverCucumberTests {

    private TestNGCucumberRunner testNGCucumberRunner;
    private Local local;
    private static final ThreadLocal<ManagedWebDriver> threadLocalWebDriver = new ThreadLocal<>();

    @BeforeClass(alwaysRun = true)
    public void setUpClass() {
        testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
    }

    private synchronized static void setThreadLocalWebDriver(ManagedWebDriver managedWebDriver) {
        threadLocalWebDriver.set(managedWebDriver);
    }

    public synchronized static ManagedWebDriver getManagedWebDriver() {
        return threadLocalWebDriver.get();
    }

    @Test(groups = "cucumber", description = "Runs Cucumber Feature", dataProvider = "scenarios")
    public void feature(PickleWrapper pickleWrapper, FeatureWrapper featureWrapper, ManagedWebDriver managedWebDriver) {
        if(Utility.isLocal(managedWebDriver) && local==null){
            local = new Local();
            Utility.startLocal(local, managedWebDriver);
        }
        managedWebDriver.setTestName(pickleWrapper.getPickle().getName());
        setThreadLocalWebDriver(managedWebDriver);
        testNGCucumberRunner.runScenario(pickleWrapper.getPickle());
    }

    @DataProvider(name = "scenarios", parallel = true)
    public Iterator<Object[]> scenarios() {
        Object[][] scenarios = testNGCucumberRunner.provideScenarios();
        //Get Iterator of Object arrays consisting PickleWrapper, FeatureWrapper and ManagedWebDriver
        return new LazyInitWebDriverIterator(scenarios);
    }

    @AfterClass(alwaysRun = true)
    public void tearDownClass() {
        if(local != null){
            try {
                local.stop();
            } catch (Exception e) {
                throw new Error("Unable to stop BrowserStack Local.");
            }
        }
        if (testNGCucumberRunner == null) {
            return;
        }
        testNGCucumberRunner.finish();
    }
}

Next steps

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

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
Download Copy Check Circle