一、创建 springboot 项目
请查看 csdn 中其他教程:省略
本次使用的是 Spring boot 2.7.0
二、集成 Junit5
-
引入 Junit5 依赖(Spring boot 2.7.0 中已经集成了 Junit 5 ,可以省略该步骤)
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.3.2</version> <scope>test</scope> </dependency>
-
排除 Junit4 的影响(其他平台上说不排除会报错,但是我没有发现😔,人家都排除,咱也排除吧😓)
Junit4 的引擎是 junit-vintage-engine;Junit5 的引擎是 junit-jupiter-engine
排除掉 junit-vintage-engine 依赖(只使用 Junit5),可避免运行测试用例时报错。<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency>
三、集成 Selenium 4.x
-
引入 Selenium 4.x 依赖
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.2.2</version> </dependency>
四、编写简单的用例,查看是否集成成功
如果可以正常从浏览器打开百度,并且关闭浏览器则说明没有问题
public class LoginTest {
public static WebDriver driver;
@BeforeAll
public static void SetUp() {
driver = new ChromeDriver();
}
@Test
public void LoginTest() {
driver.get("https://baidu.com/");
}
@AfterAll
public static void TearDown(){
driver.quit();
}