Reputation: 3
Current setup is maven executing cucumber in parallel, executing webdriver, executing chromedriver to test my software:
Maven:
<project>
...
<build>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<parallel>both</parallel>
<threadCount>3</threadCount>
<runOrder>random</runOrder>
</configuration>
</plugin>
</plugins>
</build>
</project>
Gherkin:
Function: Example
Scenario: Example 1
When I do something
Then I expect something
Scenario: Example 2
When I do some other thing
Then I expect some other thing
Cucumber-JVM / Webdriver-JVM / Chromedriver:
@Before
public void setUp() {
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("--window-size=1920,1080");
driver = new ChromeDriver(options);
driver.navigate().to(baseUrl);
}
@After
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
In principle, this works fine. Tests are execute in parallel and in random order, as instructed in the maven pom, setUp()
is called before each Scenario, tearDown()
is called after each Scenario.
However, ChromeDriver apparently runs multiple tests in one browser session, so Cookies etc. are shared between Functions and Scenarios leading to them not properly being isolated from each other which leads to further problems with cookies, sessions etc.
How to properly isolate each test case (ChromeDriver session) when running Webdriver via Cucumber-JVM in parallel?
Upvotes: 0
Views: 1519
Reputation: 167992
As per Parallel Tests Execution article:
We also need to ensure that to develop tests which can be executed simultaneously, we need to develop tests adhering to some best practices.
They are,
Independent Tests Methods: Tests should be independent of each other.
Usage of thread safe references : Tests should use thread safe variables. For example avoid usage of static references in the tests.
Repeatable : Tests should return always same results for same version of application and test inputs.
You seem to be using WebDriverManager and when you call chromedriver()
function it returns the relevant WebDriver instance from the instanceMap which is static and it seems to be the root cause of your problem. It is better to stick to ThreadLocal pattern, this way you will get confidence that each WebDriver instance is absolutely independent from the others.
If your resources allow you can add <forkMode>always</forkMode>
line to your surefire plugin configuration, this way Maven will create a new JVM instance per each thread hence you will stop suffering from the race condition, but your tests will start consuming way more RAM.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<parallel>both</parallel>
<threadCount>3</threadCount>
<runOrder>random</runOrder>
<forkMode>always</forkMode> <!--this should help-->
</configuration>
</plugin>
</plugins>
</build>
Upvotes: 2