thinket
thinket

Reputation: 97

TimeoutException when running Selenium tests in parallel

Context: Currently I'm working with a Selenium based system in Java that runs tests using JUnit and Maven. Through the Maven Surefire plugin, I'm able to run tests in parallel. I have ensured the following things -

Problem: However, when running tests in parallel, I'm getting TimeoutExceptions at WebDriverWait explicit waits. This can occur at any places in the test that use explicit waits. These timeout exceptions do not occur when the tests are running sequentially.

Question: I would like to know whether any of you have encountered this situation and how you go about solving this problem. Other relevant information and feedback are welcomed too.

Thanks in advance! If you need any supplemented resources such as sample code, I'm happy to provide.

Upvotes: 2

Views: 1739

Answers (1)

Erki M.
Erki M.

Reputation: 5072

Firstly I am not sure how to properly use multi-thread with JUnit, last time I tried I had no success, anyway, I have had better results with TestNG. Other that that, things are similar to yours, basically from maven (surefire) I am calling testng.xml, reference.

Now, webdriver, out-of-the-box, is not thread-safe. Threads can get mixed-up and all kind of "near-to-impossible-to-debug" stuff can happen. Anyway, lately WebDriver people have tried to tackle this problem and we now have ThreadGuard class available (source). That, according to docs:

Multithreaded client code should use this to assert that it accesses webdriver in a thread-safe manner.

So in your case you can simply use it like (from top of my head, sorry for typos):

ThreadLocal<WebDriver> driverStore = new ThreadLocal<>();
WebDriver driver = ThreadGuard.protect(new FirefoxDriver());
driverStore.set(driver);

I have had success using this setup.

Upvotes: 2

Related Questions