We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Prevent cross-thread WebDriver calls in parallel Java tests
ThreadGuard is a support class in Selenium's Java bindings that helps you catch thread-safety problems with WebDriver.
When you run tests in parallel, it's easy to accidentally:
ThreadGuard wraps your driver and checks:
"Is this driver being used from the same thread that created it?"
If not, it throws a WebDriverException immediately with a clear message indicating which thread created the driver and which thread is trying to access it.
Wraps your existing WebDriver instance with a layer that remembers which thread created it.
Always use protectedDriver everywhere in your test
If any other thread tries to use protectedDriver, ThreadGuard throws a clear exception:
Parallel tests (e.g. TestNG parallel, JUnit parallel, custom threads) need:
WebDriver instance per threadThreadLocal<WebDriver> or your own DriverFactoryImportant: ThreadGuard does not replace ThreadLocal. It adds a safety net—if you misconfigure your driver sharing, it fails fast with a clear error.
ThreadLocal (or per-test driver instances) with ThreadGuard to detect mistakes earlyImagine a simple page called "ThreadGuard Demo":
Main thread only
Note: This UI is intentionally simple. The real demonstration happens in your Java code when you try to access the protected driver from different threads.
The page itself is trivial—ThreadGuard's value is demonstrated in the code!
Your job is to:
driver using ThreadGuard.protect(driver)protectedDriver (not driver) to:protectedDriver:protectedDriver.getTitle()WebDriverException about thread safetyThe real goal is to see how ThreadGuard reacts when one driver is accessed from two different threads.
123456789101112131415WebDriver protectedDriver = ThreadGuard.protect(driver);
// Safe usage from the creating (main) thread
WebElement heading = protectedDriver.findElement(By.id("threadguard-heading"));
String headingText = heading.getText();
// Unsafe usage from a different thread (will trigger ThreadGuard)
Runnable otherTask = () -> {
protectedDriver.getTitle();
};
Thread otherThread = new Thread(otherTask);
otherThread.start();
protectedDriver.quit();driver in ThreadGuard, creating protectedDriver that remembers the thread that constructed it.id="threadguard-heading".otherTask to run. When otherTask calls protectedDriver.getTitle(), ThreadGuard detects that the call comes from a different thread and throws a WebDriverException.Use ThreadGuard.protect(driver) as soon as you create the driver, and then only pass around protectedDriver in your codebase.
Remember: ThreadGuard is a safety net, not a replacement for ThreadLocal or proper per-thread driver management. Use both together.
When you start seeing strange, random parallel test failures, try enabling ThreadGuard—it can turn mysterious intermittent bugs into a clear, actionable error message.