在此之前请确保自己已经配置好了浏览器驱动和Allure下载安装,浏览器驱动没有配置好可以参考我上一篇文章,后续我会更新Allure安装配置(百度也有很多Allure安装配置教程)
我的环境:JDK21+IDEA2024.1.4+Springboot3.3.7
这个入门不管创建方式使用了Maven还是Springboot,只要依赖能够添加进来,实现效果都是一样的,由于我习惯于使用Springboot,方便我集成其他组件,所以这里我就选择了Springboot,话不多说,开始。
添加依赖
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.2</version>
<!--<scope>test</scope>-->
</dependency>
<!-- Selenium -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.10.0</version>
</dependency>
<!--allure-->
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>2.27.0</version>
</dependency>
<!-- Allure dependency for attaching screenshots 将截图添加到allure报告中-->
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-java-commons</artifactId>
<version>2.17.2</version>
</dependency>
添加完依赖不要忘记ReLoad,否则不起作用
开始入门案例
首先我们在test包下创建一个AllureLearn类,用于测试我们的自动化脚本
然后开始初始化我们的浏览器驱动
@Epic("入门案例")
@Feature("邮箱登陆功能")
public class AllureLearn {
@Test
@Story("错误用户名登陆测试")
public void test() throws InterruptedException {
//Chrome驱动
System.setProperty("webdriver.chrome.driver","C:\Program Files\Google\Chrome\Application\chromedriver.exe");
ChromeOptions options = new ChromeOptions();//创建ChromeOptions对象
options.addArguments("--remote-allow-origins=*");//允许所有请求
WebDriver webDriver = new ChromeDriver(options);//创建WebDriver对象
webDriver.manage().window().maximize();//最大化浏览器窗口
}
代码解释:
@Epic 定义最高层级模块,可以描述测试用例或测试类所属的最高层级的功能模块或项目
@Feature 用于定义测试用例或测试类所属的功能模块,当你有一个大的项目(由 @Epic 描述),而这个项目又包含多个功能模块时,可以使用 @Feature 来进一步细分
@Story 描述测试用例的用户故事或场景
@Test 这个注解来自于 testng,用于声明这是一个测试用例
System.setProperty("webdriver.chrome.driver","C:\Program Files\Google\Chrome\Application\chromedriver.exe");
这行代码是声明了驱动为chrome.driver,并且给出驱动指定文件目录
ChromeOptions options = new ChromeOptions();//创建ChromeOptions对象
我们可以通过ChromeOptions对象来配置和自定义Chrome浏览器的行为
比如:
options.addArguments("--headless"); // 无头模式
options.addArguments("--disable-gpu"); // 禁用GPU加速
options.addArguments("--window-size=1920,1080"); // 设置窗口大小
......
WebDriver webDriver = new ChromeDriver(options);
ChromeOptions对象的options是用来配置 Chrome 浏览器的行为和启动参数的,而 WebDriver 接口(具体实现为 ChromeDriver)是用于与浏览器进行交互的对象。
webDriver.manage().window().maximize();
这个很见名知意了,通过webDriver调用管理方法中的window,最大化窗口
那为什么最大化窗口,因为不最大化,被遮挡到的元素就无法扫描到,无法定位
Selenium八种定位方式
接下来我们想对网站进行操作,就需要知道Selenium的八种定位方式
| Selenium八种定位方式 | 语法 |
|---|---|
| id定位 | webDriver.findElement(By.id("id的值")); |
| name定位 | webDriver.findElement(By.name("name的值")); |
| linkText | webDriver.findElement(By.linkText("链接里全部文字")); |
| partialLinkText | webDriver.findElement(By.partialLinkText("链接里部分文字")); |
| css定位 | webDriver.findElement(By.cssSelector("css选择器")); |
| xpath定位 | webDriver.findElement(By.xpath("xpath选择器")); |
| class名称定位 | webDriver.findElement(By.className("class属性")); |
| TagName标签名定位 | webDriver.findElement(By.tagName("标签名")); |
模拟登录测试
接下来我们找一个网站做模拟测试 我这边使用的是新浪邮箱,mail.sina.com.cn/
(希望新浪不要介意,我只是用于学习,狗头狗头狗头狗头狗头狗头) 代码示例
package asia.springcloud;
import io.qameta.allure.Epic;
import io.qameta.allure.Feature;
import io.qameta.allure.Story;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
/**
* @author lizheng
* @version 1.0
* @description: TODO
* @CreateTime 2025/1/5 19:28
*/
@Epic("入门案例")
@Feature("邮箱登陆功能")
public class AllureLearn {
@Test
@Story("错误用户名登陆测试")
public void test() throws InterruptedException {
//Chrome驱动
System.setProperty("webdriver.chrome.driver","C:\Program Files\Google\Chrome\Application\chromedriver.exe");
ChromeOptions options = new ChromeOptions();//创建ChromeOptions对象
options.addArguments("--remote-allow-origins=*");//允许所有请求
WebDriver webDriver = new ChromeDriver(options);//创建WebDriver对象
webDriver.manage().window().maximize();//最大化浏览器窗口
webDriver.get("https://mail.sina.com.cn/");
TimeUnit.SECONDS.sleep(3);
WebElement element = webDriver.findElement(By.id("freename"));
element.sendKeys("123456");
TimeUnit.SECONDS.sleep(1);
webDriver.findElement(By.id("freepassword")).sendKeys("123456789");
webDriver.findElement(By.className("loginBtn")).click();
TimeUnit.SECONDS.sleep(3);
webDriver.quit();
}
}
效果
可以看到,web自动化很简单的,有代码基础就很容易看懂调用的方法是什么意思
| 标题 | |
|---|---|
| webDriver.get("www.xxxxxx.xx"); | 打开某个网站 |
| TimeUnit.SECONDS.sleep(3); | 休眠,因为自动化的速度非常快的,有些元素要给他加载时间否则就会报错NoSuchWindowException,也就是找不到这个元素 |
| element.sendKeys("12345"); | 找到这个元素之后,用WebElement接收一下,调用sendKeys输入方法,没有特殊需求也可以直接在findElement后面调用 |
| webDriver.quit(); | 见名知意,退出方法,释放资源 |
然后我们在idea的控制台打开终端,输入allure serve ./allure-results命令,这是 Allure 报告生成工具中的一个命令,用于生成并查看测试报告,输入之后回车,会自动弹出网页
因为我的idea中有9个测试案例,所以我这里显示9,大家可以熟悉以下这个网页,对于我们工作还是有些帮助的
至此,我们SpringBoot+Selenium+TestNG+Allure入门案例就完成了,后续我会更新TestNG前置通知和后置通知案例,和TestNG生成测试报告,和其他更多技术,祝大家都能找到好工作