Ubuntu环境下Jenkins配置Selenium测试的分步指导求助
Hey there, I’ve been through exactly this kind of headache with Jenkins + Selenium on Ubuntu—spent days troubleshooting version mismatches and timeout issues before getting it right. Let’s walk through a version-locked, step-by-step setup to fix your connection timeouts and clear up the display mode question once and for all.
First, let’s lock down the version combinations that actually work—this is the #1 cause of timeout/display errors:
- Jenkins: 2.401.3 LTS (stable, compatible with most core plugins)
- Chrome Browser: 118.x (stable, must match ChromeDriver exactly)
- ChromeDriver: 118.x (matches your Chrome build number)
- Selenium: 4.15.0+ (supports Chrome’s native headless mode and latest ChromeDriver)
1. Prepare Ubuntu Environment
Start with a clean, updated base to avoid dependency conflicts:
- Update system packages:
sudo apt update && sudo apt upgrade -y - Install required dependencies for Chrome:
sudo apt install -y unzip libxss1 libappindicator1 libindicator7 fonts-liberation
2. Install Matching Chrome & ChromeDriver
Version mismatch here will guarantee timeout errors—don’t skip this step.
Install Chrome (Locked to Stable Version)
- Add Chrome’s official repository:
wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add - echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" | sudo tee /etc/apt/sources.list.d/google-chrome.list - Install a specific stable version (e.g., 118.0.5993.70) and lock it to prevent automatic updates:
sudo apt install -y google-chrome-stable=118.0.5993.70-1 sudo apt-mark hold google-chrome-stable
Install ChromeDriver (Exact Version Match)
- Download the ChromeDriver zip file that matches your Chrome version from the official ChromeDriver repository
- Extract and move to a system path Jenkins can access:
unzip chromedriver_linux64.zip sudo mv chromedriver /usr/local/bin/ sudo chmod +x /usr/local/bin/chromedriver - Verify the match:
google-chrome --version chromedriver --version
3. Configure Jenkins for Selenium
Global Tool Setup
- Log into Jenkins, go to Manage Jenkins > Global Tool Configuration
- If you don’t see ChromeDriver, install the ChromeDriver Plugin first via Manage Jenkins > Plugins
- Under ChromeDriver, either:
- Check "Install automatically" and select your matching version, or
- Specify the path manually:
/usr/local/bin/chromedriver
- Save the configuration
Fix Jenkins User Permissions
Jenkins runs as a dedicated jenkins user by default—grant it access to display resources:
sudo usermod -aG video jenkins sudo systemctl restart jenkins
Job Configuration
- For a Freestyle Job:
- In Build Environment, skip Xvfb if using native headless mode (we’ll cover this next)
- In Build steps, add an environment variable if needed:
PATH=$PATH:/usr/local/bin
- For a Pipeline Job:
Add a stage to set up the environment, or include the Chrome path directly in your script.
4. Run Tests Without Display Mode (Your Second Question Answered)
No, Jenkins test cases on Ubuntu do NOT need to run in display mode. Modern Chrome supports a native headless mode that eliminates the need for a physical or virtual display. Here’s how to configure your test scripts:
Python Selenium Example
from selenium import webdriver from selenium.webdriver.chrome.options import Options def setup_driver(): chrome_options = Options() # Native headless mode (Chrome 112+) chrome_options.add_argument("--headless=new") # Required for Jenkins user (lacks sandbox permissions) chrome_options.add_argument("--no-sandbox") # Fixes memory-related crashes on Ubuntu chrome_options.add_argument("--disable-dev-shm-usage") # Avoid rendering issues with a standard window size chrome_options.add_argument("--window-size=1920,1080") return webdriver.Chrome(options=chrome_options) # Test execution driver = setup_driver() driver.get("https://example.com") print(f"Page title: {driver.title}") driver.quit()
Java Selenium Example
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; public class JenkinsSeleniumTest { public static void main(String[] args) { ChromeOptions options = new ChromeOptions(); options.addArguments("--headless=new"); options.addArguments("--no-sandbox"); options.addArguments("--disable-dev-shm-usage"); options.addArguments("--window-size=1920,1080"); WebDriver driver = new ChromeDriver(options); driver.get("https://example.com"); System.out.println("Page title: " + driver.getTitle()); driver.quit(); } }
5. Troubleshooting Remaining Issues
- Chrome Connection Timeout:
- Double-check that Chrome and ChromeDriver versions are identical
- Ensure
--no-sandboxand--disable-dev-shm-usageare in your script (Jenkins can’t use Chrome’s sandbox by default) - Check Jenkins console logs for Chrome startup errors—look for lines like
Chrome failed to start
- Display Errors:
- If you must use a display (e.g., testing visual UI flows), enable Xvfb in the Jenkins job’s Build Environment and verify the
jenkinsuser is in thevideogroup
- If you must use a display (e.g., testing visual UI flows), enable Xvfb in the Jenkins job’s Build Environment and verify the
内容的提问来源于stack exchange,提问作者Tech_ninja

