seleniumjava⾼级技巧篇(必学)美化测试报告(⼗三)⼀般我们⽤的testng⾃带的报告太low了
那现在我们准备输出⼀份⽐较美观的报告,
1,先从⽹上下载个模板和⼏个jar包,
解压
把三个jar包引⽤进来,再把template复制到项⽬根⽬录下
2添加maven⽂件到l
<!-- report  -->
<dependency>
<groupId&le.code.gson</groupId>  <artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.4</version>
<scope>test</scope>
</dependency>
3创建报告类
package seleniumUtil;
stng.IReporter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import Matcher;
stng.IReporter;
stng.IResultMap;
stng.ISuite;
stng.ISuiteResult;
stng.ITestContext;
stng.ITestResult;
stng.Reporter;
l.XmlSuite;
le.gson.Gson;
le.gson.GsonBuilder;
public class TestCaseReport implements IReporter {
private long currentTime = System.currentTimeMillis();
private SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年-MM⽉-dd⽇-HH时mm分ss秒");    private Date date = new Date(currentTime);
private String reportdate = formatter.format(date);
private String path = Property("user.dir")+File.separator+reportdate+"report.html";
private String templatePath = Property("user.dir")+File.separator+"template.html";
private int testsPass = 0;
private int testsFail = 0;
private int testsSkip = 0;
private String beginTime;
private long totalTime;
private String name = "Selenium 测试报告";
/***
public ZTestReport(){
SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmssSSS");
name = formatter.format(System.currentTimeMillis());
}
public ZTestReport(String name){
this.name = name;
if(this.name==null){
SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmssSSS");
this.name = formatter.format(System.currentTimeMillis());
}
}
*/
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
List<ITestResult> list = new ArrayList<ITestResult>();
for (ISuite suite : suites) {
Map<String, ISuiteResult> suiteResults = Results();
for (ISuiteResult suiteResult : suiteResults.values()) {
ITestContext testContext = TestContext();
IResultMap passedTests = PassedTests();
testsPass = testsPass + passedTests.size();
IResultMap failedTests = FailedTests();
testsFail = testsFail + failedTests.size();
IResultMap skippedTests = SkippedTests();
testsSkip = testsSkip + skippedTests.size();
IResultMap failedConfig = FailedConfigurations();
list.addAll(this.listTestResult(passedTests));
list.addAll(this.listTestResult(failedTests));
list.addAll(this.listTestResult(failedTests));
list.addAll(this.listTestResult(skippedTests));
list.addAll(this.listTestResult(failedConfig));
}
}
this.sort(list);
this.outputResult(list);
}
private ArrayList<ITestResult> listTestResult(IResultMap resultMap) {
Set<ITestResult> results = AllResults();
return new ArrayList<ITestResult>(results);
}
private void sort(List<ITestResult> list) {
Collections.sort(list, new Comparator<ITestResult>() {
public int compare(ITestResult r1, ITestResult r2) {
if (r1.getStartMillis() > r2.getStartMillis()) {
return 1;
} else {
return -1;
}
}
});replaceall()
}
private void outputResult(List<ITestResult> list) {
try {
List<ReportInfo> listInfo = new ArrayList<ReportInfo>();
int index = 0;
for (ITestResult result : list) {
String tn = TestContext().getCurrentXmlTest().getParameter("testCase");
if(index==0){
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSS");    beginTime = formatter.format(new StartMillis()));
index++;
}
long spendTime = EndMillis() - StartMillis();
totalTime += spendTime;
String status = Status());
List<String> log = Output(result);
for (int i = 0; i < log.size(); i++) {
log.set(i, (i).replaceAll("\"", "\\\\\""));
}
Throwable throwable = Throwable();
if(throwable!=null){
log.String().replaceAll("\"", "\\\\\""));
StackTraceElement[] st = StackTrace();
for (StackTraceElement stackTraceElement : st) {
log.add(("    " + stackTraceElement).replaceAll("\"", "\\\\\""));
}
}
ReportInfo info = new ReportInfo();
info.setName(tn);
info.setSpendTime(spendTime+"ms");
info.setStatus(status);
info.InstanceName());
info.Name());
info.Method().getDescription());
info.setLog(log);
listInfo.add(info);
}
Map<String, Object> result = new HashMap<String, Object>();
result.put("testName", name);
result.put("testPass", testsPass);
result.put("testPass", testsPass);
result.put("testFail", testsFail);
result.put("testSkip", testsSkip);
result.put("testAll", testsPass+testsFail+testsSkip);
result.put("beginTime", beginTime);
result.put("totalTime", totalTime+"ms");
result.put("testResult", listInfo);
Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
String template = ad(templatePath);
//BufferedWriter output = new BufferedWriter();
//output.write(new Bytes("gbk"),"utf-8"));
//BufferedWriter output = new BufferedWriter(new FileWriter(path));
BufferedWriter output = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(new File(path)),"utf-8"));  template = placeFirst("\\$\\{resultData\\}", Matcher.Json(result)));
output.write(template);
output.flush();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private String getStatus(int status) {
String statusString = null;
switch (status) {
case 1:
statusString = "成功";
break;
case 2:
statusString = "失败";
break;
case 3:
statusString = "跳过";
break;
default:
break;
}
return statusString;
}
public static class ReportInfo {
private String name;
private String className;
private String methodName;
private String description;
private String spendTime;
private String status;
private List<String> log;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassName() {

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。