在自动化测试的实际落地过程中,我们经常会遇到需要重复运行Selenium测试的场景,比如验证某个功能在不同运行次数下的稳定性,或者排查偶发性的测试失败问题。Serenity作为一款成熟的测试报告框架,搭配JUnit 5的扩展能力,能够非常便捷地实现Selenium测试的重复执行,并且可以生成包含多次执行详情的清晰报告。

环境准备
要实现Serenity和JUnit 5结合重复运行Selenium测试,首先需要准备好对应的依赖环境,这里以Maven项目为例,需要在pom.xml中添加以下核心依赖:
<dependencies>
<!-- Serenity核心依赖 -->
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-core</artifactId>
<version>4.0.12</version>
</dependency>
<!-- Serenity与JUnit 5集成依赖 -->
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-junit5</artifactId>
<version>4.0.12</version>
</dependency>
<!-- JUnit 5依赖 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
</dependency>
<!-- Selenium依赖 -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.16.0</version>
</dependency>
</dependencies>
使用JUnit 5内置注解实现重复运行
JUnit 5提供了@RepeatedTest注解,可以直接指定测试方法的重复执行次数,这个注解和Serenity的集成非常顺畅,不需要额外的复杂配置。
基础重复测试示例
下面是一个简单的示例,演示如何使用@RepeatedTest让Selenium测试重复运行3次:
import net.serenitybdd.junit5.SerenityJUnit5Extension;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.RepeatedTest;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(SerenityJUnit5Extension.class)
public class RepeatedSeleniumTest {
private WebDriver driver;
@RepeatedTest(3) // 指定测试重复执行3次
public void testBaiduTitle() {
// 初始化Chrome浏览器驱动
driver = new ChromeDriver();
// 打开测试页面
driver.get("https://www.ipipp.com");
// 验证页面标题
assertEquals("ipipp.com", driver.getTitle());
// 关闭浏览器
driver.quit();
}
}
在这个示例中,@RepeatedTest(3)表示testBaiduTitle方法会被重复执行3次,每次执行都会独立初始化浏览器、执行测试步骤、关闭浏览器。Serenity会自动记录每一次执行的详细结果,包括执行时间、是否通过、失败原因等信息。
获取重复执行的次数信息
如果需要在测试逻辑中获取当前的重复执行次数,可以给测试方法添加RepetitionInfo参数,JUnit 5会自动注入该参数:
import net.serenitybdd.junit5.SerenityJUnit5Extension;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
@ExtendWith(SerenityJUnit5Extension.class)
public class RepeatedSeleniumWithInfoTest {
private WebDriver driver;
@RepeatedTest(5)
public void testWithRepetitionInfo(RepetitionInfo repetitionInfo) {
int currentRepetition = repetitionInfo.getCurrentRepetition();
int totalRepetitions = repetitionInfo.getTotalRepetitions();
System.out.println("当前是第" + currentRepetition + "次执行,总共执行" + totalRepetitions + "次");
driver = new ChromeDriver();
driver.get("https://www.ipipp.com");
System.out.println("第" + currentRepetition + "次执行的页面标题是:" + driver.getTitle());
driver.quit();
}
}
结合Serenity配置文件优化重复测试
除了在注解中直接指定重复次数,我们还可以通过Serenity的配置文件来统一管理测试相关的配置,在项目的src/test/resources目录下创建serenity.conf文件,添加如下配置:
serenity {
# 测试报告标题
report.title = "Selenium重复测试报告"
# 是否显示多次重复执行的详细结果
show.repeated.tests = true
}
开启show.repeated.tests配置后,Serenity生成的报告会清晰展示每一次重复执行的独立结果,而不是只展示最终的汇总结果,方便我们排查某一次特定执行出现的问题。
重复测试的注意事项
- 重复执行测试时,要确保每次执行的测试数据是独立的,避免前一次执行的数据影响后一次执行的结果,比如每次执行前都重新初始化测试数据,执行后清理数据。
- 如果测试过程中使用了浏览器驱动,每次执行后一定要确保浏览器被正确关闭,避免出现浏览器进程残留占用系统资源的问题。
- 重复执行的次数不宜设置过多,否则会大幅增加测试执行的总时长,建议根据实际测试需求合理设置重复次数,比如验证稳定性可以设置5-10次,排查偶发问题可以根据问题出现的概率设置对应次数。
查看Serenity生成的重复测试报告
测试执行完成后,Serenity会在项目的target/site/serenity目录下生成测试报告,打开index.html就可以看到所有测试的汇总结果。对于重复执行的测试,报告会展示该测试的总执行次数、通过次数、失败次数,点击对应的测试条目还可以查看每一次重复执行的详细日志、截图(如果配置了截图)等信息,帮助快速定位问题。