πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
Back to All Scenarios
Scenario 12 of 100
CI/CD
Advanced

Script Works Locally but Fails on Jenkins / CI due to Timing

πŸ•’Scenario Overview

Script Works Locally but Fails on Jenkins / CI due to Timing

Key Takeaways & Cheat Sheet

  • βœ“Acknowledge virtualized hardware resources are slower than local dev machines
  • βœ“Eliminate hardcoded Thread.sleep() and replace with dynamic wait synchronization
  • βœ“Increase default explicit wait timeouts specifically for CI environments
  • βœ“Configure headless execution viewport resolution to match desktop layouts

Short Direct Answer

CI/CD pipeline runners (like Jenkins agents, GitHub Actions, or Docker containers) operate on virtualized, shared hardware that has significantly slower rendering and network response speeds compared to a developer's local machine. This creates race conditions where elements do not load in time. To resolve this, eliminate static sleeps, standardize browser viewports to 1920x1080, and scale explicit waits dynamically using environment configuration variables.

⚠️ Senior Warning (Red Flag)

Do not simply add long Thread.sleep() statements to "fix" CI timing. This bloats test execution, wastes container billing hours, and masks real application performance degradation.

πŸ’‘ STAR Deep Dive Explanation & Pro Tip

Standardizing your environment with containers (like Docker) and pinning browser and driver versions prevents environment-specific timing failures.

SeleniumAutomation.java
Selenium 4 + Java
// βœ… Dynamic timeout configuration scaled by environment
int baseTimeout = System.getenv("CI") != null ? 20 : 10;
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(baseTimeout));

// βœ… Standardize viewport size for headless execution to prevent hidden menus
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new");
options.addArguments("--window-size=1920,1080");
WebDriver driver = new ChromeDriver(options);