Java和.Net版通用工具类实现--生成自定义Web Html/Excel测试用例和测试报告

本文介绍了一种使用Java和C#实现的自动化测试工具,能够为WEB功能测试和WebServiceJson接口测试生成统一格式的测试用例及测试报告,并支持自定义的WebHtml/Excel输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、背景概述

工作中接触到不同的项目测试需求,其中一个是对WEB功能进行测试,采用了Selenium+Junit+Maven+SVN,另一个是对Web Service Json接口进行测试,自己使用C#写了一个自动化测试工具。我希望两者都能生成一样标准格式的测试用例和测试报告,既能展示在Web站点又能得到Excel,于是分别用Java和C#实现了这个工具类--生成自定义Web Html/Excel(CSV)测试用例和测试报告。

 

二、 Html模板设计

 需要替换或追加的内容用变量标示,Html格式自定义,工具类中直接对文本内容进行替换或追加。

 

两个模板文件ListSample.htm和DetailSample.htm内容分别为:

  1. <html>  
  2. <head>  
  3. <title>$ProjectName Test Report</title>  
  4. </head>  
  5. <body>  
  6. <h1>$ProjectName Test Report</h1>  
  7. <table border="1" cellspacing="1" cellpadding="8" style="border: #000000; border-style: solid; border-width: 1px">  
  8. <tr><td align="right" style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;" colspan="1">Project Name: </td><td colspan="3">$ProjectName</td></tr>  
  9. <tr><td align="right" style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;">Total: </td><td width="100px">$Total</td>  
  10. <td align="right" style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;">Http Path: </td><td><a href="$HttpPath">$HttpPath</a></td></tr>  
  11. <tr><td align="right" style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;">Passed: </td><td style="color:green;font-weight:bold;">$Passed</td>  
  12. <td align="right" style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;">Code Path: </td><td><a href="$ScriptPath">$ScriptPath</a></td></tr>  
  13. <tr><td align="right" style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;">Failed: </td><td style="color:red;font-weight:bold;">$Failed</td>  
  14. <td align="right" style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;">Test Date: </td><td>$TestDate</td></tr>  
  15. <tr><td align="right" style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;" colspan="1">Summary: </td><td colspan="3">$Summary</td></tr>  
  16. </table>  
  17. <br />  
  18. <table width="90%" border="1" cellspacing="1" cellpadding="8" style="table-layout:fixed;border: #000000; border-style: solid; border-width: 1px ">  
  19. <tr style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;">  
  20. <td width="15%">CaseID</td>  
  21. <td width="15%"> TaskName </td>  
  22. <td width="10%"> TestTime </td>  
  23. <td> TestSummary</td>  
  24. <td width="10%"> TestResult</td>  
  25. <td width="15%"> Comments</td></tr>  
  26. <!--<tr><td>$CaseID</td><td>$TaskName</td><td>$TestTime</td><td>$TestSummary</td><td style="font-weight:bold;"><a href="$href"><font color="$color">$TestResult</font></a></td><td>$Comments</td></tr>-->  

其中:

  1. <tr><td>$CaseID</td><td>$TaskName</td><td>$TestTime</td><td>$TestSummary</td><td style="font-weight:bold;"><a href="$href"><font color="$color">$TestResult</font></a></td><td>$Comments</td></tr>  

