Jenkins运行测试用例触发org.openqa.selenium.TimedoutException求助
org.openqa.selenium.TimedoutException in Jenkins (Works Locally in Eclipse) Let's break down why your tests are timing out in Jenkins but running fine locally—this almost always boils down to environment differences or configuration gaps. Here are the most likely fixes to try:
1. Fix the WebDriverWait Parameter (Critical Mistake!)
First, double-check your wait code snippet:
WebDriverWait wait = new WebDriverWait(driver, 1000); wait.until(ExpectedConditions.visibilityOfElementLocated(...));
The second argument to WebDriverWait is seconds, not milliseconds! If you intended to wait 10 seconds, this should be 10, not 1000. Waiting 1000 seconds is extreme, but even if that's intentional, the root issue is likely elsewhere. But fixing this parameter is a quick first check—if you accidentally used milliseconds (e.g., 1 instead of 10), that would cause immediate timeouts.
2. Handle Headless/No-GUI Environment in Jenkins
Jenkins servers typically run without a graphical user interface (GUI), while your local Eclipse uses your desktop's GUI. Chrome needs a display to render pages, so without one, it might fail to start properly or load elements slowly, leading to timeouts.
Solution A: Run Chrome in Headless Mode
Add these ChromeOptions to your driver setup (compatible with Chrome 65+):
ChromeOptions options = new ChromeOptions(); options.addArguments("--headless"); options.addArguments("--disable-gpu"); // Required for Windows environments options.addArguments("--window-size=1920,1080"); // Ensure elements are rendered properly WebDriver driver = new ChromeDriver(options);
Solution B: Use Xvfb (Virtual Display) on Jenkins
If headless mode causes issues, set up Xvfb on your Jenkins server to simulate a GUI. Install the Xvfb plugin in Jenkins, then configure your job to run with Xvfb (check "Start Xvfb before the build" in your job settings).
3. Resolve Version Compatibility Issues
Your stack has a significant version gap:
- Selenium 2.53 (released 2016)
- Chrome 65 (released 2018)
- Chromedriver 2.36 (matches Chrome 65, that part is correct)
Selenium 2.53 wasn't built to support Chrome 65—this is a major compatibility red flag. Older Selenium versions often struggle with newer Chrome releases, leading to unexpected timeouts or driver failures.
Fix: Upgrade Selenium to 3.x
Update your TestNG project to use Selenium 3.141.59 (the last stable version of Selenium 3, which has broad compatibility with Chrome 65+). This is the most impactful fix for version-related issues.
4. Optimize Jenkins Environment Resources
Jenkins servers might have slower network speeds or limited CPU/memory compared to your local machine. This can delay page loads and element rendering, triggering timeouts even if your code is correct.
- Check Network Access: Verify the Jenkins server can reach your target website without restrictions (firewalls, proxies). Add debug logs to confirm the browser is loading the page successfully.
- Adjust Wait Times: Increase your explicit wait duration (e.g., from 10 to 20 seconds) to account for slower environment speeds:
WebDriverWait wait = new WebDriverWait(driver, 20); // Wait up to 20 seconds - Monitor Server Resources: Check Jenkins server's CPU/memory usage during test runs—if it's maxed out, the browser can't render pages properly.
5. Refine Element Locators and Wait Conditions
Even if locators work locally, Jenkins might render pages differently (e.g., slower JavaScript execution). Try these tweaks:
- Use
presenceOfElementLocatedinstead ofvisibilityOfElementLocatedif you just need the element to exist in the DOM (not necessarily visible):wait.until(ExpectedConditions.presenceOfElementLocated(By.id("your-element-id"))); - Add a scroll action before waiting, to ensure the element is in view:
WebElement element = driver.findElement(By.id("your-element-id")); ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element); wait.until(ExpectedConditions.visibilityOf(element)); - Avoid fragile locators (e.g., relying on dynamic IDs or XPath with indexes). Use stable attributes like
data-testidif available.
内容的提问来源于stack exchange,提问作者Manigandan Thanigai Arasu

