一、前言
java中的测试框架依赖有testng、junit,下面就这两种框架进行集成。
通过集成Allure生成web测试报告需要Allure环境,首先需要下载Allure压缩包,进行解压安装,然后配置对应的环境变量即可。
二、导入对应的依赖
2.1、使用testng进行测试的集成
<!-- testng 监听依赖 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- allure testng报表依赖 -->
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>2.12.1</version>
<scope>test</scope>
</dependency>
<!-- testNG依赖 -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
<!--执行单元测试的插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<skipTests>false</skipTests>
<!-- 配置项,如包含或排除特定的测试类 -->
<includes>
<include>**/*Tests.java</include>
<include>**/*Test.java</include>
<include>**/Test*.java</include>
</includes>
<!-- 指定testng.xml文件 -->
<!-- <suiteXmlFiles>-->
<!-- <suiteXmlFile>${basedir}/testng-test.xml</suiteXmlFile>-->
<!-- </suiteXmlFiles>-->
<!--设置参数命令行(testng) -->
<argLine>
<!-- 用于解决TestNG Result中文乱码 -->
-Dfile.encoding=UTF-8
<!-- 配置拦截器 -->
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
</argLine>
<!-- 测试失败后,是否忽略并继续测试 -->
<testFailureIgnore>true</testFailureIgnore>
<systemProperties>
<property>
<!-- 配置 allure 结果存储路径 -->
<name>allure.results.directory</name>
<value>./allure-results</value>
</property>
</systemProperties>
</configuration>
</plugin>
2.2、使用junit进行测试的集成
<!-- junit5监听 -->
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-junit5</artifactId>
<version>2.12.1</version>
<scope>test</scope>
</dependency>
<!-- junit5依赖 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<!--执行单元测试的插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<skipTests>false</skipTests>
<!-- 配置项,如包含或排除特定的测试类 -->
<includes>
<include>**/*Tests.java</include>
<include>**/*Test.java</include>
<include>**/Test*.java</include>
</includes>
<!--设置参数命令行(junit) -->
<argLine>
<!-- 用于解决TestNG Result中文乱码 -->
-Dfile.encoding=UTF-8
<!-- 配置拦截器 -->
-javaagent:"${settings.localRepository}/io/qameta/allure/allure-junit5/${allure-junit5.version}/allure-junit5-${allure-junit5.version}.jar"
</argLine>
<!-- 测试失败后,是否忽略并继续测试 -->
<testFailureIgnore>true</testFailureIgnore>
<systemProperties>
<property>
<!-- 配置 allure 结果存储路径 -->
<name>allure.results.directory</name>
<value>./allure-results</value>
</property>
</systemProperties>
</configuration>
</plugin>
三、测试
testng测试用例
package com.asl.prd004.test;
import com.asl.prd004.test.utils.BrowserWebDriverUtil;
import com.asl.prd004.test.utils.BrowserType;
//import org.junit.jupiter.api.*;
import org.openqa.selenium.WebDriver;
import com.asl.prd004.test.utils.ElementLocationType;
import org.testng.annotations.*;
import io.qameta.allure.*;
@Epic("AdminPortal测试")
@Feature("Login测试")
public class UserPortalLoginTest {
private WebDriver driver;
@BeforeTest
public void set_up(){
driver= BrowserWebDriverUtil.InitDriver(BrowserType.Chrome.getType());
}
@AfterClass
public void tearDown(){
//关闭浏览器
driver.quit();
}
@Test
@Story("LoginSuccess测试")
@Description(value = "1.Open admin portal\n" +
"2. Input a user name which the system exists\n" +
"3. Login the system")
public void testLoginSuccess() throws InterruptedException {
// Your test logic here
driver.get("http://localhost:3000/login");
driver.findElement(org.openqa.selenium.By.id("username")).click();
driver.findElement(org.openqa.selenium.By.id("password")).sendKeys("As123456!");
driver.findElement(org.openqa.selenium.By.id("username")).sendKeys("headuser3");
driver.findElement(org.openqa.selenium.By.name("rememberUser")).click();
driver.findElement(org.openqa.selenium.By.cssSelector(".MuiButton-contained")).click();
// java.util.concurrent.TimeUnit.SECONDS.sleep(2);
//显式等待
BrowserWebDriverUtil.ExplicitWait(driver,ElementLocationType.XPATH.getType(), "/html/body/div[1]/div[1]/div/div/div[1]/div[1]");
//截图
// BrowserWebDriverUtil.getImage(driver,"images/userPortal/login_success.png");
BrowserWebDriverUtil.getImageFile(driver, "login_success");
}
@Test
@Story("LoginFailUsername测试")
@Description(value = "1.Open admin portal\n" +
"2. Input a user name which the system not exists\n" +
"3. Login the system")
public void testLoginFailUsername() throws InterruptedException {
// Your test logic here
driver.get("http://localhost:3000/login");
driver.findElement(org.openqa.selenium.By.id("username")).click();
driver.findElement(org.openqa.selenium.By.id("password")).sendKeys("As123456!");
driver.findElement(org.openqa.selenium.By.id("username")).sendKeys("headuser3666");
driver.findElement(org.openqa.selenium.By.name("rememberUser")).click();
driver.findElement(org.openqa.selenium.By.cssSelector(".MuiButton-contained")).click();
// java.util.concurrent.TimeUnit.SECONDS.sleep(2);
//显式等待
BrowserWebDriverUtil.ExplicitWait(driver,ElementLocationType.XPATH.getType(), "/html/body/div/div[1]/div/div/div[1]");
//截图
// BrowserWebDriverUtil.getImage(driver,"images/userPortal/login_fail_username.png");
//添加截图附件
BrowserWebDriverUtil.getImageFile(driver, "login_fail_username");
}
@Test
@Story("LoginFailPassword测试")
@Description(value = "1.Open admin portal\n" +
"2. Input a user password which the system not exists\n" +
"3. Login the system")
@Step()
public void testLoginFailPassword() throws InterruptedException {
// Your test logic here
driver.get("http://localhost:3000/login");
driver.findElement(org.openqa.selenium.By.id("username")).click();
driver.findElement(org.openqa.selenium.By.id("password")).sendKeys("As123456!111");
driver.findElement(org.openqa.selenium.By.id("username")).sendKeys("headuser3");
driver.findElement(org.openqa.selenium.By.name("rememberUser")).click();
driver.findElement(org.openqa.selenium.By.cssSelector(".MuiButton-contained")).click();
//显式等待
BrowserWebDriverUtil.ExplicitWait(driver,ElementLocationType.XPATH.getType(),"/html/body/div/div[1]/div/div/div[1]");
//截图
// java.util.concurrent.TimeUnit.SECONDS.sleep(2);
// BrowserWebDriverUtil.getImage(driver,"images/userPortal/login_fail_password.png");
//添加截图附件
BrowserWebDriverUtil.getImageFile(driver, "login_fail_password");
}
}
junit测试用例
package com.asl.prd004.test.adminportal;
import com.asl.prd004.test.utils.BrowserWebDriverUtil;
import com.asl.prd004.test.utils.BrowserType;
import org.junit.jupiter.api.*;
import org.openqa.selenium.WebDriver;
import com.asl.prd004.test.utils.ElementLocationType;
@io.qameta.allure.Epic("AdminPortal测试")
@io.qameta.allure.Feature("Login测试")
public class AdminPortalLoginTest {
private WebDriver driver;
@BeforeEach
public void set_up(){
driver= BrowserWebDriverUtil.InitDriver(BrowserType.Chrome.getType());
}
@AfterEach
public void tearDown(){
//关闭浏览器
driver.quit();
}
@Test
@io.qameta.allure.Story("LoginSuccess测试")
@io.qameta.allure.Description(value = "1.Open admin portal\n" +
"2. Input a user name which the system exists\n" +
"3. Login the system")
public void testLoginSuccess() throws InterruptedException {
// Your test logic here
driver.get("http://localhost:3000/login");
driver.findElement(org.openqa.selenium.By.id("username")).sendKeys("headuser3");
driver.findElement(org.openqa.selenium.By.id("password")).sendKeys("As123456!");
driver.findElement(org.openqa.selenium.By.id("username")).click();
driver.findElement(org.openqa.selenium.By.name("rememberUser")).click();
driver.findElement(org.openqa.selenium.By.cssSelector(".MuiButton-contained")).click();
// java.util.concurrent.TimeUnit.SECONDS.sleep(2);
//显式等待
BrowserWebDriverUtil.ExplicitWait(driver,ElementLocationType.XPATH.getType(), "/html/body/div[1]/div[1]/div/div/div[1]/div[1]");
//截图
// BrowserWebDriverUtil.getImage(driver,"images/userPortal/login_success.png");
//添加截图附件
BrowserWebDriverUtil.getImageFile(driver, "login_success");
}
@Test
@io.qameta.allure.Story("LoginFailUsername测试")
@io.qameta.allure.Description(value = "1.Open admin portal\n" +
"2. Input a user name which the system not exists\n" +
"3. Login the system")
public void testLoginFailUsername() throws InterruptedException {
// Your test logic here
driver.get("http://localhost:3000/login");
driver.findElement(org.openqa.selenium.By.id("username")).click();
driver.findElement(org.openqa.selenium.By.id("password")).sendKeys("As123456!");
driver.findElement(org.openqa.selenium.By.id("username")).sendKeys("headuser3666");
driver.findElement(org.openqa.selenium.By.name("rememberUser")).click();
driver.findElement(org.openqa.selenium.By.cssSelector(".MuiButton-contained")).click();
// java.util.concurrent.TimeUnit.SECONDS.sleep(2);
//显式等待
BrowserWebDriverUtil.ExplicitWait(driver,ElementLocationType.XPATH.getType(), "/html/body/div/div[1]/div/div/div[1]");
//截图
// BrowserWebDriverUtil.getImage(driver,"images/userPortal/login_fail_username.png");
//添加截图附件
BrowserWebDriverUtil.getImageFile(driver, "login_fail_username");
}
@Test
@io.qameta.allure.Story("LoginFailPassword测试")
@io.qameta.allure.Description(value = "1.Open admin portal\n" +
"2. Input a user password which the system not exists\n" +
"3. Login the system")
public void testLoginFailPassword() throws InterruptedException {
// Your test logic here
driver.get("http://localhost:3000/login");
driver.findElement(org.openqa.selenium.By.id("username")).click();
driver.findElement(org.openqa.selenium.By.id("password")).sendKeys("As123456!111");
driver.findElement(org.openqa.selenium.By.id("username")).sendKeys("headuser3");
driver.findElement(org.openqa.selenium.By.name("rememberUser")).click();
driver.findElement(org.openqa.selenium.By.cssSelector(".MuiButton-contained")).click();
//显式等待
BrowserWebDriverUtil.ExplicitWait(driver,ElementLocationType.XPATH.getType(),"/html/body/div/div[1]/div/div/div[1]");
//截图
//java.util.concurrent.TimeUnit.SECONDS.sleep(2);
// BrowserWebDriverUtil.getImage(driver,"images/userPortal/login_fail_password.png");
//添加截图附件
BrowserWebDriverUtil.getImageFile(driver, "login_fail_password");
}
}
四、生成测试报告
执行完测试脚本后,会在项目路径下生成一个allure-results文件夹,其中存放着一堆json文件,这就是生成的测试结果文件了,但目前还无法直观地查看,需要使用如下方式将其渲染成可视化图表后查看。
方式一:直接创建Allure服务查看
注意这里的 要替换为allure-results文件所在的路径:
allure serve <path>
例如:allure serve E:\CodeSpace\swdata-ui-autotest\allure-results
需要注意的是:报告不会自动刷新,每次运行完测试后,都需要重新执行以上命令,重新生成报告。
方式二:生成html后查看
1、转换
allure转换命令:allure generate allure源文件目录 -o 转换后目录
allure generate allure-results -o report/
在这里插入图片描述
转换完成后,会在与allure-results同级目录下生成一个report文件夹。
其中有一个index.html文件,但不支持直接使用浏览器打开,需要使用allure做渲染后进行查看。渲染方式如下:
2、allure渲染报告并查看
以下命令中的report就是转化后的文件夹路径。
allure open report
执行完以上命令后,会自动打开浏览器。可查看对应的测试结果:
注意:有时候使用Allure生成的测试报告无法加载数据,这个时候考虑Allure版本跟项目集成的allure版本是否匹配
例如:我的allure版本是2.12.1,pom.xml中的依赖也应该与之对应