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 (DriverRule.driverHandlers.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) {
logger.debug("Creating driver: " + driverType);
if ("HtmlUnit".equals(driverType)) {
DriverRule.driverHandlers.add(new HtmlUnitDriverHandler());
} else if ("Chrome".equals(driverType)) {
DriverRule.driverHandlers.add(new ChromeDriverHandler());
} else {
throw new JuRuntimeException(String.format("Unsupported selenium driver type: %s. Check value of property %s"
, driverType
, PROP_DRIVER));
}
}
}
return new Statement() {
public void evaluate() throws Throwable {
try {
// Run test case for with all drivers.
// We cannot use a for-iterator here as the closeAll method gets called when the for
// method tries to loop again, resulting in a ConcurrentModificationException.
for (int i = 0; i < driverHandlers.size(); i++) {
DriverHandler driverHandler = driverHandlers.get(0);
try (DriverHandler.DriverHandlerCreator driverCreator = driverHandler.newDriverHandlerCreator()) {
logger.info("Running test with WebDriver " + driverCreator);
testClass.driver = driverCreator.getDriver();
// 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;
}
}