public Statement apply(final Statement base, Description description) {
// Handle JuTestEnv annotations
Method method = TestUtils.getTestMethod(description);
List<AnnotationInfo<JuTestEnv>> testEnvAnnos = ReflectUtils.getAnnotationsWithInfo(method, JuTestEnv.class, false, true, true);
Collections.reverse(testEnvAnnos);
final SystemPropertyTempSetter tempSetter = DbTestAnnotationHandler.setTestEnvProperties(testEnvAnnos);
try {
PropertyChain pc = JuUtils.getJuPropertyChain();
if (this.drivers.isEmpty()) {
// Get from the properties which drivers we should use to run the tests
String drivers[] = JuStringUtils.split(pc.get(PROP_DRIVER, true), ",", true);
Assert.assertTrue(String.format("No drivers specified in property %s", DriverRule.PROP_DRIVER), drivers.length > 0);
logger.debug("Initialize WebDrivers: " + Arrays.toString(drivers));
for (String driverType : drivers) {
WebDriver driver = null;
logger.debug("Creating driver: " + driverType);
if ("HtmlUnit".equals(driverType)) {
boolean enableJavaScript = pc.get("ju-testing-ee.selenium.htmlUnit.enableJavascript", Boolean.class);
driver = new HtmlUnitDriver(enableJavaScript);
} else if ("Chrome".equals(driverType)) {
System.setProperty("webdriver.chrome.driver", DriverRule.getChromeDriverExePath().toAbsolutePath().toString());
driver = new ChromeDriver();
} else {
throw new JuRuntimeException(String.format("Unsupported selenium driver type: %s. Check value of property %s"
, driverType
, PROP_DRIVER));
}
this.drivers.put(driverType, driver);
DriverRule.openDrivers.add(driver);
}
}
return new Statement() {
public void evaluate() throws Throwable {
try {
// Run test case for with all drivers
for (String driverType : drivers.keySet()) {
logger.info("Running test with WebDriver " + driverType);
testClass.driver = drivers.get(driverType);
// If the evaluation fails, it will break our loop, i.e. if we want to run drivers
// d1 and d2 and in d1, we have an exception, d2 won't be executed at all...
base.evaluate();
}
} finally {
tempSetter.close();
}
}
};
} catch (Exception ex) {
tempSetter.close();
throw ex;
}
}