Speed up JUnit tests with concurrency
By running multiple junit tests at the same time you can cut down on overall test time
Helper class needed to run JUnit tests in parallel
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.runners.Parameterized;
import org.junit.runners.model.RunnerScheduler;
public class Parallelized extends Parameterized {
private static class ThreadPoolScheduler implements RunnerScheduler {
private ExecutorService executor;
public ThreadPoolScheduler() {
String threads = System.getProperty("junit.parallel.threads", "16");
int numThreads = Integer.parseInt(threads);
executor = Executors.newFixedThreadPool(numThreads);
}
@Override
public void finished() {
executor.shutdown();
try {
executor.awaitTermination(10, TimeUnit.MINUTES);
} catch (InterruptedException exc) {
throw new RuntimeException(exc);
}
}
@Override
public void schedule(Runnable childStatement) {
executor.submit(childStatement);
}
}
public Parallelized(Class<?> klass) throws Throwable {
super(klass);
setScheduler(new ThreadPoolScheduler());
}
}
Below is a JUnit Test example, which uses the above helper class to run the test in parallel
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Platform;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
@RunWith(Parallelized.class)
public class JUnitParallel {
private String platform;
private String browserName;
private String browserVersion;
@Parameterized.Parameters
public static LinkedList<String[]> getEnvironments() throws Exception {
LinkedList<String[]> env = new LinkedList<String[]>();
env.add(new String[]{Platform.WINDOWS.toString(), "chrome", "latest"});
env.add(new String[]{Platform.WINDOWS.toString(),"firefox","latest"});
env.add(new String[]{Platform.WINDOWS.toString(),"ie","9"});
//add more browsers here
return env;
}
public JUnitParallel(String platform, String browserName, String browserVersion) {
this.platform = platform;
this.browserName = browserName;
this.browserVersion = browserVersion;
}
private WebDriver driver;
@Before
public void setUp() throws Exception {
DesiredCapabilities capability = new DesiredCapabilities();
capability.setCapability("platform", platform);
capability.setCapability("browser", browserName);
capability.setCapability("browserVersion", browserVersion);
capability.setCapability("name", "Parallel test");
driver = new RemoteWebDriver(
new URL("https://key:secret@hub.testingbot.com/wd/hub"),
capability
);
}
@Test
public void testSimple() throws Exception {
driver.get("http://www.google.com");
String title = driver.getTitle();
System.out.println("Page title is: " + title);
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("TestingBot");
element.submit();
driver = new Augmenter().augment(driver);
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(srcFile, new File("Screenshot.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
@After
public void tearDown() throws Exception {
driver.quit();
}
}
Configuring capabilities
To let TestingBot know on which browser/platform you want to run your test on, you need to specify the browsername, version, OS and other optional options in the capabilities field.
Before
FirefoxDriver driver = new FirefoxDriver();
After
WebDriver driver = new RemoteWebDriver(
new URL("https://key:secret@hub.testingbot.com/wd/hub"),
DesiredCapabilities.firefox()
);
To see how to do this, please select a combination of browser, version and platform in the drop-down menus below:
-
Windows 10
-
Windows 8.1
-
Windows 8
Windows 7
macOS High Sierra
macOS Sierra
OS X El Capitan
OS X Yosemite
OS X Mavericks
Linux
iOS
Android
-
-
iPhone X12.1
-
iPhone 8 Plus12.1
-
iPhone 812.1
-
iPhone 7 Plus12.1
-
iPhone 712.1
-
iPhone 6s Plus12.1
-
iPhone 6s12.1
-
iPhone 612.1
-
iPhone 5s12.1
-
iPhone X11.4
-
iPhone 8 Plus11.4
-
iPhone 811.4
-
iPhone 6s Plus10.3
-
iPhone 6s Plus9.3
-
iPhone 711.4
-
iPhone 7 Plus11.4
-
Testing Internal/Staged Websites
We've built TestingBot Tunnel, to provide you with a secure way to run tests against your staged/internal webapps.
Please see our TestingBot Tunnel documentation for more information about this easy to use tunneling solution.
The example below shows how to easily run a Java WebDriver test with our Tunnel:
1. Download our tunnel and start the tunnel:
2. Adjust your test: instead of pointing to 'hub.testingbot.com/wd/hub'
like the example above - change it to point to your tunnel's IP address.
Assuming you run the tunnel on the same machine you run your tests, change to 'localhost:4445/wd/hub'
. localhost is the machine running the tunnel, 4445 is the default port of the tunnel.
This way your test will go securely through the tunnel to TestingBot and back:
@Before
public void setUp() throws Exception {
DesiredCapabilities capability = new DesiredCapabilities();
capability.setCapability("platform", platform);
capability.setCapability("browser", browserName);
capability.setCapability("browserVersion", browserVersion);
capability.setCapability("name", "Parallel test");
driver = new RemoteWebDriver(
new URL("http://key:secret@localhost:4445/wd/hub"),
capability
);
}
Other Options
We offer many other test options, for example: disable video recording, specifying a custom Firefox Profile, loading Chrome/Firefox/Safari extensions, running an executable before your test starts, uploading files, ...
See our list of test options for a full list of options to customize your tests.
Pick a Java test framework
-
JUnit
Junit is a unit testing framework for the java programming language
-
Parallel JUnit
By running multiple JUnit tests at the same time you can cut down on overall test time
-
TestNG
TestNG is a framework similar to JUnit and NUnit, which supports some additional commands and features