您好,登錄后才能下訂單哦!
自定義報告學習筆記一
效果圖:
在長輩的代碼下,自己修改:
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。