1.pom.xml配置java
<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.javaweb
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 = "一、打開瀏覽器 二、輸入百度地址"; return step; } /** * 打印測試步驟 * @param tr */ // @Attachment(value = "指望結果以下:") public String exceptedResult(ITestResult tr){ String result = "顯示查詢結果"; return result; } }
代碼解釋:
使用Allure的@Attachment註解就能夠在測試報告裏添加附件了,這裏添加了截圖、測試步驟和指望結果
截圖代碼((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES)裏的driver經過BaseTest類傳過來chrome
3.設置監聽apache
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.測試方法類maven
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.xmlui
<?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 testgoogle
7.查看Allure報表 allure serve allure-resultsspa
8.後續完善。。。。待續。。。。
參考原文:https://blog.csdn.net/galen2016/article/details/88877395