-
-
Save TimB0/569935c85c9c8e96e3e72b7c9b6b2485 to your computer and use it in GitHub Desktop.
Parallel Selenium example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example.junit; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.TimeUnit; | |
import org.apache.log4j.Logger; | |
import org.junit.runners.Parameterized; | |
import org.junit.runners.model.RunnerScheduler; | |
/** | |
* From: http://hwellmann.blogspot.de/2009/12/running-parameterized-junit-tests-in.html | |
*/ | |
public class Parallelized extends Parameterized { | |
private static final Logger log = Logger.getLogger(Parallelized.class); | |
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) { | |
log.debug(String.format("scheduled (%s): %s", childStatement.getClass(), childStatement)); | |
executor.submit(childStatement); | |
} | |
} | |
public Parallelized(Class klass) throws Throwable { | |
super(klass); | |
setScheduler(new ThreadPoolScheduler()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example.junit; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collection; | |
import java.util.Collections; | |
import java.util.Iterator; | |
import java.util.List; | |
import org.apache.commons.lang3.StringUtils; | |
import org.apache.log4j.Logger; | |
import org.junit.AfterClass; | |
import org.junit.Before; | |
import org.junit.BeforeClass; | |
import org.junit.runner.RunWith; | |
import org.junit.runners.Parameterized; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
import org.openqa.selenium.firefox.FirefoxDriver; | |
/** | |
* Parallel Selenium WebDriver example for http://stackoverflow.com/questions/30353996/selenium-and-parallelized-junit-webdriver-instances | |
* Parallelized class is like http://hwellmann.blogspot.de/2009/12/running-parameterized-junit-tests-in.html | |
*/ | |
@RunWith(Parallelized.class) | |
public class ParallelSeleniumTestBase { | |
private static final Logger log = Logger.getLogger(ParallelSeleniumTestBase.class); | |
/** Available driver types */ | |
enum WebDriverType { | |
CHROME, | |
FIREFOX | |
} | |
/** Create WebDriver instances for specified type */ | |
static class WebDriverFactory { | |
static WebDriver create(WebDriverType type) { | |
WebDriver driver; | |
switch (type) { | |
case FIREFOX: | |
driver = new FirefoxDriver(); | |
break; | |
case CHROME: | |
driver = new ChromeDriver(); | |
break; | |
default: | |
throw new IllegalStateException(); | |
} | |
log(driver, "created"); | |
return driver; | |
} | |
} | |
// for description how to user Parametrized | |
// see: https://github.com/junit-team/junit/wiki/Parameterized-tests | |
@Parameterized.Parameter | |
public WebDriverType currentDriverType; | |
// test case naming requires junit 4.11 | |
@Parameterized.Parameters(name= "{0}") | |
public static Collection<Object[]> driverTypes() { | |
return Arrays.asList(new Object[][] { // | |
{ WebDriverType.CHROME }, // | |
{ WebDriverType.FIREFOX } // | |
}); | |
} | |
private static ThreadLocal<WebDriver> currentDriver = new ThreadLocal<WebDriver>(); | |
private static List<WebDriver> driversToCleanup = Collections.synchronizedList(new ArrayList<WebDriver>()); | |
@BeforeClass | |
public static void initChromeVariables() { | |
System.setProperty("webdriver.chrome.driver", "c:\\Programs\\chromedriver\\chromedriver.exe"); | |
//System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); | |
} | |
@Before | |
public void driverInit() { | |
if (currentDriver.get()==null) { | |
WebDriver driver = WebDriverFactory.create(currentDriverType); | |
driversToCleanup.add(driver); | |
currentDriver.set(driver); | |
} | |
} | |
protected WebDriver getDriver() { | |
return currentDriver.get(); | |
} | |
@AfterClass | |
public static void driverCleanup() { | |
Iterator<WebDriver> iterator = driversToCleanup.iterator(); | |
while (iterator.hasNext()) { | |
WebDriver driver = iterator.next(); | |
log(driver, "about to quit"); | |
driver.quit(); | |
iterator.remove(); | |
} | |
} | |
/** | |
* Log: current thread, current {@link WebDriver} and message. | |
*/ | |
protected static void log(WebDriver driver, String message) { | |
String driverShortName = StringUtils.substringAfterLast(driver.getClass().getName(), "."); | |
log.info(String.format("%15s, %15s: %s", Thread.currentThread().getName(), driverShortName, message)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example.junit; | |
import org.junit.Test; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebElement; | |
public class SearchKeywordsTest extends ParallelSeleniumTestBase { | |
@Test | |
public void searchForChromeDriver() throws InterruptedException { | |
openAndSearch(getDriver(), "chromedriver"); | |
} | |
@Test | |
public void searchForJunit() throws InterruptedException { | |
openAndSearch(getDriver(), "junit"); | |
} | |
@Test | |
public void searchForStackoverflow() throws InterruptedException { | |
openAndSearch(getDriver(), "stackoverflow"); | |
} | |
private void openAndSearch(WebDriver driver, String phraseToSearch) throws InterruptedException { | |
log(driver, "search for: "+phraseToSearch); | |
driver.get("http://www.google.com"); | |
WebElement searchBox = driver.findElement(By.name("q")); | |
searchBox.sendKeys(phraseToSearch); | |
searchBox.submit(); | |
Thread.sleep(3000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment