效果图:
声明:是在长辈下的代码修改
代码:
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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。