您好,登錄后才能下訂單哦!
效果圖:
代碼:
package com.rpoter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
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;
/** 手機(jī)型號 */
private String Mobile_phone = "小米手機(jī)";
/** 測試包名 */
private String package_name = "com.systoon.beijingtoon";
/** 版本號 */
private String versionName = "1.8.1";
/** 手機(jī)系統(tǒng)版本 */
private String MobileSystem = "安卓5.6";
/** 通過 */
public int Passed = 0;
/** 失敗 */
public int Failed = 0;
/** 跳過 */
public int Skipped = 0;
/**用例總共合計(jì)*/
public int CountNum = 0;
/**通過結(jié)果*/
public String P***esuTmp,PassgetName;
/**開始時(shí)間*/
public Date StartDate ;
/**結(jié)束時(shí)間*/
public Date EndDate ;
@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自動化測試用例運(yùn)行結(jié)果列表";
sb.append("<html>\n<head><title>UI自動化測試報(bào)告</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 cellspacing='0' cellpadding='0' border='1' width='90%'>")
.append("<tr><th>手機(jī)品牌名稱</th><th>手機(jī)系統(tǒng)版本</th><th>測試包名</th><th>版本號</th><th>通過數(shù)</th><th>失敗數(shù)</th><th>跳過</th><th>合計(jì) </th><th>開始時(shí)間 </th><th>結(jié)束時(shí)間 </th></tr>\n");
String res = sb.toString();
try {
Files.write((Paths.get(reportPath)), res.getBytes("utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
StringBuilder sb1 = new StringBuilder();
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());
StartDate = overview.getStartDate();
System.err.println("End Time: "+overview.getEndDate());
EndDate = overview.getEndDate();
CountNum = overview.getAllTestMethods().length;
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 = "/"+formateDate() +"index"+".html";
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>"+CountNum+"</td><td>"+StartDate+"</td><td>"+EndDate+"</td>\n");
sb1.append("</table><br/>")
.append("<pre></pre>")
.append("<table cellspacing='0' cellpadding='0' border='0' width='90%'>")
.append("<tr><th>"+"執(zhí)行用例數(shù):"+CountNum+"</th></tr>")
.append("</table>")
.append("<tbody style=\"word-wrap:break-word;font-weight:bold;\" align=\"center\"><h3>詳 情</h3>")
.append("<table cellspacing='0' cellpadding='0' border='1' width='90%'>")
.append("<tr><th>序列號 </th><th>狀 態(tài)</th><th>用例類名</th><th>詳情信息</th><th>截圖</th></tr>\n");
/**詳細(xì)通過數(shù)*/
for (ISuite suite : suites) {
Map<String, ISuiteResult> tests = suite.getResults();
for (ISuiteResult r : tests.values()) {
ITestContext overview = r.getTestContext();
CountNum = overview.getAllTestMethods().length;
Set<ITestResult> passedSet = overview.getPassedTests().getAllResults();
int i=0;
for(ITestResult p : passedSet){
System.err.println("class: " +p.getTestClass().getName() + " | method: "+p.getName());
P***esuTmp = p.getTestClass().getName();
PassgetName =p.getName();
sb1.append("<tr><td>"+i+"</td><td><font color=\"#00FF00\">通 過</font></td><td>"+P***esuTmp+"</td><td>"+"詳情信息:"+PassgetName+"</td><td><font color=\"#00FF00\">通過數(shù)無需截圖</font></td></tr>");
i++;
}
}
}
sb1.append("<tr><th>序列號 </th><th>狀 態(tài)</th><th>用例類名</th><th>詳情信息</th><th>截圖</th></tr>\n");
/**詳細(xì)失敗數(shù)*/
for (ISuite suite : suites) {
Map<String, ISuiteResult> tests = suite.getResults();
for (ISuiteResult r : tests.values()) {
ITestContext overview = r.getTestContext();
CountNum = overview.getAllTestMethods().length;
int i=0;
Set<ITestResult> failedSet = overview.getFailedTests().getAllResults();
for(ITestResult f : failedSet){
System.err.println("class: " +f.getTestClass().getName()+ " | method: "+f.getName() + " | error: "+ f.getThrowable());
sb1.append("<tr><td>"+i+"</td><td><font color=\"FF0000\">失敗</font></td><td>"+f.getTestClass().getName()+"</td><td>"+"測試方法名: "+f.getName() + " 失敗詳情: "+ f.getThrowable()+"</td><td>截圖路徑:</td></tr>");
i++;
}
}
}
sb1.append("<tr><th>序列號 </th><th>狀 態(tài)</th><th>用例類名</th><th>詳情信息</th><th>截圖</th></tr>\n");
/**詳細(xì)跳過數(shù)*/
for (ISuite suite : suites) {
Map<String, ISuiteResult> tests = suite.getResults();
for (ISuiteResult r : tests.values()) {
ITestContext overview = r.getTestContext();
CountNum = overview.getAllTestMethods().length;
Set<ITestResult> skippedSet = overview.getSkippedTests().getAllResults();
int i=0;
for(ITestResult s : skippedSet){
System.err.println("---- " +s.getName() );
System.err.println(s.getThrowable());
sb1.append("<tr><td>"+i+"</td><td><font color=\"FF9966\">跳過</font></td><td>"+s.getName()+"</td><td>"+"詳情信息:"+s.getThrowable()+"</td><td><font color=\"FF9966\">該用例跳過沒有截圖</font></td></tr>");
i++;
}
}
}
sb1.append("</table><br/>");
sb1.append("</tbody></table><a href=\"#top\">自動化測試部</a></div></body></html>\n");
String res1 = sb1.toString();
try {
Files.write((Paths.get(reportPath)), res1.getBytes("utf-8"), StandardOpenOption.APPEND);
FileWriter writer = new FileWriter(".//test-output//results.txt", false);
writer.write(Passed+":");
writer.write(Failed+":");
writer.write(Skipped+"");
writer.close();
} 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);
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。