为需要替换追加的内容。 

  1. <html>  
  2. <head>  
  3. <title>$TaskName Test Details</title>  
  4. </head>  
  5. <body>  
  6. <h1>$TaskName Test Details</h1>  
  7. <h3><a href="$href">[[Return>>]]</a></h3>  
  8. <table border="1" cellspacing="1" cellpadding="8" style="border: #000000; border-style: solid; border-width: 1px ">  
  9. <tr><td align="right" style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;">Task Name: </td><td>$TaskName</td></tr>  
  10. <tr><td align="right" style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;">Test Time: </td><td>$TestTime</td></tr>  
  11. <tr><td align="right" style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;">Test Summary: </td><td>$TestSummary </td></tr>  
  12. <tr><td align="right" style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;">Test Result: </td><td style="color:$color;font-weight:bold;">$TestResult</td></tr>  
  13. <tr><td align="right" style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;">Comments: </td><td>$Comments</td></tr>  
  14. </table>  
  15. <br />  
  16. <table width="90%" border="1" cellspacing="1" cellpadding="8" style="table-layout:fixed;border: #000000; border-style: solid; border-width: 1px ">  
  17. <tr style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;"><td>CaseID</td></tr>  
  18. <tr><td>$CaseID</td></tr>  
  19. <tr style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;"><td>TaskName </td></tr>  
  20. <tr><td>$TaskName</td></tr>  
  21. <tr style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;"><td> Precondition </td></tr>  
  22. <tr><td>$Precondition</td></tr>  
  23. <tr style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;"><td> Steps</td></tr>  
  24. <tr><td>$Steps</td></tr>  
  25. <tr style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;"><td> Expects</td></tr>  
  26. <tr><td>$Expects</td></tr>  
  27. <tr style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;"><td> Results</td></tr>  
  28. <tr><td>$Results</td></tr>  
  29. <tr style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;"><td> TestResult</td></tr>  
  30. <tr><td style="color:$color;font-weight:bold;">$TestResult</td></tr>  
  31. <tr style="BACKGROUND-COLOR:#333399;COLOR:white;font-weight:bold;"><td> Remarks</td></tr>  
  32. <tr><td>$Remarks</td></tr>  
  33. </table></body>  
  34. </html>  

  

三、Java实现

SnapShot类(网页截图):

  1. package com.nhn.platform.qa.cwmtest.Utils;  
  2. import java.awt.Dimension;  
  3. import java.awt.Rectangle;  
  4. import java.awt.Robot;  
  5. import java.awt.Toolkit;  
  6. import java.awt.image.BufferedImage;  
  7. import java.io.File;  
  8. import java.io.IOException;  
  9.   
  10. import javax.imageio.ImageIO;  
  11.   
  12. import org.apache.commons.io.FileUtils;  
  13. import org.openqa.selenium.TakesScreenshot;  
  14. import org.openqa.selenium.WebDriver;  
  15. import org.openqa.selenium.OutputType;  
  16.   
  17. public class SnapShot {  
  18.   
  19.     static boolean __Debug = false;  
  20.   
  21.     static String imageFormat = "png"; // image format  
  22.     static int serialNum = 0;  
  23.     static Dimension d = Toolkit.getDefaultToolkit().getScreenSize();  
  24.   
  25.     static void Initilize() {  
  26.         serialNum = 0;  
  27.     }  
  28.   
  29.     /****************************  
  30.      * snapShot the whole screen *  
  31.      ****************************/  
  32.     public static void screenShoot(String dirPath,String picName,String htmlPath) {  
  33.   
  34.         try {  
  35.             serialNum++;  
  36.             // copy a screen shoot capture to a BufferedImage object screens  
  37.             // hoot  
  38.             BufferedImage screenShoot = (new Robot()).createScreenCapture(new Rectangle(0, 0, (int) d.getWidth(), (int) d.getHeight()));  
  39.             String picPathName = dirPath + "\\images\\" + picName;  
  40.             File dir=new File(dirPath + "\\images");  
  41.             if(!dir.exists()){  
  42.             dir.mkdirs();  
  43.             }  
  44.             File f = new File(picPathName);  
  45.             if (__Debug) {  
  46.                 System.out.print("Save File " + picName);  
  47.             }  
  48.             // Write to file  
  49.             ImageIO.write(screenShoot, imageFormat, f);  
  50.   
  51.             if (__Debug) {  
  52.                 System.out.print("..Finished!\n");  
  53.             }  
  54.   
  55.             appendSnapShotToLogFile("images/"+picName,htmlPath);  
  56.         } catch (Exception ex) {  
  57.             System.out.println(ex);  
  58.         }  
  59.     }  
  60.   
  61.     public static void appendSnapShotToLogFile(String imageName,String htmlPath) {  
  62.         String content = "";  
  63.         content += "<table width=\"90%\"><tr><td>\n";  
  64.         //content += "<img src=\"" + imageName + "\" width=\"" + EtcIO.logPicWidth + "\" height=\"" + EtcIO.logPicHeight + "\" onclick=\"showPic(this);\" />\n";  
  65.         content += "<img src=\"" + imageName + "\" />\n";  
  66.         content += "</td></tr></table>\n";  
  67.   
  68.         EtcIO.AppendContent(htmlPath, content);  
  69.     }  
  70.     public static void appendSnapShot(WebDriver driver,String dirPath,String picName,String htmlPath) {  
  71.         File screenShotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);  
  72.         try {  
  73.             FileUtils.copyFile(screenShotFile,new File(dirPath + "\\images\\" + picName));  
  74.         } catch (IOException e) {  
  75.             // TODO Auto-generated catch block  
  76.             e.printStackTrace();  
  77.         }  
  78.         String content = "\n<br /><a href=\"images/"+picName+"\" target=\"_blank\"><img src=\"images/"+picName+"\" width=\"" + EtcIO.logPicWidth + "\" height=\"" + EtcIO.logPicHeight + "\" onclick=\"showPic(this);\" /></a>";  
  79.         EtcIO.AppendContent(htmlPath, content);  
  80.     }  
  81.   
  82. }  


