您好,登录后才能下订单哦!
自定义报告学习笔记一
效果图:
在长辈的代码下,自己修改:
package com.rpoter;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.reporters.HtmlHelper;
import org.testng.xml.XmlSuite;
public class Reporter implements IReporter {
private String reportPath;
/** 手机型号 */
private String Mobile_phone = "小米手机";
/** 测试包名 */
private String package_name = "com.systoon.beijingtoon";
/** 版本号 */
private String versionName = "1.8.1";
/** 手机系统版本 */
private String MobileSystem = "安卓5.6";
/** 通过 */
public int Passed = 0;
/** 失败 */
public int Failed = 0;
/** 跳过 */
public int Skipped = 0;
@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
File htmlReportDir = new File("test-output/Test-report");
if (!htmlReportDir.exists()) {
htmlReportDir.mkdirs();
}
String reportName = formateDate() + ".html";
reportPath = htmlReportDir + "/" + reportName;
File report = new File(htmlReportDir, reportName);
if (report.exists()) {
try {
report.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
StringBuffer sb = new StringBuffer();
String title = "UI自动化测试用例运行结果列表";
sb.append("<html>\n<head><title>UI自动化测试报告</title>")
.append(HtmlHelper.getCssString("."))
.append("</head><body style=\"background-color:#FAEBD7;\">\n")
.append("<h3><p align='center'>").append(title).append("</p></h3>\n")
.append("<table border='1' width='90%'>")
.append("<tr><th>手机品牌名称</th><th>手机系统版本</th><th>测试包名</th><th>版本号</th><th>通过数</th><th>失败数</th><th>跳过</th><th>详情 </th></tr>\n");
String res = sb.toString();
try {
Files.write((Paths.get(reportPath)), res.getBytes("utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
for (ISuite suite : suites) {
Map<String, ISuiteResult> tests = suite.getResults();
for (ISuiteResult r : tests.values()) {
ITestContext overview = r.getTestContext();
System.err.println("suite: " + overview.getName());
System.err.println("Stard Time: "+overview.getStartDate());
System.err.println("End Time: "+overview.getEndDate());
System.err.println("all methods num : "+overview.getAllTestMethods().length);
//passed
Passed = overview.getPassedTests().size();
System.err.println("passed: " + overview.getPassedTests().size());
Set<ITestResult> passedSet = overview.getPassedTests().getAllResults();
for(ITestResult p : passedSet){
System.err.println("class: " +p.getTestClass().getName() + " | method: "+p.getName());
}
//failed
Failed = overview.getFailedTests().size();
System.err.println("failed: " + overview.getFailedTests().size());
Set<ITestResult> failedSet = overview.getFailedTests().getAllResults();
for(ITestResult f : failedSet){
System.err.println("class: " +f.getTestClass().getName()+ " | method: "+f.getName() + " | error: "+ f.getThrowable());
}
//skipped
Skipped = overview.getSkippedTests().size();
System.err.println("skipped: " + overview.getSkippedTests().size());
Set<ITestResult> skippedSet = overview.getSkippedTests().getAllResults();
for(ITestResult s : skippedSet){
System.err.println("---- " +s.getName() );
System.err.println(s.getThrowable());
}
System.err.println("==================================");
}
}
String reportN = "file:///D:/TOOL/JAVAspace/JmeterTest/"+htmlReportDir+"/"+formateDate() +"index"+".html";
StringBuilder sb1 = new StringBuilder();
sb1.append("<td>"+Mobile_phone+"</td><td>"+MobileSystem+"</td><td>"+versionName+"</td><td>"+package_name+"</td>");
sb1.append("<td>"+Passed+"</td><td>"+Failed+"</td><td>"+Skipped+"</td><td>链接地址为: "+reportN+"");
sb1.append("<a href='").append(reportN).append("</a></td>\n");
sb1.append("</table><br/>");
sb1.append("</body></html>\n");
String res1 = sb1.toString();
try {
Files.write((Paths.get(reportPath)), res1.getBytes("utf-8"), StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String formateDate() {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
return sf.format(date);
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。