第二十三章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);
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;
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