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 Java 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 Java 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

You can achieve parallel testing in multiple ways even if you are not using one of the popular test frameworks which has feature for enabling parallel testing. We show 3 such ways below but this is not an exhaustive list:

  1. Using a multi-threaded Java program (manually managing threads)
  2. Using a multi-threaded program and using an ExecutorService to manage threads
  3. Writing a shell script to invoke multiple Java programs in parallel

Parallel tests using a multi-threaded program

The following sample script shows a multi-threaded Java program. These are the salient points of the script:

  • The same script is run across 3 different browser/device combinations viz. iPhone 12 Pro, Samsung Galaxy S20, and Safari on Big Sur.
  • The capabilities are being populated in a HashTable and that is being passed on to the test function.
  • The test function has been written as a separate method and it takes care of the starting of the test for each of the browsers after iterating on the HashTable that is passed on to it.
  • Multi-threading has been implemented by defining multiple classes, all of which implements the Runnable Java class.
  • The main class contains the main() method and all the 3 threads are invoked from the main class.
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
class TestClass1 implements Runnable {
	public void run() {
		Hashtable<String, String> capsHashtable = new Hashtable<String, String>();
		capsHashtable.put("device", "iPhone 12 Pro");
		capsHashtable.put("real_mobile", "true");
		capsHashtable.put("os_version", "14");
    capsHashtable.put("build", "BStack-[Java] Sample Build");
		capsHashtable.put("name", "Thread 1");
		mainTestClass r1 = new mainTestClass();
		r1.executeTest(capsHashtable);
  }
}
class TestClass2 implements Runnable {
  public void run() {
		Hashtable<String, String> capsHashtable = new Hashtable<String, String>();
		capsHashtable.put("device", "Samsung Galaxy S20");
		capsHashtable.put("real_mobile", "true");
		capsHashtable.put("os_version", "11.0");
		capsHashtable.put("build", "BStack-[Java] Sample Build");
		capsHashtable.put("name", "Thread 2");
		mainTestClass r1 = new mainTestClass();
    r1.executeTest(capsHashtable);
  }
}
class TestClass3 implements Runnable {
	public void run() {
		Hashtable<String, String> capsHashtable = new Hashtable<String, String>();
		capsHashtable.put("browser", "safari");
		capsHashtable.put("browser_version", "14");
		capsHashtable.put("os", "OS X");
		capsHashtable.put("os_version", "Big Sur");
		capsHashtable.put("build", "BStack-[Java] Sample Build");
		capsHashtable.put("name", "Thread 3");
		mainTestClass r1 = new mainTestClass();
    r1.executeTest(capsHashtable);
  }
}
public class mainTestClass {
  public static final String USERNAME = "YOUR_USERNAME";
  public static final String AUTOMATE_KEY = "YOUR_ACCESS_KEY";
  public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";
  public static void main(String[] args) throws Exception {
	  Thread object1 = new Thread(new TestClass1());
    object1.start();
    Thread object2 = new Thread(new TestClass2());
    object2.start();
    Thread object3 = new Thread(new TestClass3());
    object3.start();
  }
	public void executeTest(Hashtable<String, String> capsHashtable) {
		String key;
	  DesiredCapabilities caps = new DesiredCapabilities();
		// Iterate over the hashtable and set the capabilities
		Set<String> keys = capsHashtable.keySet();
    Iterator<String> itr = keys.iterator();
    while (itr.hasNext()) {
       key = itr.next();
       caps.setCapability(key, capsHashtable.get(key));
    }
    WebDriver driver;
		try {
			driver = new RemoteWebDriver(new URL(URL), caps);
	    JavascriptExecutor jse = (JavascriptExecutor)driver;
	    // Searching for 'BrowserStack' on google.com
	    driver.get("https://www.google.com");
	    WebElement element = driver.findElement(By.name("q"));
	    element.sendKeys("BrowserStack");
	    element.submit();
			// Setting the status of test as 'passed' or 'failed' based on the condition; if title of the web page contains 'BrowserStack'
			WebDriverWait wait = new WebDriverWait(driver, 5);
	    try {
	    	wait.until(ExpectedConditions.titleContains("BrowserStack"));
	    	jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"passed\", \"reason\": \"Title matched!\"}}");
	    }
	    catch(Exception e) {
	    	jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \"Title not matched\"}}");
	    }
	    System.out.println(driver.getTitle());
	    driver.quit();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
}

