Avoid Usage of Static Variables in Tests
Using static variables should be avoided because in a parallel execution environment this invariably results in conflicts.
Consider this example.
The code snippet is a Factory class that returns a driver which is static, that is, one per class.
public class DriverFactory {
private static AppiumDriver driver;
public static AppiumDriver getDriver(DesiredCapabilities dc) {
if (driver == null) {
driver = new AndroidDriver("<seetest_cloud_url>", dc) :
}
return driver;
}
}
Assume two different tests try to make use of it.
public class A {
@Test
public void testA() {
DesiredCapabilities dc = new DesiredCapabilities();
.
.
AppiumDriver driver = DriverFactory.getDriver();
.
.
driver.quit();
}
}
public class B {
@Test
public void testB() {
DesiredCapabilities dc = new DesiredCapabilities();
.
.
AppiumDriver driver = DriverFactory.getDriver();
.
.
driver.quit();
}
}
Consider Class A and Class B belong to Test1 and Test2 respectively and both tests are run in parallel.
This would mean test methods, testA and testB both grab the driver simultaneously and lead to conflicts.
Many such examples can be given which shows that using static variables is not a good idea for parallel execution of tests and hence should be avoided.