转载

ReportNG测试报告模板定制

部分参考:http://tech.it168.com/a2013/0906/1530/000001530755_3.shtml

ReportNG提供了简单的方式来查看测试结果,并能对结果进行着色,还可以通过修改模板定制化内容,修改CSS来替换默认的输出样式等。为了使用ReportNG,首先我们要引入reportng-1.1.4.jar和velocity-dep-1.4.jar,或者直接导入其源代码,进行定制化。

一.增加项目名称、Android设备信息等数据。

在ReportMetadata.java文件增加以下内容(获得Android设备id和名称):

 1     /**  2      *  get Device UDID  3      */  4     public String getDeviceUdid() {  5         List<String> list = getDeviceInfo();  6         String[] split = {"", "", ""};  7         for (int i = 0; i < list.size() - 1; i++) {  8             if (list.get(i).contains("model:")) {  9                 split = list.get(i).split(" ");        10             }        11         } 12  13         return split[0]; 14     } 15  16     /** 17      *  get Device Name 18      */ 19     public String getDeviceName() { 20         List<String> list = getDeviceInfo(); 21         int index = 0; 22         String[] split = {"", "", ""}; 23          24         for (int i = 0; i < list.size() - 1; i++) { 25             if (list.get(i).contains("model:")) { 26                 index = list.get(i).indexOf("model:"); 27                 split = list.get(i).substring(index + 6).split(" ");         28             }        29         } 30  31         return split[0]; 32     }     33  34     public List<String> getDeviceInfo() { 35         List<String> list = new LinkedList<String>(); 36         String line = "";   37         String command = "cmd /c adb devices -l"; 38         try { 39             Process process = Runtime.getRuntime().exec(command); 40             BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));                 41             while ((line = reader.readLine()) != null) { 42                 list.add(line.toString()); 43             } 44             process.waitFor(); 45           46         } catch (IOException | InterruptedException e) { 47             e.getMessage(); 48         } 49          50         return list; 51     } 52

项目名称和服务器信息存储到xml文件中,这里继续在ReportMetadata.java文件读取xml:

/**  * 读取xml文件  * @param regInfoPath string 文件路径  */  private static Map<String, String> parserXml(String regInfoPath) {  Map<String, String> temp = new LinkedHashMap<String, String>();  String path = System.getProperty("user.dir");  path += regInfoPath;  File inputXml = new File(path);  SAXReader saxReader = new SAXReader();   try {    Document document = saxReader.read(inputXml);   //把文件读入到文档    Element employees = document.getRootElement();   //获取文档根节点      for(Iterator<?> i = employees.elementIterator(); i.hasNext();) {        Element employee = (Element) i.next();       for(Iterator<?> j = employee.elementIterator(); j.hasNext();) {      Element node = (Element) j.next();           String key = node.getName();     String value = node.getText();     temp.put(key, value);    }   }       } catch (DocumentException e) {    System.out.println(e.getMessage());   }   return temp; } /**  * 返回节点value  * @param key  * @return  */ public static String getXmlValue(String key) {    Map<String, String> init = parserXml("//data//init.xml");  return init.get(key); }  public static String getServer() {  return getXmlValue("project"); } public static String getXmlSetting() {  String temp = "";  if (getXmlValue("switch").equals("1")) {   return "线上环境";  } else if (getXmlValue("switch").equals("2")) {   return "测试环境";  }  return temp; } 

到模板属性文件reportng.properties里添加模板属性:

project=Project: setting=Server: deviceUdid=Device Udid: deviceName=Device Name: createTime=Create Time:

然后到模板输出文件overview.html.vm里修改显示内容,meta为类对象,后面为调用方法:

 1 <table id="info" align="right">    2   <tr>  3     <td><b>$messages.getString("project")</b> $meta.server</td>  4   </tr>  5   <tr>  6     <td><b>$messages.getString("setting")</b> $meta.xmlSetting</td>  7   </tr>         8   <tr>  9     <td><b>$messages.getString("deviceName")</b> $meta.deviceName</td> 10   </tr> 11   <tr> 12     <td><b>$messages.getString("deviceUdid")</b> $meta.deviceUdid </td> 13   </tr> 14   <tr> 15     <td><b>$messages.getString("createTime")</b> $meta.reportDate $meta.reportTime</td> 16   </tr> 17 </table>

二.修改模板中执行方法排序问题

模板中的方法排序是以字母顺序排序的,这里想要改成以方法的执行顺序排序,到TestResultComparator.java文件中修改以下位置:

 1     public int compare(ITestResult result1, ITestResult result2)  2     {  3         int longresult2 = 0;  4         if (result1.getStartMillis() < result2.getStartMillis()) {  5             longresult2 = -1;  6         } else {  7             longresult2 = 1;  8         }  9  10         return longresult2; 11 //        return result1.getName().compareTo(result2.getName()); 12     }

三.模板中增加Case异常截图,暂未研究。。

ReportNG测试报告修改后如图:

ReportNG测试报告模板定制

正文到此结束
Loading...