第二十三章Extent Reports -高级报告(ExtentReport的使用及解决CSS样式加载不出来的问题)
1.给 ExtentReport 添加样式等信息(解决CSS样式加载不出来的问题)
package ExtentFactory;
import java.io.File;
import java.util.Date;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ResourceCDN;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;
public class ExtentFactory {
public static ExtentReports getInstance() {
Date date = new Date();
String form = String.format("%tF", date);
String hour = String.format("%tH", date);
String minute = String.format("%tM", date);
String second = String.format("%tS", date);
// 生成的路径以及文件名
final String OUTPUT_FOLDER = "G:/wangjing-test/test-output3/";
final String FILE_NAME = "index" + form + hour + minute + second + ".html";
// 文件夹不存在的话进行创建
File reportDir = new File(OUTPUT_FOLDER);
if (!reportDir.exists() && !reportDir.isDirectory()) {
reportDir.mkdir();
}
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUTPUT_FOLDER + FILE_NAME);
// 设置静态文件的DNS
htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);
// 怎么样解决cdn.rawgit.com访问不了的情况
// htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);
htmlReporter.config().setDocumentTitle("标品页面功能自动化测试报告");
htmlReporter.config().setReportName("标品冒烟测试--页面功能自动化测试报告");
htmlReporter.config().setChartVisibilityOnOpen(true);
htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
htmlReporter.config().setTheme(Theme.STANDARD);
htmlReporter.config().setCSS(".node.level-1 ul{ display:none;} .node.level-1.active ul{display:block;}");
htmlReporter.config().setEncoding("utf-8");
ExtentReports extent = new ExtentReports();
extent.attachReporter(htmlReporter);
extent.setReportUsesManualConfiguration(true);
extent.setSystemInfo("Selenium Version", "3.11.0");
extent.setSystemInfo("Platform", "Windows");
return extent;
}
}
2.使用新构造的类创建报告,失败的时候截图并加入到测试报告中
package ScreenShots;
/**
* @author 96510
* @version 1.0
* @date 2021/7/25
*/
import ExtentFactory.ExtentFactory;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import utility.Screenshots1;
import java.io.IOException;
import java.text.SimpleDateFormat;
public class TestNG_Screenshots1 {
String baseUrl;
WebDriver driver;
public static ExtentReports report;
ExtentTest test;
@BeforeClass
public void beforeClass(){
baseUrl = "https://mail.qq.com/";
report = ExtentFactory.getInstance();
test =report.createTest("Test01");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(baseUrl);
test.log(Status.INFO,"LoginDemo Opened!");
}
@Test
public void loginTest() throws InterruptedException {
driver.switchTo().frame("login_frame");
driver.findElement(By.id("u")).sendKeys("965103066@qq.com");
test.log(Status.INFO,"Enter User Name!");
driver.findElement(By.id("p")).sendKeys("11111");
test.log(Status.INFO,"Enter User Password!");
driver.findElement(By.id("login_button")).click();
test.log(Status.INFO,"Click login button!");
Thread.sleep(3000);
WebElement webElement = null;
try {
webElement = driver.findElement(By.id("useralias"));
}catch (NoSuchElementException e){
System.out.println(e.getMessage());
}
Assert.assertTrue(webElement!=null);
test.log(Status.PASS,"登录成功!");
}
@AfterMethod
public void tearDown(ITestResult testResult) throws IOException {
if (testResult.getStatus()==ITestResult.FAILURE){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String path = Screenshots1.takeScreenshot(driver, testResult.getName());
test.addScreenCaptureFromPath(path);
test.log(Status.FAIL, "Verified Login failed");
}
driver.quit();
report.flush();//刷新报告
}
}
截图类:
package utility;
/**
* @author 96510
* @version 1.0
* @date 2021/7/25
*/
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import java.io.File;
import java.io.IOException;
public class Screenshots1 {
public static String takeScreenshot(WebDriver driver,String fileName) throws IOException{
fileName = fileName+".png";
String directory = "G:\\wangjing-test\\Screenshots\\";
File sourceFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(sourceFile, new File(directory+fileName));
String destination = directory +fileName;
return destination;
}
}
3.多个测试类生成一个测试报告
配置xml运行 多个类,多个测试类需要使用同一个测试报告对象
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Regression Test">
<test name="Test01" preserve-order="false">
<classes>
<class name="ReporterLog.TestNG_Reports"></class>
<class name="ScreenShots.TestNG_Screenshots2"></class>
</classes>
</test>
</suite>
package ScreenShots;
/**
* @author 96510
* @version 1.0
* @date 2021/7/25
*/
import DataClass.Data;
import ExtentFactory.ExtentFactory;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import utility.Screenshots1;
import java.io.IOException;
import java.text.SimpleDateFormat;
public class TestNG_Screenshots2 {
String baseUrl;
WebDriver driver;
ExtentReports report;
ExtentTest test;
@BeforeClass
public void beforeClass(){
baseUrl = "https://mail.qq.com/";
// report = ExtentFactory.getInstance();
report = TestNG_Screenshots1.report;
test =report.createTest("Test02");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(baseUrl);
test.log(Status.INFO,"LoginDemo Opened!");
}
@Test
public void loginTest() throws InterruptedException {
driver.switchTo().frame("login_frame");
driver.findElement(By.id("u")).sendKeys("965103066@qq.com");
test.log(Status.INFO,"Enter User Name!");
driver.findElement(By.id("p")).sendKeys(Data.getPwd());
test.log(Status.INFO,"Enter User Password!");
driver.findElement(By.id("login_button")).click();
test.log(Status.INFO,"Click login button!");
Thread.sleep(3000);
WebElement webElement = null;
try {
webElement = driver.findElement(By.id("useralias"));
}catch (NoSuchElementException e){
System.out.println(e.getMessage());
}
Assert.assertTrue(webElement!=null);
test.log(Status.PASS,"登录成功!");
}
@AfterMethod
public void tearDown(ITestResult testResult) throws IOException {
if (testResult.getStatus()==ITestResult.FAILURE){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String path = Screenshots1.takeScreenshot(driver, testResult.getName());
test.addScreenCaptureFromPath(path);
test.log(Status.FAIL, "Verified Login failed");
}
driver.quit();
report.flush();
}
4.在POM中用ExtentReport
HomePage类 封装页面操作
package HomePage;
import DataClass.Data;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
/**
* @author 96510
* @version 1.0
* @date 2021/7/27
*/
public class HomePageDemo {
WebDriver driver = null;
ExtentTest test;//构建是传入test,保证和测试执行类使用同一个test,日志信息打印到同一个test中
public HomePageDemo(WebDriver driver, ExtentTest test) {
this.driver = driver;
this.test = test;
}
public void SendName() {
driver.switchTo().frame("login_frame");
driver.findElement(By.id("u")).sendKeys("965103066");
test.log(Status.INFO, "Enter User Name!");
}
public void Sendpwd() {
driver.findElement(By.id("p")).sendKeys(Data.getPwd());
test.log(Status.INFO, "Enter User Password!");
}
public void click() {
driver.findElement(By.id("login_button")).click();
test.log(Status.INFO, "Click login button!");
}
public boolean findifcomein() {
boolean flag = false;
try {
flag = driver.findElement(By.id("useralias")).getText().equals(Data.getName());
} catch (Exception e) {
System.out.println(e.getMessage());
}
return flag;
}
}
执行操作,将结果写入测试报告
package HomePage;
/**
* @author 96510
* @version 1.0
* @date 2021/7/25
*/
import ExtentFactory.ExtentFactory;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import utility.Screenshots1;
import java.io.IOException;
import java.text.SimpleDateFormat;
public class TestNG_Screenshots00 {
String baseUrl;
WebDriver driver;
public static ExtentReports report;
ExtentTest test;
HomePageDemo hp;
@BeforeClass
public void beforeClass() {
baseUrl = "https://mail.qq.com/";
report = ExtentFactory.getInstance();
test = report.createTest("Test01");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(baseUrl);
test.log(Status.INFO, "LoginDemo Opened!");
}
@Test
public void loginTest() throws InterruptedException {
hp = new HomePageDemo(driver, test);
hp.SendName();
hp.Sendpwd();
hp.click();
Thread.sleep(3000);
Assert.assertTrue(hp.findifcomein());
test.log(Status.PASS, "登录成功!");
}
@AfterMethod
public void tearDown(ITestResult testResult) throws IOException {
if (testResult.getStatus() == ITestResult.FAILURE) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String path = Screenshots1.takeScreenshot(driver, testResult.getName());
test.addScreenCaptureFromPath(path);
test.log(Status.FAIL, "Verified Login failed");
}
driver.quit();
report.flush();
}
}
两个类的日志信息在测试报告的同一个test里面