HtmlDoc类:

  1. package com.nhn.platform.qa.cwmtest.Utils;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.text.SimpleDateFormat;  
  6. import java.util.Date;  
  7.   
  8. import org.openqa.selenium.WebDriver;  
  9.   
  10. public class HtmlDoc {  
  11.     protected String DirPath = "";  
  12.     protected String IndexList = "<tr><td>$CaseID</td><td>$TaskName</td><td>$TestTime</td><td>$TestSummary</td><td style=\"font-weight:bold;\"><a href=\"$href\"><font color=\"$color\">$TestResult</font></a></td><td>$Comments</td></tr>";  
  13.     protected String IndexModel = "";  
  14.     protected String DetailModel = "";  
  15.     protected String IndexFile = "";  
  16.     protected String DetailFile = "";  
  17.     protected String CsvFile = "";  
  18.   
  19.     protected String ProjectName = "CMS";  
  20.     protected String HttpPath = "http://127.0.0.1/";  
  21.     protected String ScriptPath = "http://127.0.0.1/";  
  22.     protected String TestDate = "2013-01-16";  
  23.     protected String Summary = "";  
  24.     protected int Total = 0;  
  25.     protected int Passed = 0;  
  26.     protected int Failed = 0;  
  27.   
  28.     protected String CaseID = "";  
  29.     protected String TaskName = "";  
  30.     protected String TestSummary = "";  
  31.     protected String TestResult = "";  
  32.     protected String href = "";  
  33.     protected String color = "";  
  34.     protected String Comments = "none";  
  35.   
  36.     protected String Precondition = "";  
  37.     protected String Steps = "";  
  38.     protected String Expects = "";  
  39.     protected String Results = "";  
  40.     protected String Remarks = "none";  
  41.   
  42.     public String HomePath;  
  43.     private String testTime;  
  44.   
  45.     public String getTestTime() {  
  46.         Date now = new Date();  
  47.         SimpleDateFormat dateFormat = new SimpleDateFormat("HH.mm.ss");  
  48.         this.testTime = dateFormat.format(now);  
  49.         return this.testTime;  
  50.     }  
  51.   
  52.     public HtmlDoc() {  
  53.         this.ProjectName = EtcIO.readValue("HtmlDoc.ProjectName");  
  54.         this.HomePath = EtcIO.readValue("HtmlDoc.HomePath");  
  55.   
  56.         this.IndexModel = EtcIO.readValue("HtmlDoc.IndexModel");  
  57.         this.DetailModel = EtcIO.readValue("HtmlDoc.DetailModel");  
  58.         this.ScriptPath = EtcIO.readValue("HtmlDoc.ScriptPath");  
  59.   
  60.         Date now = new Date();  
  61.         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
  62.         this.TestDate = dateFormat.format(now);  
  63.   
  64.         this.DirPath = EtcIO.readValue("HtmlDoc.DirPath") + "\\"  
  65.                 + this.TestDate;  
  66.         this.HttpPath = EtcIO.readValue("HtmlDoc.HttpPath") + "/"  
  67.                 + this.TestDate;  
  68.   
  69.         this.IndexFile = this.DirPath + "\\" + "index.html";  
  70.         this.CsvFile = this.DirPath + "\\" + "test_result.txt";  
  71.   
  72.         try {  
  73.             String temp = EtcIO.readValue("HtmlDoc.IndexList");  
  74.             if (temp != null && temp.equals("")) {  
  75.                 this.IndexList = temp;  
  76.             }  
  77.         } catch (Exception e) {  
  78.         }  
  79.   
  80.         File dirPath = new File(this.DirPath);  
  81.         if (dirPath.exists()) {  
  82.             String  distFolder = this.DirPath + ".bak." + this.getTestTime();  
  83.             try {  
  84.                 FileToolkit.moveFile(this.DirPath, distFolder);  
  85.             } catch (IOException e1) {  
  86.                 // TODO Auto-generated catch block  
  87.                 e1.printStackTrace();  
  88.             }  
  89.             try {  
  90.                 dirPath.delete();  
  91.                 Thread.sleep(1000);  
  92.             } catch (InterruptedException e) {  
  93.                 // TODO Auto-generated catch block  
  94.                 e.printStackTrace();  
  95.             }  
  96.         }  
  97.         dirPath.mkdirs();  
  98.         String[] search = new String[] { "$ProjectName", "$HttpPath",  
  99.                 "$ScriptPath", "$TestDate" };  
  100.         String[] replace = new String[] { this.ProjectName, this.HttpPath,  
  101.                 this.ScriptPath, this.TestDate };  
  102.         EtcIO.ReplaceContent(this.IndexModel, this.IndexFile, search, replace);  
  103.     }  
  104.   
  105.     public void InsertHtml(String TaskName, String TestSummary,  
  106.             String TestResult, String Comments, String Precondition,  
  107.             String Steps, String Expects, String Results, String Remarks) {  
  108.         this.Total++;  
  109.         this.CaseID = this.ProjectName + "-TEST-";  
  110.         if (this.Total < 10) {  
  111.             this.CaseID += "000" + this.Total;  
  112.         } else if (this.Total < 100) {  
  113.             this.CaseID += "00" + this.Total;  
  114.         } else if (this.Total < 1000) {  
  115.             this.CaseID += "0" + this.Total;  
  116.         } else {  
  117.             this.CaseID += this.Total;  
  118.         }  
  119.         this.TestResult = TestResult;  
  120.         this.TestSummary = TestSummary;  
  121.         this.Comments = Comments;  
  122.         if (this.TestResult.trim().toLowerCase().equals("pass")) {  
  123.             this.Passed++;  
  124.             this.color = "GREEN";  
  125.         } else {  
  126.             this.Failed++;  
  127.             this.color = "RED";  
  128.         }  
  129.         this.TaskName = TaskName;  
  130.         this.href = this.HttpPath + "/" + TaskName + "-" + this.CaseID  
  131.                 + ".html";  
  132.         this.DetailFile = this.DirPath + "\\" + TaskName + "-" + this.CaseID  
  133.                 + ".html";  
  134.         this.Precondition = Precondition;  
  135.         this.Steps = Steps;  
  136.         this.Expects = Expects;  
  137.         this.Results = Results;  
  138.         this.Remarks = Remarks;  
  139.         String temprow = this.IndexList.replace("$CaseID", this.CaseID)  
  140.                 .replace("$TaskName", this.TaskName)  
  141.                 .replace("$TestTime", this.getTestTime())  
  142.                 .replace("$TestSummary", this.TestSummary)  
  143.                 .replace("$href", this.href).replace("$color", this.color)  
  144.                 .replace("$TestResult", this.TestResult)  
  145.                 .replace("$Comments", this.Comments);  
  146.         EtcIO.AppendContent(this.IndexFile, temprow);  
  147.         String[] search = new String[] { "$CaseID", "$TaskName", "$TestTime",  
  148.                 "$TestSummary", "$color", "$TestResult", "$Comments",  
  149.                 "$Precondition", "$Steps", "$Expects", "$Results", "$Remarks",  
  150.                 "$href" };  
  151.         String[] replace = new String[] { this.CaseID, this.TaskName,  
  152.                 this.getTestTime(), this.TestSummary, this.color,  
  153.                 this.TestResult, this.Comments, this.Precondition, this.Steps,  
  154.                 this.Expects, this.Results, this.Remarks, this.HttpPath };  
  155.         EtcIO.ReplaceContent(this.DetailModel, this.DetailFile, search, replace);  
  156.         EtcIO.AppendContent(this.CsvFile, this.CaseID + "`" + this.TaskName  
  157.                 + "`" + this.TestSummary + "`" + this.Precondition + "`"  
  158.                 + this.Steps + "`" + this.Expects + "`" + this.getTestTime()  
  159.                 + "`" + this.Results + "`" + this.TestResult + "`"  
  160.                 + this.Remarks + "\r\n");  
  161.     }  
  162.   
  163.     public void CompleteCount() {  
  164.         String[] search = new String[] { "$Total", "$Passed", "$Failed" };  
  165.         String[] replace = new String[] { "" + this.Total, "" + this.Passed,  
  166.                 "" + this.Failed };  
  167.         EtcIO.ReplaceContent(this.IndexFile, this.IndexFile, search, replace);  
  168.         this.Summary = "PassRate: "  
  169.                 + String.format("%.2f",  
  170.                         ((double) this.Passed / this.Total) * 100)  
  171.                 + "% \t CompleteRate: 100%";  
  172.         EtcIO.ReplaceContent(this.IndexFile, this.IndexFile,  
  173.                 new String[] { "$Summary" }, new String[] { this.Summary });  
  174.     }  
  175.   
  176.     public void ScreenCapture() {  
  177.         if (!this.DetailFile.equals("")) {  
  178.             SnapShot.screenShoot(this.DirPath, this.CaseID + ".png",  
  179.                     this.DetailFile);  
  180.         }  
  181.     }  
  182.   
  183.     public void ScreenCapture(WebDriver driver) {  
  184.         if (!this.DetailFile.equals("")) {  
  185.             SnapShot.appendSnapShot(driver, this.DirPath, this.CaseID + ".png",  
  186.                     this.DetailFile);  
  187.         }  
  188.     }  
  189.   
  190.     public void ScreenCapture(String imagePath) {  
  191.         if (!this.DetailFile.equals("")) {  
  192.             SnapShot.appendSnapShotToLogFile(imagePath, this.DetailFile);  
  193.         }  
  194.     }  
  195. }  

  

  1. InsertHtml(String TaskName, String TestSummary,  
  2.         String TestResult, String Comments, String Precondition,  
  3.         String Steps, String Expects, String Results, String Remarks)  

