一般我们用的testng自带的报告太low了

那现在我们准备输出一份比较美观的报告,

1,先从网上下载个模板和几个jar包,https://download.csdn.net/download/qq_36379597/11839533

解压

把三个jar包引用进来,再把template复制到项目根目录下

2添加maven文件 到pom.xml

 <!-- report  --><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.8.6</version></dependency><dependency><groupId>org.uncommons</groupId><artifactId>reportng</artifactId><version>1.1.4</version><scope>test</scope></dependency>

3创建报告类

package seleniumUtil;import org.testng.IReporter;import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;import org.testng.IReporter;
import org.testng.IResultMap;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.xml.XmlSuite;import com.google.gson.Gson;
import com.google.gson.GsonBuilder;public class TestCaseReport implements IReporter {private long currentTime = System.currentTimeMillis();private SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年-MM月-dd日-HH时mm分ss秒");private Date date = new Date(currentTime);private String reportdate = formatter.format(date);private String path = System.getProperty("user.dir")+File.separator+reportdate+"report.html";private String templatePath = System.getProperty("user.dir")+File.separator+"template.html";private int testsPass = 0;private int testsFail = 0;private int testsSkip = 0;private String beginTime;private long totalTime;private String name = "Selenium 测试报告";/***public ZTestReport(){SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmssSSS");name = formatter.format(System.currentTimeMillis());}public ZTestReport(String name){this.name = name;if(this.name==null){SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmssSSS");this.name = formatter.format(System.currentTimeMillis());}}*/public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {List<ITestResult> list = new ArrayList<ITestResult>();for (ISuite suite : suites) {Map<String, ISuiteResult> suiteResults = suite.getResults();for (ISuiteResult suiteResult : suiteResults.values()) {ITestContext testContext = suiteResult.getTestContext();IResultMap passedTests = testContext.getPassedTests();testsPass = testsPass + passedTests.size();IResultMap failedTests = testContext.getFailedTests();testsFail = testsFail + failedTests.size();IResultMap skippedTests = testContext.getSkippedTests();testsSkip = testsSkip + skippedTests.size();IResultMap failedConfig = testContext.getFailedConfigurations();list.addAll(this.listTestResult(passedTests));list.addAll(this.listTestResult(failedTests));list.addAll(this.listTestResult(skippedTests));list.addAll(this.listTestResult(failedConfig));}}this.sort(list);this.outputResult(list);}private ArrayList<ITestResult> listTestResult(IResultMap resultMap) {Set<ITestResult> results = resultMap.getAllResults();return new ArrayList<ITestResult>(results);}private void sort(List<ITestResult> list) {Collections.sort(list, new Comparator<ITestResult>() {public int compare(ITestResult r1, ITestResult r2) {if (r1.getStartMillis() > r2.getStartMillis()) {return 1;} else {return -1;}}});}private void outputResult(List<ITestResult> list) {try {List<ReportInfo> listInfo = new ArrayList<ReportInfo>();int index = 0;for (ITestResult result : list) {String tn = result.getTestContext().getCurrentXmlTest().getParameter("testCase");if(index==0){SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSS");beginTime = formatter.format(new Date(result.getStartMillis()));index++;}long spendTime = result.getEndMillis() - result.getStartMillis();totalTime += spendTime;String status = this.getStatus(result.getStatus());List<String> log = Reporter.getOutput(result);for (int i = 0; i < log.size(); i++) {log.set(i, log.get(i).replaceAll("\"", "\\\\\""));}Throwable throwable = result.getThrowable();if(throwable!=null){log.add(throwable.toString().replaceAll("\"", "\\\\\""));StackTraceElement[] st = throwable.getStackTrace();for (StackTraceElement stackTraceElement : st) {log.add(("    " + stackTraceElement).replaceAll("\"", "\\\\\""));}}ReportInfo info = new ReportInfo();info.setName(tn);info.setSpendTime(spendTime+"ms");info.setStatus(status);info.setClassName(result.getInstanceName());info.setMethodName(result.getName());info.setDescription(result.getMethod().getDescription());info.setLog(log);listInfo.add(info);}Map<String, Object> result = new HashMap<String, Object>();result.put("testName", name);result.put("testPass", testsPass);result.put("testFail", testsFail);result.put("testSkip", testsSkip);result.put("testAll", testsPass+testsFail+testsSkip);result.put("beginTime", beginTime);result.put("totalTime", totalTime+"ms");result.put("testResult", listInfo);Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();String template = this.read(templatePath);//BufferedWriter output = new BufferedWriter();//output.write(new String(s.getBytes("gbk"),"utf-8"));//BufferedWriter output = new BufferedWriter(new FileWriter(path));BufferedWriter output = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(new File(path)),"utf-8"));template = template.replaceFirst("\\$\\{resultData\\}", Matcher.quoteReplacement(gson.toJson(result)));output.write(template);output.flush();output.close();} catch (IOException e) {e.printStackTrace();}}private String getStatus(int status) {String statusString = null;switch (status) {case 1:statusString = "成功";break;case 2:statusString = "失败";break;case 3:statusString = "跳过";break;default:break;}return statusString;}public static class ReportInfo {private String name;private String className;private String methodName;private String description;private String spendTime;private String status;private List<String> log;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getClassName() {return className;}public void setClassName(String className) {this.className = className;}public String getMethodName() {return methodName;}public void setMethodName(String methodName) {this.methodName = methodName;}public String getSpendTime() {return spendTime;}public void setSpendTime(String spendTime) {this.spendTime = spendTime;}public String getStatus() {return status;}public void setStatus(String status) {this.status = status;}public List<String> getLog() {return log;}public void setLog(List<String> log) {this.log = log;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}}private String read(String path) {File file = new File(path);InputStream is = null;StringBuffer sb = new StringBuffer();try {is = new FileInputStream(file);int index = 0;byte[] b = new byte[1024];while ((index = is.read(b)) != -1) {sb.append(new String(b, 0, index));}return sb.toString();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (is != null) {is.close();}} catch (IOException e) {e.printStackTrace();}}return null;}}

