How ThreadLocal Fixes Appium and Cucumber Test Failures in Parallel Execution
Running Appium and Cucumber tests in parallel can cause random failures because cucumber-spring creates a single ApplicationContext, making the AppiumDriver a shared singleton across all threads. This design flaw leads to interleaved commands, NullPointerExceptions, and screen objects bound to the wrong driver session. The fix involves moving the driver into a ThreadLocal so each thread maintains its own isolated session, replacing automatic driver creation with an explicit per-scenario call. Cucumber @Before and @After hooks must manage the driver lifecycle, and quitDriver() must call ThreadLocal.remove() to prevent dead sessions from being inherited by reused worker threads. Applying @ScenarioScope to screen objects ensures element proxies bind to the correct driver instance for each scenario.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in