Features

Speed up JUnit tests by running them in parallel

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();
	}
}

Specify Browsers & Devices

To let TestingBot know on which browser/platform/device you want to run your test on, you need to specify the browsername, version, OS and other optional options in the capabilities field.



Testing Internal 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 JUnit test with our Tunnel:


1. Download our tunnel and start the tunnel:

java -jar testingbot-tunnel.jar key secret

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
	);
}

Mark tests as passed/failed

To see if a test passed or not in our member area, or to send additional meta-data to TestingBot, you need to use our API.

TestingBot has a Java client for using the TestingBot API.

Once included with your tests, you can send back test status and other meta-data to TestingBot:


import com.testingbot.testingbotrest.TestingbotREST;

@After
public void tearDown() throws Exception {
	TestingbotREST r = new TestingbotREST("key", "secret");
	Map<String, String> data = new HashMap<String, String>();
	data.put("success", "1");
	data.put("name", "My Test");
	r.updateTest(driver.getSessionId(), data);
	driver.quit();
}

Other Java Framework examples

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