如何在Jenkins中于回归套件前运行Selenium WebDriver及前置配置测试
Hey there, let's walk through how to set up pre-regression tests (both configuration-focused tasks and Selenium WebDriver tests) in Jenkins for your Eclipse+TestNG+Maven+Java environment. I'll split this into two clear sections to cover both of your needs.
These tests handle setup tasks like verifying environment configs, initializing test data, or ensuring dependencies are ready before your regression suite runs. Here's how to wire this up:
Option 1: Use Maven Phases + TestNG Suites
First, structure your tests to separate setup from regression:
- Create a dedicated TestNG suite file (e.g.,
pre-regression-setup.xml) that contains all your configuration test classes. - In your Maven
pom.xml, bind this suite to an early phase (likepre-integration-test) so it runs before your regression tests (typically bound tointegration-test).
Example pom.xml snippet:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.1.2</version> <executions> <!-- Run pre-regression setup tests --> <execution> <id>pre-regression-setup</id> <phase>pre-integration-test</phase> <goals> <goal>test</goal> </goals> <configuration> <suiteXmlFiles> <suiteXmlFile>src/test/resources/pre-regression-setup.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </execution> <!-- Run regression suite --> <execution> <id>regression-suite</id> <phase>integration-test</phase> <goals> <goal>test</goal> </goals> <configuration> <suiteXmlFiles> <suiteXmlFile>src/test/resources/regression-suite.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </execution> </executions> </plugin> </plugins> </build>
Option 2: Split into Maven Modules
If your setup tests are extensive, split your project into modules:
- Create a
pre-regression-setupmodule with its own pom.xml and TestNG tests. - Your main regression module can reference this setup module (use
test-scopedependencies if shared test utilities are needed).
In Jenkins, run steps sequentially:
- Execute
mvn clean test -f pre-regression-setup/pom.xmlto run setup tests - Only proceed to the regression suite if the setup tests pass
Jenkins Configuration (Freestyle or Pipeline)
Freestyle Project
- Add a "Build Step" of type "Invoke top-level Maven targets" with goals matching your setup flow (e.g.,
clean pre-integration-testfor phase binding). - Enable "Abort the build if any step fails" to stop the pipeline if setup tests fail.
Pipeline (Recommended for Scalability)
Use a declarative pipeline to define the sequence clearly:
pipeline { agent any stages { stage('Run Pre-Regression Setup Tests') { steps { sh 'mvn clean test -DsuiteXmlFile=src/test/resources/pre-regression-setup.xml' } post { failure { echo 'Pre-regression setup failed! Aborting build.' error('Setup tests failed, regression suite will not run.') } } } stage('Run Regression Suite') { steps { sh 'mvn clean test -DsuiteXmlFile=src/test/resources/regression-suite.xml' } } } }
For Selenium tests, you need to handle headless execution (since Jenkins usually runs without a GUI) and smooth driver management. Here's how:
Step 1: Configure Selenium for Headless Execution
Use WebDriverManager (a Maven dependency) to auto-manage browser drivers—no manual downloads needed. Update your pom.xml:
<dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.11.0</version> </dependency> <dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>5.5.3</version> <scope>test</scope> </dependency> <!-- TestNG dependency --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.8.0</version> <scope>test</scope> </dependency> </dependencies>
Configure your Selenium tests to run headless (Chrome example):
import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import io.github.bonigarcia.wdm.WebDriverManager; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class PreRegressionSeleniumTest { private ChromeDriver driver; @BeforeClass public void setup() { WebDriverManager.chromedriver().setup(); ChromeOptions options = new ChromeOptions(); options.addArguments("--headless=new"); options.addArguments("--disable-gpu"); options.addArguments("--window-size=1920,1080"); driver = new ChromeDriver(options); } @Test public void verifyLoginPageLoads() { driver.get("https://your-test-app-url.com/login"); assert driver.getTitle().contains("Login"); } // Add more pre-regression Selenium tests here }
Step 2: Integrate Selenium Tests into Pre-Regression Flow
- Add your Selenium test classes to the
pre-regression-setup.xmlTestNG suite (or create a separatepre-regression-selenium.xmlif you want to split setup and Selenium tests). - Update your Maven or Jenkins configuration to run this suite before the regression suite, just like the configuration tests.
Jenkins-Specific Notes
- For Linux agents: Install Chrome/Firefox via package manager (e.g.,
sudo apt install chromium-browserfor Chrome). - For Windows agents: Ensure the browser is installed and WebDriverManager can access it.
- Avoid parallel Selenium test execution unless you've configured thread-safe driver instances—this prevents flaky failures.
- Always run pre-regression tests locally in Eclipse first to validate functionality before deploying to Jenkins.
- Use TestNG's
dependsOnGroupsordependsOnMethodsif you need specific execution order within pre-tests. - Add detailed logging to setup tests to speed up debugging in Jenkins.
内容的提问来源于stack exchange,提问作者Bob Jones

