You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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.


1. Implementing Pre-Regression Configuration Tests

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 (like pre-integration-test) so it runs before your regression tests (typically bound to integration-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-setup module with its own pom.xml and TestNG tests.
  • Your main regression module can reference this setup module (use test-scope dependencies if shared test utilities are needed).

In Jenkins, run steps sequentially:

  1. Execute mvn clean test -f pre-regression-setup/pom.xml to run setup tests
  2. 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-test for phase binding).
  • Enable "Abort the build if any step fails" to stop the pipeline if setup tests fail.

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'
      }
    }
  }
}

2. Running Selenium WebDriver Tests Before Regression Suite

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.xml TestNG suite (or create a separate pre-regression-selenium.xml if 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-browser for 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.

Final Tips
  • Always run pre-regression tests locally in Eclipse first to validate functionality before deploying to Jenkins.
  • Use TestNG's dependsOnGroups or dependsOnMethods if you need specific execution order within pre-tests.
  • Add detailed logging to setup tests to speed up debugging in Jenkins.

内容的提问来源于stack exchange,提问作者Bob Jones

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:38:00