4修改testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<!-- 测试报告主要添加下面三个监听 --><listeners><listener class-name="org.uncommons.reportng.HTMLReporter"></listener><listener class-name="org.uncommons.reportng.JUnitXMLReporter"></listener><listener class-name="seleniumUtil.TestCaseReport"></listener>
</listeners> <test thread-count="5" name="Test"><!-- 这里写上自己的测试类 --></test> <!-- Test -->
</suite> <!-- Suite -->

<listener class-name="seleniumUtil.TestCaseReport"></listener> 是自己创建的监听类的包名.类名

5修改eclipse的workplace空间的编码格式

完成,运行testng.xml

刷新项目

出现效果图

selenium java 高级技巧篇(必学)美化测试报告(十三)相关推荐

  1. 【笔记-java】java工程师-入门必学

    路径 目录 子目录 重点 课程名 备注 java工程师 入门必学 语法基础 环境搭建.开发工具使用.基础语法 java入门第一季 面向对象 继承.封装.多态 java入门第二季 常用工具类 异常.字符 ...

  2. Java程序猿必学第十一篇——接口

    //1.接口//1.1 接口概述//接口: 特殊的抽象类,使用方式及组成部分都与抽象类类似 //语法: interface implements //注意: 在接口中只能定义公开的静态常量及公开的抽象 ...

  3. Java程序猿必学第二十二篇—— 网络编程

    //1.File相关 //1.1 FileFilter //FileFilter:文件过滤器 //listFiles方法用于将当前层的文件和目录的File对象放入数组 //FileFilter则是在放 ...

  4. java程序员必学_Java入门基础学习,成为一个Java程序员的必备知识

    引言 众所周知,Java是一种面向对象的编程语言.您可以在Windows操作系统上编写Java源代码,而在Linux操作系统上运行编译后的字节码,而无需修改源代码. 数据类型 Java 有 2 种数据 ...

  5. 来电通java版_终于有人把Java程序员必学知识点整理出来了,令人有如醍醐灌顶...

    JVM 无论什么级别的Java从业者,JVM都是进阶时必须迈过的坎.不管是工作还是面试中,JVM都是必考题.如果不懂JVM的话,薪酬会非常吃亏(近70%的面试者挂在JVM上了) 详细介绍了JVM有关于 ...

  6. 终于有人把Java程序员必学知识点整理出来了,令人有如醍醐灌顶

    JVM 无论什么级别的Java从业者,JVM都是进阶时必须迈过的坎.不管是工作还是面试中,JVM都是必考题.如果不懂JVM的话,薪酬会非常吃亏(近70%的面试者挂在JVM上了) 详细介绍了JVM有关于 ...

  7. wpf创建xml随程序一起打包_Springboot,Java程序员必学

    1. Spring boot是Spring家族中的一个全新的框架,它用来简化Spring应用程序的创建和开发过程,也可以说Spring boot能简化我们之前采用SpringMVC + Spring ...

  8. java基础案例教程课后答案,终于有人把Java程序员必学知识点全整理出来了

    前言 微架构的出现,很好地适应了这个时代对快速发展变化的要求.它不再提倡一体化的项目设计,而是对项目进行有效的"业务区"(可以简单理解为不同的子系统〉划分,并利用合理的技术对业务性 ...

  9. java在容器中导入图片_Java程序员必学技术:@Import直接导入类,在容器@Configuration、@Component中是怎么直接导入类注册到容器的?...

    Java程序员必学技术:@Import直接导入类,在容器@Configuration.@Component中是怎么直接导入类注册到容器的? Spring IoC 容器是一个管理 Bean 的容器,在 ...

最新文章

  1. python异常处理_Python入门 断言与异常处理
  2. oracle sde 安装失败,sde无法安装案例
  3. Android背景透明的 Dialog
  4. 第五章 代码重用与函数编写(1)
  5. 机器学习- 吴恩达Andrew Ng Week1 知识总结 Introduciton
  6. intersystem-M语言基础语法
  7. 解决在使用Java API操作HBase中出现的Could not locate executable null\bin\winutils.exe in the Hadoop binaries.错误
  8. Chrome谷歌浏览器安装crx后缀插件方法
  9. Django之 Timezone 详解
  10. 点击自定义按钮弹出百度商桥对话框
  11. 固定table首行或尾行
  12. Python调用Gurobi:Assignment Problem(指派问题)简单案例
  13. 网页设计配色应用实例剖析——黄色系
  14. [转]倾斜摄影单体化实现方案
  15. 关关难过关关过——编译edm
  16. 错误: Failed to install 'unknown package' from GitHub: schannel: failed to receive handshake, SSL/TL
  17. 给 iOS 开发者的 Sketch 入门教程
  18. 实习日志 - 第二天
  19. java word 模板_java通过word模板生成word文档
  20. 期末作业C#实现学生宿舍管理系统

热门文章

  1. DAO层的异常处理模式
  2. java计算机毕业设计基于web旅游网站的设计与实现源程序+mysql+系统+lw文档+远程调试
  3. python基于PHP+MySQL的综合排课系统
  4. SpringBoot允许跨域请求
  5. service连接泄露异常:Activity MainActivity has leaked ServiceConnection MainActivity解决方法
  6. 长春BI工具- 长春商业智能- 亿信华辰排第几?
  7. 大学生兼职管理平台-JAVA【数据库设计、源码、开题报告】
  8. Redis分布式锁背后的原理
  9. jointJS 获取path数据
  10. 中国硬分币价格表参考