I'm using JUnit and Selenium to do some browser testing, and need to run the same tests with multiple WebDrivers. To this end, I'm using JUnit parameterization so I can pass in an arbitrary number of WebDrivers at runtime and run the full test class for each driver.
Each driver should have .quit() called on it when it's no longer needed. Ideally, there would be an @AfterParameter method annotation that is called when each set of parameters are finished testing. In the absence of such an annotation, what's the cleanest way to avoid requiring test-writers to add a lot of boilerplate to their tests?
Here's what I have currently:
@Parameters(name = "{0}")
public static Collection<Object[]> config() throws IOException {
return SeleniumCore.getConfig();
}
private static WebDriver driver;
private static Collection<WebDriver> oldDrivers = new ArrayList<>();
public TestingClass(String name, SeleniumTestParameters params) {
// Stores old open drivers to be closed down at the end of testing.
if (TestingClass.driver != null) {
oldDrivers.add(driver);
}
TestingClass.driver = params.getDriver();
}
@AfterClass
public static void afterClass() {
for (WebDriver oldDriver : oldDrivers) {
oldDriver.quit();
}
driver.quit();
}
It allows for old drivers to be stored until the entire class gets torn down, and it works properly. However, it also requires extra boilerplate in both the constructor and the afterClass methods, as well as an additional piece of information. Is it possible to clean this up?