Skip to content

Commit 455e818

Browse files
committed
Samples for jUnit parallel.
1 parent 8254c6d commit 455e818

2 files changed

Lines changed: 124 additions & 0 deletions

File tree

junit/JUnitParallelSample.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import static org.junit.Assert.*;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.URL;
6+
import java.util.LinkedList;
7+
8+
import org.apache.commons.io.FileUtils;
9+
import org.junit.After;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import org.junit.runners.Parameterized;
14+
import org.openqa.selenium.By;
15+
import org.openqa.selenium.OutputType;
16+
import org.openqa.selenium.Platform;
17+
import org.openqa.selenium.TakesScreenshot;
18+
import org.openqa.selenium.WebDriver;
19+
import org.openqa.selenium.WebElement;
20+
import org.openqa.selenium.remote.Augmenter;
21+
import org.openqa.selenium.remote.DesiredCapabilities;
22+
import org.openqa.selenium.remote.RemoteWebDriver;
23+
24+
@RunWith(Parallelized.class)
25+
public class JUnitParallelSample {
26+
private String platform;
27+
private String browserName;
28+
private String browserVersion;
29+
30+
@Parameterized.Parameters
31+
public static LinkedList getEnvironments() throws Exception {
32+
LinkedList<String[]> env = new LinkedList();
33+
34+
env.add(new String[]{Platform.XP.toString(), "chrome", "27"});
35+
env.add(new String[]{Platform.WINDOWS.toString(),"firefox","20"});
36+
env.add(new String[]{Platform.WINDOWS.toString(),"ie","7"});
37+
env.add(new String[]{Platform.WINDOWS.toString(),"opera","12.14"});
38+
//add more browsers here
39+
40+
return env;
41+
}
42+
43+
public JUnitParallelSample(String platform, String browserName, String browserVersion) {
44+
this.platform = platform;
45+
this.browserName = browserName;
46+
this.browserVersion = browserVersion;
47+
}
48+
49+
private WebDriver driver;
50+
51+
@Before
52+
public void setUp() throws Exception {
53+
DesiredCapabilities capability = new DesiredCapabilities();
54+
capability.setCapability("platform", platform);
55+
capability.setCapability("browser", browserName);
56+
capability.setCapability("browserVersion", browserVersion);
57+
capability.setCapability("build", "JUnit-Parallel");
58+
driver = new RemoteWebDriver(new URL("http://USERNAME:ACCESS_KEY@hub.browserstack.com/wd/hub"), capability);
59+
}
60+
61+
@Test
62+
public void testSimple() throws Exception {
63+
driver.get("http://www.google.com");
64+
String title = driver.getTitle();
65+
System.out.println("Page title is: " + title);
66+
assertEquals("Google", title);
67+
WebElement element = driver.findElement(By.name("q"));
68+
element.sendKeys("Browser Stack");
69+
element.submit();
70+
driver = new Augmenter().augment(driver);
71+
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
72+
try {
73+
FileUtils.copyFile(srcFile, new File("Screenshot.png"));
74+
} catch (IOException e) {
75+
e.printStackTrace();
76+
}
77+
}
78+
79+
@After
80+
public void tearDown() throws Exception {
81+
driver.quit();
82+
}
83+
}

junit/Parallelized.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.browserstack;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.TimeUnit;
6+
7+
import org.junit.runners.Parameterized;
8+
import org.junit.runners.model.RunnerScheduler;
9+
10+
public class Parallelized extends Parameterized {
11+
12+
private static class ThreadPoolScheduler implements RunnerScheduler {
13+
private ExecutorService executor;
14+
15+
public ThreadPoolScheduler() {
16+
String threads = System.getProperty("junit.parallel.threads", "16");
17+
int numThreads = Integer.parseInt(threads);
18+
executor = Executors.newFixedThreadPool(numThreads);
19+
}
20+
21+
@Override
22+
public void finished() {
23+
executor.shutdown();
24+
try {
25+
executor.awaitTermination(10, TimeUnit.MINUTES);
26+
} catch (InterruptedException exc) {
27+
throw new RuntimeException(exc);
28+
}
29+
}
30+
31+
@Override
32+
public void schedule(Runnable childStatement) {
33+
executor.submit(childStatement);
34+
}
35+
}
36+
37+
public Parallelized(Class klass) throws Throwable {
38+
super(klass);
39+
setScheduler(new ThreadPoolScheduler());
40+
}
41+
}

0 commit comments

Comments
 (0)