Parallel tests using a multi-threaded program and ExecutorService

In case of the above implementation, you would have to manually keep track of which thread has completed its execution and depending on that, you will be required to spawn new threads keeping in mind you do not exceed the limit of parallel tests that is supported by your BrowserStack account.

You can choose to not manage the threads manually and use the Java ExecutorService to do the same for you.

Suppose you have a 2 parallel account and you have 20 tests to be run. You can create 20 different methods (similar to the 4 shown in the above example) and create a thread pool of 2 threads using Java ExecutorService as shown below:

// Import relevant packages here
public class executorSample {
	public static final String USERNAME = "YOUR_USERNAME";
	public static final String AUTOMATE_KEY = "YOUR_ACCESS_KEY";
	public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		ExecutorService executorService = Executors.newFixedThreadPool(2);  // A pool of 2 threads are being created here. You can change this as per your parallel limit
		Set<Callable<String>> callables = new HashSet<Callable<String>>();
		executorSample obj1 = new executorSample();
		callables.add(new Callable<String>() {
		    public String call() throws Exception {
					Hashtable<String, String> capsHashtable = new Hashtable<String, String>();
					capsHashtable.put("device", "iPhone 12 Pro");
					capsHashtable.put("real_mobile", "true");
					capsHashtable.put("os_version", "14");
			    capsHashtable.put("build", "BStack-[Java] Sample Build");
					capsHashtable.put("name", "Thread 1");
		    	obj1.executeTest(capsHashtable);
		        return "Task 1 completed";
		    }
		});
		callables.add(new Callable<String>() {
		    public String call() throws Exception {
					Hashtable<String, String> capsHashtable = new Hashtable<String, String>();
					capsHashtable.put("device", "Samsung Galaxy S20");
					capsHashtable.put("real_mobile", "true");
					capsHashtable.put("os_version", "11.0");
					capsHashtable.put("build", "BStack-[Java] Sample Build");
					capsHashtable.put("name", "Thread 2");
			    obj1.executeTest(capsHashtable);
		        return "Task 2 completed";
		    }
		});
		callables.add(new Callable<String>() {
		    public String call() throws Exception {
					Hashtable<String, String> capsHashtable = new Hashtable<String, String>();
					capsHashtable.put("browser", "safari");
					capsHashtable.put("browser_version", "14");
					capsHashtable.put("os", "OS X");
					capsHashtable.put("os_version", "Big Sur");
					capsHashtable.put("build", "BStack-[Java] Sample Build");
					capsHashtable.put("name", "Thread 3");
			    obj1.executeTest(capsHashtable);
		        return "Task 3 completed";
		    }
		});
		callables.add(new Callable<String>() {
		    public String call() throws Exception {
		    	obj1.executeTest("Safari", "latest", "OS X", "Catalina", "Test 4", "Java Executor Sample Build");
		        return "Task 4 completed";
		    }
		});
		// You can add as many test functions as Callables as you want
		List<Future<String>> futures;
		futures = executorService.invokeAll(callables);
		for(Future<String> future : futures){
			System.out.println("future.get = " + future.get());
		}
		executorService.shutdown();
	}
  public void executeTest(String browser, String version, String os, String os_version, String test_name, String build_name) {
	  // The same test function as shown in the code snippet above
	}
}

Parallel tests using a shell script

Alternatively, you can also write Java classes with respect to each test that you wish to run. Each class then has to be compiled and then you can run the individual Java tests in parallel using a shell script as shown below:

javac testClass1.java
javac testClass2.java
javac testClass3.java
javac testClass4.java
javac testClass5.java
javac testClass6.java
# Compile all your java classes

java testClass1 &
java testClass2 &
java testClass3 &
java testClass4 &
java testClass5 &
java testClass6 &
Note: You have to ensure that you have added the external Selenium libraries (JARs) as downloaded from Selenium website, in your CLASSPATH during compiling and running your Java classes.

In the above shell script, all 6 tests will be executed in parallel, in their own processes.

Conclusion

If you are using one of the Java 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, then 3 techniques are shown above for running Java tests in parallel. But it is not an exhaustive list. You may incorporate any method you like, to run your test code in parallel.

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