该方法即ListSample.htm插入一条Summary并生成一个DetailSample.htm测试详细文件。

 CompleteCount()方法为测试完成后对ListSample.htm替换统计和计算测试通过率。

ScreenCapture()方法为测试过程中截图,截屏或者网页截图,并插入DetailSample.htm测试详细文件中。

 同时,会生成一个CSV类似格式的文本文件,自定义分隔符·,可以方便使用Excel打开。 

 

四、 .NET实现

HtmlDoc类:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.IO;  
  6. using System.Threading;  
  7.   
  8. namespace WugSshLib  
  9. {  
  10.     public class HtmlDoc  
  11.     {  
  12.         public string DirPath = "";  
  13.   
  14.         public string IndexList = "<tr><td>$CaseID</td><td>$TaskName</td><td>$TestTime</td><td>$TestSummary</td><td style=\"font-weight:bold;\"><a href=\"$href\"><font color=\"$color\">$TestResult</font></a></td><td>$Comments</td></tr>";  
  15.         public string IndexModel = "";  
  16.         public string DetailModel = "";  
  17.         public string IndexFile = "";  
  18.         public string DetailFile = "";  
  19.         public string CsvFile = "";  
  20.   
  21.         public string ProjectName = "CMS";  
  22.         public string HttpPath = "http://127.0.0.1/";  
  23.         public string ScriptPath = "http://127.0.0.1/";  
  24.         public string TestDate = "2013-01-16";  
  25.         public string Summary = "";  
  26.         public int Total = 0;  
  27.         public int Passed = 0;  
  28.         public int Failed = 0;  
  29.   
  30.         public string CaseID = "";  
  31.         public string TaskName = "";  
  32.         public string TestSummary = "";  
  33.         public string TestResult = "";  
  34.         public string href = "";  
  35.         public string color = "";  
  36.         public string Comments = "none";  
  37.   
  38.         public string Precondition = "";  
  39.         public string Steps = "";  
  40.         public string Expects = "";  
  41.         public string Results = "";  
  42.         public string Remarks = "none";  
  43.   
  44.         public string TestTime  
  45.         {  
  46.             get  
  47.             {  
  48.                 return DateTime.Now.ToLongTimeString();  
  49.             }  
  50.         }  
  51.         public HtmlDoc(string project,string webdir)  
  52.         {  
  53.             this.ProjectName = project;  
  54.             if (Directory.Exists(webdir))  
  55.             {  
  56.                 this.DirPath = Path.Combine(webdir,project);  
  57.             }  
  58.             else  
  59.             {  
  60.                 this.DirPath = Path.Combine(System.Windows.Forms.Application.StartupPath, "nginx/html/" + project );  
  61.             }  
  62.             if (!Directory.Exists(this.DirPath))  
  63.             {  
  64.                 Directory.CreateDirectory(this.DirPath);  
  65.             }  
  66.             IniFiles inifile = new IniFiles(Path.Combine(System.Windows.Forms.Application.StartupPath, "HtmlDoc.ini"));  
  67.             //IndexList = Path.Combine(this.DirPath, inifile.ReadString("HTMLDOC", "IndexList", IndexList));  
  68.             IndexModel = Path.Combine(System.Windows.Forms.Application.StartupPath, inifile.ReadString("HTMLDOC", "IndexModel", "ListSample.htm"));  
  69.             DetailModel = Path.Combine(System.Windows.Forms.Application.StartupPath, inifile.ReadString("HTMLDOC", "DetailModel", "DetailSample.htm"));  
  70.             this.ScriptPath = inifile.ReadString("HTMLDOC", "ScriptPath", "http://127.0.0.1/");  
  71.             this.TestDate = DateTime.Now.ToString("yyyy-MM-dd");  
  72.             this.HttpPath = inifile.ReadString("HTMLDOC", "HttpPath", "http://127.0.0.1/") + project + "/" + this.TestDate;  
  73.             this.IndexFile = Path.Combine(this.DirPath, this.TestDate, "index.html");  
  74.             this.CsvFile = Path.Combine(this.DirPath, this.TestDate, "test_result.txt");  
  75.             if (Directory.Exists(Path.Combine(this.DirPath, this.TestDate)))  
  76.             {  
  77.                 Directory.Move(Path.Combine(this.DirPath, this.TestDate), Path.Combine(this.DirPath, this.TestDate) + ".bak" + DateTime.Now.ToString("-HHmmss"));  
  78.                 Thread.Sleep(2000);  
  79.             }  
  80.             Directory.CreateDirectory(Path.Combine(this.DirPath, this.TestDate));  
  81.             string tempmodel = File.ReadAllText(this.IndexModel).Replace("$ProjectName", this.ProjectName).Replace("$HttpPath", this.HttpPath).Replace("$ScriptPath", this.ScriptPath).Replace("$TestDate", this.TestDate);  
  82.             File.AppendAllText(this.IndexFile, tempmodel);  
  83.         }  
  84.         public void InsertHtml(string TaskName, string TestSummary, string TestResult, string Comments, string Precondition, string Steps, string Expects, string Results, string Remarks)  
  85.         {  
  86.             this.Total++;  
  87.             this.CaseID = this.ProjectName + "-TEST-";  
  88.             if (this.Total < 10)  
  89.             {  
  90.                 this.CaseID += "000" + this.Total;  
  91.             }  
  92.             else if (this.Total < 100)  
  93.             {  
  94.                 this.CaseID += "00" + this.Total;  
  95.             }  
  96.             else if (this.Total < 1000)  
  97.             {  
  98.                 this.CaseID += "0" + this.Total;  
  99.             }  
  100.             else  
  101.             {  
  102.                 this.CaseID += this.Total;  
  103.             }  
  104.             this.TestResult = TestResult;  
  105.             this.TestSummary = TestSummary;  
  106.             this.Comments = Comments;  
  107.             if (this.TestResult.Trim().ToLower().Equals("pass"))  
  108.             {  
  109.                 this.Passed++;  
  110.                 this.color = "GREEN";  
  111.             }  
  112.             else  
  113.             {  
  114.                 this.Failed++;  
  115.                 this.color = "RED";  
  116.             }  
  117.             this.TaskName = TaskName;  
  118.             this.href = this.HttpPath + "/" + TaskName + "-"+this.CaseID+".html";  
  119.             this.DetailFile = Path.Combine(this.DirPath, this.TestDate, TaskName + "-" + this.CaseID + ".html");  
  120.             this.Precondition = Precondition;  
  121.             this.Steps = Steps;  
  122.             this.Expects = Expects;  
  123.             this.Results = Results;  
  124.             this.Remarks = Remarks;  
  125.             string temprow = this.IndexList.Replace("$CaseID", this.CaseID).Replace("$TaskName", this.TaskName).Replace("$TestTime", this.TestTime).Replace("$TestSummary", this.TestSummary).Replace("$href", this.href).Replace("$color", this.color).Replace("$TestResult", this.TestResult).Replace("$Comments", this.Comments);  
  126.             File.AppendAllText(this.IndexFile, temprow);  
  127.             string tempdetail = File.ReadAllText(this.DetailModel).Replace("$CaseID", this.CaseID).Replace("$TaskName", this.TaskName).Replace("$TestTime", this.TestTime).Replace("$TestSummary", this.TestSummary).Replace("$color", this.color).Replace("$TestResult", this.TestResult).Replace("$Comments", this.Comments).Replace("$Precondition", this.Precondition).Replace("$Steps", this.Steps).Replace("$Expects", this.Expects).Replace("$Results", this.Results).Replace("$Remarks", this.Remarks).Replace("$href", this.HttpPath);  
  128.             File.AppendAllText(this.DetailFile, tempdetail);  
  129.             File.AppendAllText(this.CsvFile, this.CaseID + "`" + this.TaskName + "`" + this.TestSummary + "`" + this.Precondition + "`" + this.Steps + "`" + this.Expects + "`" + this.TestTime + "`" + this.Results + "`" + this.TestResult + "`" + this.Remarks + "\r\n");  
  130.         }  
  131.         public void CompleteCount()  
  132.         {  
  133.             string tempindex = File.ReadAllText(this.IndexFile);  
  134.             tempindex = tempindex.Replace("$Total", "" + this.Total).Replace("$Passed", "" + this.Passed).Replace("$Failed", "" + this.Failed);  
  135.             this.Summary = "PassRate: "+Math.Round(((double)this.Passed / this.Total)*100,2) + "% \t CompleteRate: 100%";  
  136.             tempindex = tempindex.Replace("$Summary", this.Summary);  
  137.             //string file = this.IndexFile.Replace("index.htm", "index.html");  
  138.             File.WriteAllText(this.IndexFile, tempindex);  
  139.             //File.Delete(this.IndexFile);  
  140.         }  
  141.     }  
  142. }  


同样,InsertHtml(string TaskName, string TestSummary, string TestResult, string Comments, string Precondition, string Steps, string Expects, string Results, string Remarks)函数即ListSample.htm插入一条Summary并生成一个DetailSample.htm测试详细文件。

 CompleteCount()函数为测试完成后对ListSample.htm替换统计和计算测试通过率。该处没有使用截图,需要的可以引用WinAPI对屏幕或窗口进行截图。

同时,会生成一个CSV类似格式的文本文件,自定义分隔符·,可以方便使用Excel打开。 

 

五、 效果展示

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值