1.pom.xml配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xiaobing</groupId> <artifactId>web_auto</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <aspectj.version>1.8.10</aspectj.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8.8</version> <scope>test</scope> </dependency> <dependency> <groupId>io.qameta.allure</groupId> <artifactId>allure-testng</artifactId> <version>2.12.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <configuration> <argLine> -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar" </argLine> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> <dependencies> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj.version}</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
2.截图监听器:TestngListener.java
package com.test.util; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.testng.ITestResult; import org.testng.TestListenerAdapter; import com.test.cases.BaseCase; import io.qameta.allure.Attachment; public class TestngListener extends TestListenerAdapter { public void onTestFailure(ITestResult tr) { super.onTestFailure(tr); BaseCase bt = (BaseCase) tr.getInstance(); WebDriver driver = bt.driver; takePhoto(driver); logCaseStep(tr); exceptedResult(tr); } @Attachment(value = "失败截图如下:",type = "image/png") public byte[] takePhoto(WebDriver driver){ byte[] screenshotAs = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES); return screenshotAs; } /** * 打印测试步骤 * @param tr */ // @Attachment(value = "操作步骤如下:") public String logCaseStep(ITestResult tr){ String step = "1、打开浏览器 2、输入百度地址"; return step; } /** * 打印测试步骤 * @param tr */ // @Attachment(value = "期望结果如下:") public String exceptedResult(ITestResult tr){ String result = "显示查询结果"; return result; } }
代码解释:
使用Allure的@Attachment注解就可以在测试报告里添加附件了,这里添加了截图、测试步骤和期望结果
截图代码((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES)里的driver通过BaseTest类传过来
3.设置监听
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterSuite; import org.testng.annotations.BeforeSuite; import org.testng.annotations.Listeners; import org.testng.annotations.Parameters; import com.util.TestngListener; @Listeners({TestngListener.class}) public class BaseCase {//声明驱动对象 public static WebDriver driver = null; /**根据浏览器类型去初始化驱动对象(启动浏览器) * @param browserType */ @Parameters(value={"browserType"}) @BeforeSuite public void init(String browserType){ ...........................................if("chrome".equalsIgnoreCase(browserType)){ //chrome System.setProperty("webdriver.chrome.driver","src/test/resources/chromedriver.exe"); driver = new ChromeDriver(); } ............................................. } @AfterSuite public void teardown() throws InterruptedException{ Thread.sleep(10000); //关闭浏览器,关闭可执行驱动 driver.quit(); } }
代码解释:
这是测试用例的基类,具体的测试用例类通过继承BaseTest获取driver
BaseTest加上@Listeners({TestngListener.class})注解,就可以对所有的测试类进行监听了
4.测试方法类
package com.test.cases; import org.testng.Assert; import org.testng.annotations.Test; public class TestScreenshot extends BaseCase { @Test public void test01() { driver.get("https://www.baidu.com"); Assert.assertEquals(driver.getTitle(), "百度一下,你就知道"); } @Test public void test02() { driver.get("https://www.google.cn/intl/zh-CN/chrome/"); Assert.assertEquals(driver.getTitle(), "百度一下,你就知道"); } @Test public void test03() { driver.get("https://www.google.cn/intl/zh-CN/chrome/"); Assert.assertTrue(driver.getTitle().contains("chrome"), "是否打开chrome官网页面"); } }
代码解释:
测试用例类,继承BaseCase.java,直接使用BaseCase的driver
5.Testng.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <!-- 套件 --> <suite name="TEST系统"> <!-- 测试集/模块 --> <test name="TEST"> <!-- 测试类集合 --> <classes> <class name="com.test.cases.TestScreenshot"></class> </classes> </test> </suite>
6.构建 mvn clean test
7.查看Allure报表 allure serve allure-results
8.后续完善。。。。待续。。。。
参考原文:https://blog.csdn.net/galen2016/article/details/88877395