您好,登錄后才能下訂單哦!
效果圖:
聲明:是在長輩下的代碼修改
代碼:
package com.rpoter;
import java.io.File;
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 org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
public class TestReport extends TestListenerAdapter {
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 = 22;
/** 失敗 */
public int Failed = 10;
/** 跳過 */
public int Skipped = 2;
//public String reportName = formateDate() + ".html";
//File htmlReportDir = new File("test-output/customizeHtml-report");
public void onStart(ITestContext context ) {
File htmlReportDir = new File("test-output/Test-report");
if (!htmlReportDir.exists()) {
htmlReportDir.mkdirs();
}
String reportName = formateDate() +"index"+".html";
reportPath = htmlReportDir + "/" + reportName;
File report = new File(htmlReportDir, reportName);
if (report.exists()) {
try {
report.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
StringBuilder sb = new StringBuilder();
String val = "<!DOCTYPE html PUBLIC \"-//W3C//DTDXHTML1.0Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\"><head><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
+ "<style>table {border-collapse: collapse;}.table1 tr th {color:DarkGreen;background-color: AntiqueWhite;} </style>"
+ "<title >UI自動化測試報告</title></head><body style=\"background-color:#FAEBD7;\">"
+ "<div id=\"top\" align=\"center\"><p style=\"font-weight:bold;\"><h3>UI自動化測試用例運行結果列表</h3></p>"
+ "<table border=\"1\" width=\"90%\" height=\"66\" table1>" + "<tr>" + "<th>手機型號</th>" + "<th>測試包名</th>"
+ "<th>版本號</th>" + "<th>手機系統版本</th>" + "<th>通過</th>" + "<th>失敗</th>" + "<th>跳過</th>" + "</tr>" + "<tr>"
+ "<td>" + Mobile_phone + "</td>" + "<td>" + package_name + "</td>" + "<td>" + versionName + "</td>"
+ "<td>" + MobileSystem + "</td>" + "<td>" + Passed + "</td>" + "<td>" + Failed + "</td>" + "<td>"
+ Skipped + "</td>" + "</tr>" + " </table>"
+ "<tbody style=\"word-wrap:break-word;font-weight:bold;\" align=\"center\"><h3>詳 情</h3>";
sb.append(val);
String Detailed_Situation = "<table width=\"90%\" height=\"80\" border=\"1\" align=\"center\" cellspacing=\"0\" rules=\"all\" style=\"table-layout:relative;\">"
+ "<thead>" + "<th>用例序列號</th>" + "<th>用例模塊</th>" + "<th>失敗原因</th>" + "<th>結果</th>" + "<th>截圖</th>"
+ "</thead>";
sb.append(Detailed_Situation);
String res = sb.toString();
try {
Files.write((Paths.get(reportPath)), res.getBytes("utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
/** 通過 */
@Override
public void onTestSuccess(ITestResult result) {
StringBuilder sb = new StringBuilder("<tr><td>通過序列號-</td><td>111");
sb.append("序列號");
sb.append(result.getMethod().getRealClass() + "." + result.getMethod().getMethodName());
sb.append("</td><td>該用例測試通過</td><td><font color=\"#00FF00\">Passed</font></td></td><td>通過沒有截圖");
sb.append(result.getMethod().getSuccessPercentage());
sb.append("%通過沒有截圖</tr>");
String res = sb.toString();
try {
Files.write((Paths.get(reportPath)), res.getBytes("utf-8"), StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
}
/** 失敗 */
@Override
public void onTestFailure(ITestResult result) {
StringBuilder sb = new StringBuilder("<tr><td>跳過序列號1</td><td>");
sb.append(result.getMethod().getRealClass() + "." + result.getMethod().getMethodName());
sb.append("</td><td>");
sb.append("<p align=\"left\">測試用例執行<font color=\"red\">失敗</font>,原因:<br>");
Throwable throwable1 = result.getThrowable();
sb.append(throwable1.getMessage());
sb.append("<br><a style=\"background-color:#CCCCCC;\">");
sb.append("B</td><td><font color=\"red\">Failed</font></td><td><br>");
/** 堆棧信息單元測試需要 */
/*
* sb.append("<br><br>"); StackTraceElement[] se =
* throwable.getStackTrace(); sb.append("堆棧信息:"); sb.append("<br>"); for
* (StackTraceElement e : se) { sb.append(e.toString());
* sb.append("<br>"); }
*/
sb.append("</a></p></td></tr>");
String res = sb.toString();
try {
Files.write((Paths.get(reportPath)), res.getBytes("utf-8"), StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
}
/** 跳過 */
@Override
public void onTestSkipped(ITestResult result) {
StringBuilder sb = new StringBuilder("<tr><td>跳過序列號1</td><td>");
sb.append(result.getMethod().getRealClass() + "." + result.getMethod().getMethodName());
sb.append("</td><td>");
sb.append("<p align=\"left\">測試用例<font color=\"red\">跳過</font>,原因:<br>");
Throwable throwable1 = result.getThrowable();
sb.append(throwable1.getMessage());
sb.append("B</td><td><font color=\"#FFA500\">Skipped</font></td><td>");
sb.append("<br><a style=\"background-color:#CCCCCC;\">");
sb.append("</a></p>跳過注釋</td></tr>");
String res = sb.toString();
try {
Files.write((Paths.get(reportPath)), res.getBytes("utf-8"), StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFinish(ITestContext testContext) {
StringBuilder sb = new StringBuilder("</tbody></table><a href=\"#top\">自動化測試部</a></div></body>");
sb.append("</html>");
String msg = sb.toString();
try {
Files.write((Paths.get(reportPath)), msg.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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。