QTP简单框架(8)之HTML报表

Dim oReporterManager
Set oReporterManager = New ReporterManager
Call oReporterManager.StartReport("Html","人人围登录1","C:\Report.html","测试下而已1","sirrah")
'Call oReporterManager.StartReport("Excel","人人围登录1","C:\Report.xls","测试下而已1","sirrah")
Call oReporterManager.Report("Pass","Test_Case1","成功","成功","步骤详细1")
Call oReporterManager.StartReport("Html","人人围登录2","C:\Report.html","测试下而已2","sirrah")
'Call oReporterManager.StartReport("Excel","人人围登录2","C:\Report.xls","测试下而已2","sirrah")
Call oReporterManager.Report("Fail","Test_Case2","成功","成功","步骤详细2")
Call oReporterManager.StartReport("Html","人人围登录3","C:\Report.html","测试下而已2","sirrah")
'Call oReporterManager.StartReport("Excel","人人围登录2","C:\Report.xls","测试下而已2","sirrah")
Call oReporterManager.Report("Fail","Test_Case3","成功","成功","步骤详细3")
Call oReporterManager.Report("Fail","Test_Case3","成功","成功","步骤详细4")
Call oReporterManager.Report("Fail","Test_Case3","成功","成功","步骤详细35")
Call oReporterManager.Report("Fail","Test_Case3","成功","成功","步骤详细36")
Call oReporterManager.Report("Fail","Test_Case3","成功","成功","步骤详细37")
Call oReporterManager.Report("Fail","Test_Case3","成功","成功","步骤详细38")
Call oReporterManager.Report("Fail","Test_Case3","成功","成功","步骤详细39")
Call oReporterManager.Report("Fail","Test_Case3","成功","成功","步骤详细30")
Call oReporterManager.Report("Fail","Test_Case3","成功","成功","步骤详细3-")

Call oReporterManager.StopReport




Class ReporterManager

'定义3个Dictionary,分别存放测试用例、整个测试、已开启的报告格式
Dim Dic_CaseResult,Dic_MyTest,Dic_Filter
Dim Str_HtmlReportFile,Str_ExcelReportFile,Str_ReportTypes
Dim Bln_Initialized,Bln_NewCase

'-------------------------------------------------------------------------------
'  函数名称: StartReport
'  函数说明: 开启生成测试报告机制
'  参数说明: 
'            (1).sReportType:报告类型(目前仅支持Excel、Html)
'            (2).sCaseName:测试用例名称
'            (3).sFileName:报告输出的路径(文件在磁盘绝对路径位置)
'            (4).sCaseDesc:测试用例描述、说明
'            (5).sAuthor:测试用例设计者名称
'  返回结果: 无 
'  调用方法:
'            StartReport("Html","TestLogin","C:\Report.html","TestLoginDesc","Sirrah")
'-------------------------------------------------------------------------------

Function StartReport(sReportType,sCaseName,sFileName,sCaseDesc,sAuthor)
	'目前报告所支持的所有格式
	Str_ReportTypes = "Html|Excel"
	'校验sReportType格式,是否是目前所支持的报告格式
	If Not CheckType(sReportType,Str_ReportTypes) Then
		MsgBox "对不起!目前暂不支持"&sReportType&"格式"
	End If
	'校验sFileName文件格式是否是合法的报告格式
	If Not CheckFile(sReportType,sFileName) Then
		MsgBox "对不起!您输入的文件名:"&sFileName&"是非法 "&sReportType&" 格式!"
	End If
	'检查是否已初始化报告,否则首先初始化报告
	If Not Bln_Initialized Then
		Call InitReport
	End If
	'判断当前格式的报告机制是否已开启
	If CheckFilter(sReportType) Then
	   If sFileName <> Str_HtmlReportFile And sFileName <> Str_ExcelReportFile Then
	    	Dic_MyTest.RemoveAll
	   End If 
	Else
		Dic_Filter.Add sReportType,""
	End If
	If sFileName <> Str_HtmlReportFile And sFileName <> Str_ExcelReportFile Then 
	    If IsFileExist(sFileName) Then
	'判断当前格式的报告是否存在,如果存在则删除该文件
			DeleteFile(sFileName)
		End If
	'根据报告格式,获取报告文件名,创建报告文件
		Select Case sReportType
			Case "Html" 
				Str_HtmlReportFile = sFileName
				CreateReport_Html
			Case "Excel" 
				Str_ExcelReportFile = sFileName
				CreateReport_Excel
		End Select
	End If
	
	'判断当前执行的测试用例是否是新的测试用例
	If  Dic_CaseResult.Exists("CaseName")  Then 
		If  Dic_CaseResult.Item("CaseName") <> sCaseName Then
			'如果是新测试用例,则更新Dic_CaseResult中的成功、失败用例的计数
			If  Dic_CaseResult.Item("TestResult") = "Pass" Then
				Dic_MyTest.Item("PassCases_count") = Dic_MyTest.Item("PassCases_count") + 1
			Else
				Dic_MyTest.Item("FailCases_count") = Dic_MyTest.Item("FailCases_count") + 1
			End If
		    Call ReportToReport
			'执行新测试用例,更新Dic_MyTest中总测试用例计数
			Dic_MyTest.Item("TotalCases_count") = Dic_MyTest.Item("TotalCases_count") +1
			'将Dic_CaseResult中的Item信息清空,记录新测试用例的结果信息
			Dic_CaseResult.RemoveAll
			Bln_NewCase = True
		End If
	Else
	Bln_NewCase = True
	End If 
	'如果是新测试用例,则创建Dic_CaseResult
	If Bln_NewCase Then
		Call CreateDic_Case(sCaseName,sCaseDesc,sAuthor)
	End If
End Function

'-------------------------------------------------------------------------------
'  函数名称 :  InitReport
'  函数说明 :  初始化报告
'  参数说明 :  Null
'  返回结果 :  Null   
'-------------------------------------------------------------------------------
Function InitReport
	Err.clear
	Set Dic_CaseResult = CreateObject("Scripting.Dictionary")
	Set Dic_MyTest = CreateObject("Scripting.Dictionary")
	Set Dic_Filter = CreateObject("Scripting.Dictionary")
	Call CreateDic_MyTest
	'判断初始化是否成功,初始化成功设置为True,失败提示信息
	If Err.Number = 0 Then
		Bln_Initialized = True
	Else
		MsgBox "对不起!初始化报告失败!"&"#Error:"&Err.Description
	End If
End Function

'-------------------------------------------------------------------------------
'  函数名称 :  CreateDic_MyTest
'  函数说明 :  创建MyTest Dictionary
'  参数说明 :  Null
'  返回结果 :  Null   
'-------------------------------------------------------------------------------
Function CreateDic_MyTest
	Dic_MyTest.Add "TestDate",Date
	Dic_MyTest.Add "TestStart",Time
	Dic_MyTest.Add "TestStop",Time
	Dic_MyTest.Add "TestUse",Time
	Dic_MyTest.Add "TotalCases_count",1
	Dic_MyTest.Add "TotalCheckPoints_count",0
	Dic_MyTest.Add "PassCases_count",0
	Dic_MyTest.Add "FailCases_count",0
End Function

'-------------------------------------------------------------------------------
'  函数名称 :  CreateDic_Case
'  函数说明 :  创建Case Dictionary
'  参数说明 :  sCaseName:测试用例名称
'              sCaseDesc:测试用例描述
'              sAuthor:测试用例设计者
'  返回结果:   Null   
'-------------------------------------------------------------------------------
Function CreateDic_Case(sCaseName,sCaseDesc,sAuthor)
	Dic_CaseResult.Add "CaseName",sCaseName
	Dic_CaseResult.Add "CaseDesc",sCaseDesc
	Dic_CaseResult.Add "Author",sAuthor
	Dic_CaseResult.Add "TestResult",""   
	Dic_CaseResult.Add "CheckPointCount",0
	Bln_NewCase = False
End Function

'-------------------------------------------------------------------------------
'  函数名称 :  Report
'  函数说明 :  脚本中检查点处调用,记录脚本的运行结果
'  参数说明 :  CP_Result:检查点结果
'              CP_Name:检查点名称
'              CP_ExpectedValue:检查点预期值
'              CP_ActualValue:检查点实际值
'              CP_Detail:检查点描述,可以输入检查点错误信息描述
'  返回结果:   Null   
'-------------------------------------------------------------------------------
Function Report(CP_Result,CP_Name,CP_ExpectedValue,CP_ActualValue,CP_Detail)
	Set Dic_CheckPoint= CreateObject("Scripting.Dictionary")
	Dic_CheckPoint.Add "CP_Result",CP_Result
	Dic_CheckPoint.Add "CP_Name",CP_Name
	Dic_CheckPoint.Add "CP_ExpectedValue",CP_ExpectedValue
	Dic_CheckPoint.Add "CP_ActualValue",CP_ActualValue
	Dic_CheckPoint.Add "CP_Detail",CP_Detail
	'将检查点Dictionary添加至Dic_CaseResult中
	Dic_CaseResult.Add "CheckPoint"&Dic_CaseResult.Count-4,Dic_CheckPoint
	Dic_CaseResult.Item("CheckPointCount")=Dic_CaseResult.Item("CheckPointCount")+1
	Dic_MyTest.Item("TotalCheckPoints_count") = Dic_MyTest.Item("TotalCheckPoints_count") +1
	'仅当TestResult为空或者状态为Pass时,每次存储检查点信息时,更新TestResult值
	If Trim(Dic_CaseResult.Item("TestResult")) = "" Or Dic_CaseResult.Item("TestResult") = "Pass" Then
		Dic_CaseResult.Item("TestResult") = CP_Result
	End If
End Function

'-------------------------------------------------------------------------------
'  函数名称:  IsFolderExist
'  函数说明:  检查文件夹是否存在
'  参数说明:  sFolder:文件夹路径
'  返回结果:  Boolean,文件夹是否存在 
'-------------------------------------------------------------------------------
Function IsFolderExist(sFolder)
	Set fso = CreateObject("Scripting.FileSystemObject")
	IsFolderExist = fso.FolderExists(sFolder)
End Function

'-------------------------------------------------------------------------------
'  函数名称:  IsFileExist
'  函数说明:  检查文件是否存在
'  参数说明:  sFolder:文件路径
'  返回结果:  Boolean,文件夹是否存在 
'-------------------------------------------------------------------------------
Function IsFileExist(sFile)
	Set fso = CreateObject("scripting.FileSystemObject") 
	IsFileExist =  fso.FileExists(sFile)
End Function

'-------------------------------------------------------------------------------
'  函数名称 :  sFile
'  函数说明 :  删除文件
'  参数说明:   sFile:文件路径
'  返回结果:   Null
'-------------------------------------------------------------------------------
Function DeleteFile(sFile)
	Set fso = CreateObject("scripting.FileSystemObject") 
	fso.DeleteFile(sFile)
End Function

'-------------------------------------------------------------------------------
'  函数名称 :  CheckFile
'  函数说明 :  检查文件格式是否符合所选择的
'  参数说明 :  sFile:文件路径
'  返回结果 :  Boolean,文件名称是否合法
'-------------------------------------------------------------------------------
Function CheckFile(sType,sFileName)
	'sSuffix 获取文件的后缀名
	sSuffix=Right(sFileName,Len(sFileName)-InStrRev(sFileName,"."))
	Select Case sType
		Case "Html"
			If LCase(sSuffix)  =  "html" Or LCase(sSuffix) =  "htm" Then
				CheckFile = True
			End If 
		Case "Excel"
			 If LCase(sSuffix) = "xls" Or LCase(sSuffix) = "xlsx"  Then
				CheckFile = True
			End If    
	End Select
End Function

'-------------------------------------------------------------------------------
'  函数名称 :  CheckFile
'  函数说明 :  检查文件格式是否符合所选择的
'  参数说明 :  sFile:文件路径
'  返回结果 :  Boolean,文件名称是否合法
'-------------------------------------------------------------------------------

'-------------------------------------------------------------------------------
'  函数名称 :  CheckType
'  函数说明 :  检查报告格式是否是目前所支持的所有报告格式
'  参数说明 :  sType:报告格式
'              sTypes:报告所有支持的格式
'  返回结果 :  Boolean,报告格式是否是目前所有支持的报告格式
'-------------------------------------------------------------------------------
Function CheckType(sType,sTypes)
	Types = Split(sTypes,"|")
	For Types_Count = 0 To UBound(Types)
		If  Types(Types_Count) = sType Then
			CheckType = True
			Exit Function
		End If 
	Next
End Function

'-------------------------------------------------------------------------------
'  函数名称 :  CheckFilter
'  函数说明 :  检查报告机制是否已开启
'  参数说明 :  sType:报告格式
'  返回结果 :  Boolean,报告格式是否已开启
'-------------------------------------------------------------------------------
Function CheckFilter(sType)
	If   Dic_Filter.Exists(sType) Then
		CheckFilter = True
	Else
		CheckFilter = False
	End If
End Function

'-------------------------------------------------------------------------------
'  函数名称 :  CreateReport_Excel
'  函数说明 :  创建Excel报告
'  参数说明 :  Null
'  返回结果 :  Null
'-------------------------------------------------------------------------------
Function CreateReport_Excel
	Set oExcel=CreateObject("Excel.Application")
	oExcel.Visible=False
	oExcel.DisplayAlerts = False
	oExcel.Workbooks.Add 
	'设置第一个sheet样式
	Set oSheet = oExcel.Sheets.Item(1) 
	oExcel.Sheets.Item(1).Select 
	With oSheet

		'将第一个sheet命名为"测试概要"
		.Name = "测试概要" 
		'设置顶部第一行标题
		.Range("B1").Value = "测试结果" 
		'设置单元格B1到C1合并居中
		.Range("B1:E1").Merge 
		'设置单元格背景颜色
		.Range("B1:E1").Interior.ColorIndex = 21 
		'设置单元格对齐方式:居中
		.Range("B1:E1").HorizontalAlignment = 3 
		'设置字体的颜色
		.Range("B1:E1").Font.ColorIndex = 19 
		'设置字体加粗
		.Range("B1:E1").Font.Bold = True 
		'设置字体的大小
		.Range("B1:E1").Font.Size = 16   
          
		'设置测试概要信息内容
		.Range("B3").Value = "测试日期: "
		.Range("D3").Value = "测试开始时间: "
		.Range("B4").Value = "测试用时: " 
		.Range("D4").Value = "测试结束时间: "
		.Range("B5").Value = "总用例数:"
		.Range("D5").Value = "总步骤数:"
		.Range("B6").Value = "成功用例数:"
		.Range("D6").Value = "失败用例数:"

		.Range("C3").Value = Date()
		.Range("E3").Value = Time()
		'设置时间的格式为24小时制
		.Range("E3").NumberFormat = "hh:mm:ss" 
		.Range("E4").Value = Time()
		'设置时间的格式为24小时制
		.Range("E4").NumberFormat = "hh:mm:ss"
		'设置时间:结束时间减去开始时间
		.Range("C4").Value = "=RC[2]-R[-1]C[2]" 
		.Range("C4").NumberFormat = "hh:mm:ss" 
		.Range("C5").Value = "0"
		.Range("E5").Value = "0"
		.Range("C6").Value = "0"
		.Range("E6").Value = "0"
   
		'设置单元格边框样式
		.Range("B3:E6").Borders(1).LineStyle = 1 
		.Range("B3:E6").Borders(2).LineStyle = 1
		.Range("B3:E6").Borders(3).LineStyle = 1
		.Range("B3:E6").Borders(4).LineStyle = 1

		'设置背景色和字体样式
		.Range("B3:E6").Font.Bold = True
		.Range("B3:E6").Font.Size = 10

		.Range("B3:B6").Interior.ColorIndex = 50
		.Range("B3:B6").Font.ColorIndex = 19
		.Range("D3:D6").Interior.ColorIndex = 50
		.Range("D3:D6").Font.ColorIndex = 19

		.Range("C3:C6").Interior.ColorIndex = 15
		.Range("C3:C6").HorizontalAlignment = 3
		.Range("C3:C5").Font.ColorIndex = 25
		.Range("C6:C6").Font.ColorIndex = 10
            
		.Range("E3:E6").Interior.ColorIndex = 15
		.Range("E3:E6").HorizontalAlignment = 3
		.Range("E3:E5").Font.ColorIndex = 25
		.Range("E6:E6").Font.ColorIndex = 3
            
		'设置测试用例区域标题   
		.Range("B10").Value = "用例名称"
		.Range("C10").Value = "测试结果"
		.Range("D10").Value = "用例步骤数"
		.Range("E10").Value = "用例描述"
		.Range("F10").Value = "设计者"
		.Range("G10").Value = "*点击用例名查看详细的测试结果"
   
		'设置背景色和字体样式
		.Range("B10:F10").Interior.ColorIndex = 21
		.Range("B10:F10").HorizontalAlignment = 3
		.Range("B10:F10").Font.ColorIndex = 19
		.Range("B10:F10").Font.Bold = True
		.Range("B10:F10").Font.Size = 14
 
		'设置单元格四周边框
		.Range("B10:F10").Borders(1).LineStyle = 1 
		.Range("B10:F10").Borders(2).LineStyle = 1
		.Range("B10:F10").Borders(3).LineStyle = 1
		.Range("B10:F10").Borders(4).LineStyle = 1
 
		'设置单元格自动适应大小
		.Columns("B:G").Autofit
		.Range("B11").Select
		oExcel.ActiveWindow.FreezePanes = True
	End With

	'设置第二个sheet样式
	Set oSheet = oExcel.Sheets.Item(2)
	oExcel.Sheets.Item(2).Select
	With oSheet
		'设置第二个sheet命名为"测试结果"
		.Name = "测试结果"  
		'标题栏每列的宽度
		.Columns("B:B").ColumnWidth = 20
		.Columns("C:C").ColumnWidth = 15
		.Columns("D:D").ColumnWidth = 25
		.Columns("E:E").ColumnWidth = 25
		.Columns("F:F").ColumnWidth = 25
		'设置单元格对齐方式:左对齐
		.Columns("B:F").HorizontalAlignment = 2 
		.Columns("B:F").WrapText = True
     
		'设置标题栏名称
		.Range("B1").Value = "步骤名"
		.Range("C1").Value = "测试结果"
		.Range("D1").Value = "预期结果"
		.Range("E1").Value = "实际结果"
		.Range("F1").Value = "结果描述"
   
		'设置标题栏字体样式
		.Range("B1:F1").Interior.ColorIndex = 21
		.Range("B1:F1").Font.ColorIndex = 19
		.Range("B1:F1").Font.Bold = True
		.Range("B1:F1").Font.Size = 16
 
		'设置单元格四周边框
		.Range("B1:F1").Borders(1).LineStyle = 1 
		.Range("B1:F1").Borders(2).LineStyle = 1
		.Range("B1:F1").Borders(3).LineStyle = 1
		.Range("B1:F1").Borders(4).LineStyle = 1
		.Range("B2").Select
		oExcel.ActiveWindow.FreezePanes = True
	End With

	'删除多余的第三个sheet
	Set oSheet = oExcel.Sheets.Item(3) 
	oExcel.Sheets.Item(3).delete 
	oExcel.Sheets.Item(1).Select 
	oExcel.ActiveWorkbook.saveas Str_ExcelReportFile
	oExcel.Quit
	Set oSheet=Nothing
	Set oExcel=Nothing
End Function

'-------------------------------------------------------------------------------
'  函数名称 :  CreateReport_Html
'  函数说明 :  创建Html报告
'  参数说明 :  Null
'  返回结果 :  Null
'-------------------------------------------------------------------------------
Function CreateReport_Html
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set ts = fso.OpenTextFile(Str_HtmlReportFile,2,True)
	ts.writeline("<!DOCTYPE HTML>")
	ts.writeline("<html>")
	ts.writeline("<head><title>自动化测试报告</title>")
	ts.writeline("<style>")
	ts.writeline("body {margin:30px auto;background:#D5DAE0;text-align:center;} ")
	ts.writeline(".commonWrap_1{width:88%;margin:10px auto auto auto;border:1px solid #c2c9cf;background:#FCFDFF;border-radius:10px;}")
	ts.writeline(".commonWrap_2{width:95%;margin:5px auto 8px auto;border:1px dotted #85614D;background:#FCFDFF;border-radius:2px;}")
	ts.writeline(".commonWrap_3{width:85%;margin:5px auto 8px auto;border:1px dotted black;background:#FCFDFF;border-radius:3px;}")
	ts.writeline(".commonWrap_4{width:95%;margin:5px auto 8px auto;border:1px dotted black;background:#FCFDFF;border-radius:3px;}")
	ts.writeline(".open {font:0/0'宋体'; border:6px solid; border-color:#F5F5F5 #F5F5F5 #F5F5F5 #EF993A;float:left;}")
	ts.writeline(".close {font:0/0'宋体'; border:6px solid; border-color:#959CEE #F5F5F5 #F5F5F5 #F5F5F5;float:left;}")
	ts.writeline(".line_1{display:block;border-top:15px solid #D5DAE0;border-bottom:2px solid #616D79;margin:10px auto 5px auto ;}")
	ts.writeline(".line_2{display:block;border-top:1px solid #D5DAE0;border-bottom:1px solid #FFFFFF;margin:10px auto 5px auto ;}")
	ts.WriteLine(".line_3{width:99%;display:block;border-top:1px solid #D5DAE0;border-bottom:1px solid #85614D;margin:auto;}")
	ts.writeline(".MainTitle {width:100%;color:#000080;font:normal normal 35px'Microsoft Yahei'}")
	ts.writeline(".maintitle_1 {color:black;font:normal normal 25px'Microsoft Yahei'}")
	ts.writeline(".maintitle_2 {color:black;font:normal normal 25px'Microsoft Yahei'}")
	ts.writeline("table.TestInformation {width:100%;border-collapse: collapse;border:1px hidden;empty-cells:show;margin:auto;font:normal normal 13px 'Microsoft Yahei'}")
	ts.writeline("table.Testcase {text-align: center;width:100%;margin:auto auto auto auto ;background:#F5F5F5;font:normal normal 10px'Microsoft Yahei'}")
	ts.writeline("table.CheckPoint {width:100%;border-collapse: collapse;border:1px hidden;}")
	ts.writeline("div.foldingbutton{text-align:center;font-size:0.8em;height:0.9em;width:0.9em;float:left;background-color:#F5F5F5;}")
	ts.writeline("tr.checkpointrow{background-color:#FFFFFF;border: 1px solid #000000;}")
	ts.writeline("td.Info_Item {width:25%;color:#000000;border: 1px solid #5E5E5E;background:#A9A9A9;}")
	ts.writeline("td.Info_Value{text-align: center;color:#A52A2A;border: 1px solid #5E5E5E;background:#F5F5F5;}")
	ts.writeline("td.Info_Value_Pass{text-align: center;color:#008000;border: 1px solid #5E5E5E;background:#F5F5F5;}")
	ts.writeline("td.Info_Value_Fail{text-align: center;color:#FF0000;border: 1px solid #5E5E5E;background:#F5F5F5;}")
	ts.writeline("td.Case_Name_1{width:9%;text-align:left;font-size:13px;}")
	ts.writeline("td.Case_Name_2{width:15%;text-align:left;color:#8B4513;font-size:13px;}")
	ts.writeline("td.Case_Result{width:8%;text-align:center;}")
	ts.writeline("td.Case_Result_Pass{width:5%;text-align:center;color:#228B22;}")
	ts.writeline("td.Case_Result_Fail{width:5%;text-align:center;color:#FF0000;}")
	ts.writeline("td.Case_Ponts_1{width:7%;text-align:center;}")
	ts.writeline("td.Case_Ponts_2{width:5%;text-align:center;color:#FF00FF;}")
	ts.writeline("td.Case_Desc_1{width:8%;text-align:center;font-size:10px;}")
	ts.writeline("td.Case_Desc_2{width:26%;text-align:left;color:#4169E1;font-size:10px;}")
	ts.writeline("td.Case_Author_1{width:7%;text-align:Center;}")
	ts.writeline("td.Case_Author_2{width:9%;text-align:Center;color:#800080;font-style:italic;}")
	ts.writeline("td.CP_title{text-align:center;background: #A9A9A9;color:#FFFFFF;border: 1px solid #5E5E5E;}")
	ts.writeline("td.passcentertext{text-align: center;color:#008000;font-size:10px;border: 1px solid #5E5E5E;} ")
	ts.writeline("td.failedcentertext{text-align: center;color:#FF0000;font-size:10px;border: 1px solid #5E5E5E;}")
	ts.writeline("td.otherertext{text-align: center;color:#000000;font-size:10px;border: 1px solid #5E5E5E;}")
	ts.writeline(".backToTop {display: none; width: 18px; line-height: 1.2; padding: 5px 0;")
	ts.writeline("background-color: #000; color: #fff; font-size: 12px; text-align: center;")
	ts.writeline("position: fixed; _position: absolute; right: 2px; bottom: 35px;")
	ts.writeline("_bottom:'auto'; cursor: pointer; opacity: .6; filter: Alpha(opacity=60);}")
	ts.writeline("</style>")
	ts.writeline("<script type='text/javascript' src='http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js'></script>")
	ts.writeline("<script type='text/javascript'>")
	ts.writeline("function toggle_child_visibility(element_id) {")
	ts.writeline("if (document.getElementById(element_id +'_children') != null) {toggle_visibility(element_id +'_children')}")
	ts.writeline("else {toggle_visibility(element_id)}")
	ts.writeline("if (document.getElementById(element_id +'_foldlink') != null) {toggle_visibility(element_id +'_foldlink')}")
	ts.writeline("if (document.getElementById(element_id +'_unfoldlink') != null) {toggle_visibility(element_id +'_unfoldlink')}}")
	ts.writeline("function toggle_visibility(element_id) {")
	ts.writeline("var element = document.getElementById(element_id)")
	ts.writeline("if (element == null) { return }")
	ts.writeline("if (element.style.display =='none') {element.style.display ='block'}")
	ts.writeline("else {element.style.display ='none'}}")
	ts.writeline("function mouseover(obj){obj.style.background='#66CDAA'}")
	ts.writeline("function mouseout(obj){obj.style.backgroundColor ='white'}")
	ts.writeline("</script>")
	ts.writeline("</head>")
	ts.writeline("<body>")
	ts.writeline("<b class='MainTitle'>自动化测试报告</b>")
	ts.writeline("<span class='line_1'></span>")
	ts.writeline("<div class='commonWrap_1'>")
	ts.writeline("<br><b class='maintitle_1'>测试概述</b>")
	ts.writeline("<span class='line_2'></span>")
	ts.writeline("<div class='commonWrap_3'>")
	ts.writeline("<table class='TestInformation' align='center'>")
	ts.writeline("<tr>")
	ts.writeline("<td class='Info_Item'>测 试 日 期:</td><td class='Info_Value'>%TestDate%</td>")
	ts.writeline("<td class='Info_Item'>测 试 开 始 时 间:</td><td class='Info_Value'>%TestStart%</td>")
	ts.writeline("</tr>")
	ts.writeline("<tr>")
	ts.writeline("<td class='Info_Item'>测 试 用 时:</td><td class='Info_Value'>%TestUse%</td>")
	ts.writeline("<td class='Info_Item'>测 试 结 束 时 间:</td><td class='Info_Value'>%TestStop%</td>")
	ts.writeline("</tr> ")
	ts.writeline("<tr><td class='Info_Item'>总 用 例 数:</td><td class='Info_Value'>%ToTal_TestCase%</td>")
	ts.writeline("<td class='Info_Item'>总 步 骤 数:</td><td class='Info_Value'>%Total_CheckPoints%</td>")
	ts.writeline("</tr>")
	ts.writeline("<tr><td class='Info_Item'>成 功 用 例 数:</td><td class='Info_Value_Pass'>%Pass_TestCase%</td>")
	ts.writeline("<td class='Info_Item'>失 败 用 例 数:</td><td class='Info_Value_Fail'>%Fail_TestCase%</td>")
	ts.writeline("</tr>")
	ts.writeline("</table>")
	ts.writeline("</div>")
	ts.writeline("</div>")
	ts.writeline("<div class='commonWrap_1'>")
	ts.writeline("<br><b class='maintitle_2'>测试结果</b>")
	ts.writeline("<span class='line_2'></span>")
End Function

'-------------------------------------------------------------------------------
'  函数名称 :  AddHtmlButtom
'  函数说明 :  添加Html底部
'  参数说明 :  Null
'  返回结果 :  Null
'-------------------------------------------------------------------------------
Function AddHtmlButtom
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set ts = fso.OpenTextFile(Str_HtmlReportFile,8,False)
	ts.writeline("</div></div>")
	ts.writeline("<script type='text/javascript'>(function() {")
    ts.writeline("var $backToTopTxt ='返回顶部', $backToTopEle = $('<div class="&Chr(34)&"backToTop"&Chr(34)&"></div>').appendTo($('body'))")
    ts.writeline(".text($backToTopTxt).attr('title', $backToTopTxt).click(function() {")
    ts.writeline("$('html, body').animate({ scrollTop: 0 }, 120);}), $backToTopFun = function() {")
    ts.writeline("var st = $(document).scrollTop(), winh = $(window).height();")
    ts.writeline("(st > 0)? $backToTopEle.show(): $backToTopEle.hide();")
    ts.writeline("if (!window.XMLHttpRequest) {$backToTopEle.css('top', st + winh - 166);}};")
    ts.writeline("$(window).bind('scroll', $backToTopFun);$(function() { $backToTopFun(); });})();")
    ts.writeline("</script>")
	ts.writeline("</body></html>")
End Function

'-------------------------------------------------------------------------------
'  函数名称 :  ReportToReport
'  函数说明 :  将测试用例的结果信息写入已开启机制的报告
'  参数说明 :  Null
'  返回结果 :  Null
'-------------------------------------------------------------------------------
Function  ReportToReport
	'获取Dic_Filter所有的键值
	sKeys = Dic_Filter.Keys 
	'遍历所有的键值,将测试用例的结果信息写入已开启机制的报告
	For Each sKey In sKeys 
		Select Case sKey
			Case "Html" 
				Call ReportToHtml
			Case "Excel" 
				Call ReportToExcel
		End Select 
	Next 
End Function

'-------------------------------------------------------------------------------
'  函数名称 :  ReportToExcel
'  函数说明 :  将测试用例结果信息写入Excel文件
'  参数说明 :  Null
'  返回结果 :  Null
'-------------------------------------------------------------------------------
Function ReportToExcel
	Dim oSheet,oWorkBook 
	Dim Row, TCRow
	Set oExcel=CreateObject("Excel.Application")
	Set oWorkBook = oExcel.Workbooks.Open (Str_ExcelReportFile)
	Set oSheet = oExcel.Sheets("测试概要")
	'选择Excel文件中,“测试概要”sheet表
	oExcel.Sheets("测试概要").Select
	With oSheet 
	'获取当前测试步骤行
		Row = .Range("E5").Value + 2*.Range("C5").Value + 3 
		'获取当前测试用例行
		TCRow = .Range("C5").Value + 11
		.Cells(TCRow, 2).Value = Dic_CaseResult.Item("CaseName")
		'为测试用例名添加链接
		oExcel.ActiveSheet.Hyperlinks.Add oSheet.Cells(TCRow, 2), "", "测试结果!B" & Row, Dic_CaseResult.Item("CaseName")
		.Cells(TCRow, 3).Value = Dic_CaseResult.Item("TestResult")
		.Cells(TCRow, 4).Value = Dic_CaseResult.Item("CheckPointCount")
		.Cells(TCRow, 5).Value = Dic_CaseResult.Item("CaseDesc")
		.Cells(TCRow, 6).Value = Dic_CaseResult.Item("Author")
		Select Case Dic_CaseResult.Item("TestResult")
			Case "Fail"
				.Range("C" & TCRow).Font.ColorIndex = 3
			Case "Pass"
				.Range("C" & TCRow).Font.ColorIndex = 10
		End Select

		'设置测试用例信息单元格边框
		.Range("B" & TCRow & ":F" & TCRow).Borders(1).LineStyle = 1 
		.Range("B" & TCRow & ":F" & TCRow).Borders(2).LineStyle = 1
		.Range("B" & TCRow & ":F" & TCRow).Borders(3).LineStyle = 1
		.Range("B" & TCRow & ":F" & TCRow).Borders(4).LineStyle = 1
		'设置测试用例信息单元格背景色
		.Range("B" & TCRow & ":F" & TCRow).Interior.ColorIndex = 19
		.Range("B" & TCRow & ":F" & TCRow).HorizontalAlignment = 3
		'设置测试用例信息单元格字体颜色
		.Range("B" & TCRow).Font.ColorIndex = 53
		'设置测试用例信息单元格字体样式
		.Range("B" & TCRow & ":D" & TCRow).Font.Bold = True
		.Range("F" & TCRow & ":F" & TCRow).Font.Bold = True
		'设置测试用例信息单元格字体大小
		.Range("B" & TCRow & ":D" & TCRow).Font.Size=10
		.Range("E" & TCRow & ":E" & TCRow).Font.Size=9
		.Range("F" & TCRow & ":F" & TCRow).Font.Size=10
		'设置测试用例信息单元格自动换行
		.Range("B" & TCRow & ":F" & TCRow).WrapText = True
		.Range("E" & TCRow).Font.ColorIndex=23
        
       '设置测试概要信息内容
		.Range("C3").Value = Dic_MyTest.Item("TestDate")
		.Range("E3").Value = Dic_MyTest.Item("TestStart")
		.Range("E4").Value = Dic_MyTest.Item("TestStop")
		.Range("C4").Value = GetTimeDiff(Dic_MyTest.item("TestStart"),Dic_MyTest.item("TestStop"))
		.Range("C5").Value = Dic_MyTest.Item("TotalCases_count")
		.Range("E5").Value = Dic_MyTest.Item("TotalCheckPoints_count")
		.Range("C6").Value = Dic_MyTest.Item("PassCases_count")
		.Range("E6").Value = Dic_MyTest.Item("FailCases_count")
        
		'设置测试用例信息单元格宽度为自动适应
		.Columns("B:F").Autofit
	End With
 
	'选择"测试结果"sheet表
	Set oSheet = oExcel.Sheets("测试结果")
	oExcel.Sheets("测试结果").Select
	With oSheet

		'输入测试结果内容
		.Range("B" & Row & ":F" & Row).Interior.ColorIndex = 2
		.Range("B" & Row & ":F" & Row).Merge 
		.Range("B" & Row).Value ="测试用例名:"&VbTab&Dic_CaseResult.Item("CaseName")
		'设置每个测试用例的标题格式
		.Range("B" & Row & ":F" & Row).Interior.ColorIndex = 47
		.Range("B" & Row & ":F" & Row).Font.ColorIndex = 2
		.Range("B" & Row & ":F" & Row).Font.Bold = True
		.Range("B" & Row & ":F" & Row).Font.Size = 14


		keys_ = Dic_CaseResult.Keys  
		'遍历所有的检查点,依次输入检查点信息
		For i = Dic_CaseResult.Count - CInt(Dic_CaseResult.Item("CheckPointCount"))+1  To Dic_CaseResult.Count
			names_ = keys_(i-1)
			Row = Row + 1
			.Range("B" & Row).Value =Dic_CaseResult.Item(names_).Item("CP_Name")
			.Range("C" & Row).Value = Dic_CaseResult.Item(names_).Item("CP_Result") 
			.Range("C" & Row).Font.Bold = True     
			Select Case Dic_CaseResult.Item(names_).Item("CP_Result") 
				Case "Pass"
				.Range("C" & Row).Font.ColorIndex = 10
				Case "Fail"
				.Range("C" & Row).Font.ColorIndex = 3
			End Select
			.Range("C" & Row).Font.Bold = True
			.Range("D" & Row).Value = Dic_CaseResult.Item(names_).Item("CP_ExpectedValue")
			.Range("E" & Row).Value = Dic_CaseResult.Item(names_).Item("CP_ActualValue")
			.Range("F" & Row).Value = Dic_CaseResult.Item(names_).Item("CP_Detail")

			'设置边框样式以及字体大小
'			.Range("B" & Row & ":F" & Row).Borders(1).LineStyle = 1
			.Range("B" & Row & ":F" & Row).Borders(2).LineStyle = 1
'			.Range("B" & Row & ":F" & Row).Borders(3).LineStyle = 1
			.Range("B" & Row & ":F" & Row).Borders(4).LineStyle = 1
			.Range("B" & Row & ":F" & Row).VerticalAlignment = -4160
			.Range("B" & Row & ":F" & Row).Interior.ColorIndex = 15
			.Range("B" & Row & ":F" & Row).Font.Size = 10
		Next
	End With
	oExcel.Sheets("测试概要").Select
	oWorkBook.Save
	oExcel.Quit
	Set oSheet=Nothing
	Set oExcel=Nothing
	Set oWorkBook = Nothing
End Function

'-------------------------------------------------------------------------------
'  函数名称 :  ReportToHtml
'  函数说明 :  将测试用例结果信息写入Html文件
'  参数说明 :  Null
'  返回结果 :  Null
'-------------------------------------------------------------------------------
Function ReportToHtml
	CheckPointOrder=1
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set ts = fso.OpenTextFile(Str_HtmlReportFile,8,True)
	ts.writeline("<div class='commonWrap_2'>")
	ts.writeline("<table class='Testcase'><tr><td width ='100%' ><table width='99%' align='center'><tr>")
	ts.writeline("<td width='2%'>")
	ts.writeline("<div class ='foldingbutton' id='suite"&Dic_MyTest.Item("TotalCases_count")&"_foldlink' onclick="&Chr(34)&"toggle_child_visibility('suite"&Dic_MyTest.Item("TotalCases_count")&"');"&Chr(34)&"style='display: none;'><div class='close'></div></div>")
	ts.writeline("<div class ='foldingbutton' id='suite"&Dic_MyTest.Item("TotalCases_count")&"_unfoldlink' onclick="&Chr(34)&"toggle_child_visibility('suite"&Dic_MyTest.Item("TotalCases_count")&"');"&Chr(34)&"style='display: block;'><div class='open'></div></div></td>")
	ts.writeline("<td class ='Case_Name_1'>[ 用例名称 ]:</td><td class ='Case_Name_2'>"& Dic_CaseResult.Item("CaseName")&"</td>")
	ts.writeline("<td class ='Case_Result'>[ 测试结果 ]:</td>")
	If  Dic_CaseResult.Item("TestResult") = "Pass" Then
		ts.writeline("<td class ='Case_Result_Pass'>"&Dic_CaseResult.Item("TestResult") &"</td>")
	Else
		ts.writeline("<td class ='Case_Result_Fail'>"&Dic_CaseResult.Item("TestResult") &"</td>")
	End If

	ts.writeline("<td class ='Case_Ponts_1'>[ 步骤数 ]:</td><td class ='Case_Ponts_2'>"&Dic_CaseResult.Item("CheckPointCount")&"</td>")
	ts.writeline("<td class ='Case_Desc_1'>[ 用例描述 ]:</td><td class ='Case_Desc_2'>"&Dic_CaseResult.Item("CaseDesc")&"</td>")
	ts.writeline("<td class ='Case_Author_1'>[ 设计者 ]:</td><td class='Case_Author_2'>"&Dic_CaseResult.Item("Author")&"</td>")
	ts.writeline("</tr></table></td></tr><tr>")
	ts.writeline("<td width ='100%'>")
	ts.writeline("<div id='suite"&Dic_MyTest.Item("TotalCases_count")&"_children' style='display: none;'>")
	ts.WriteLine("<span class='line_3'></span>")
	ts.writeline("<div class='commonWrap_4'>")
	ts.writeline("<table class='CheckPoint' align='center'>")
	ts.writeline("<tr><td class='CP_title' width='5%'>序号</td><td class='CP_title' width='15%'>步骤名</td><td class='CP_title' width='10%'>测试结果</td>")
	ts.writeline("<td class='CP_title' width='15%'>预期结果</td><td class='CP_title' width='15%'>实际结果</td><td class='CP_title' width='40%'>结果描述</td></tr>")

	keys_ = Dic_CaseResult.Keys
	'遍历所有的检查点,依次输入检查点信息
	For i = Dic_CaseResult.Count - CInt(Dic_CaseResult.Item("CheckPointCount"))+1  To Dic_CaseResult.Count
		names_ = keys_(i-1)
		ts.writeline("<tr class='checkpointrow'onMouseOver='mouseover(this)' onMouseOut='mouseout(this)'>")
		ts.writeline("<td class='otherertext'>"&CheckPointOrder&"</td>")
		ts.writeline("<td class='otherertext'>"&Dic_CaseResult.Item(names_).Item("CP_Name")&"</td>")
	If  Dic_CaseResult.Item(names_).Item("CP_Result") = "Pass" Then 
		ts.writeline("<td class='passcentertext'>"&Dic_CaseResult.Item(names_).Item("CP_Result")&"</td>")
	Else
		ts.writeline("<td class='failedcentertext'>"&Dic_CaseResult.Item(names_).Item("CP_Result")&"</td>")
	End If
	ts.writeline("<td class='otherertext'>"&Dic_CaseResult.Item(names_).Item("CP_ExpectedValue")&"</td>")
	ts.writeline("<td class='otherertext'>"&Dic_CaseResult.Item(names_).Item("CP_ActualValue")&"</td>")
	ts.writeline("<td class='otherertext'>"&Dic_CaseResult.Item(names_).Item("CP_Detail")&"</td></tr>")
		CheckPointOrder = CheckPointOrder+1
	Next
	ts.writeline("</tr></table></div></table></div>")
End Function

'-------------------------------------------------------------------------------
'  函数名称 :  StopReport
'  函数说明 :  停止测试报告机制
'  参数说明 :  Null
'  返回结果 :  Null
'-------------------------------------------------------------------------------
Function StopReport 
	'更新Dic_MyTest中的测试停止时间
	Dic_MyTest.Item("TestStop") = Time
	'更新Dic_CaseResult中的测试结果,以及Pass、Fail计数
	If  Dic_CaseResult.Item("TestResult") = "Pass" Then
		Dic_MyTest.Item("PassCases_count") = Dic_MyTest.Item("PassCases_count") + 1
	Else
		Dic_MyTest.Item("FailCases_count") = Dic_MyTest.Item("FailCases_count") + 1
	End If
	'将最后执行的测试用例的结果信息写入报告
	Call ReportToReport
	sKeys = Dic_Filter.Keys 
	'判断当前是否开启Htnl报告,如已开启则,更新Html中测试概要信息,以及添加Html底部
	For Each sKey In sKeys 
		If sKey = "Html" Then
			Call ReplaceHtmlInfo
			Call AddHtmlButtom
		End If
	Next 
End Function

'-------------------------------------------------------------------------------
'  函数名称 :  ReplaceHtmlInfo
'  函数说明 :  更新Html中测试概要信息
'  参数说明 :  Null
'  返回结果 :  Null
'-------------------------------------------------------------------------------
Function ReplaceHtmlInfo
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set textFile = fso.OpenTextFile(Str_HtmlReportFile, 1 )
	strText = textFile.ReadAll
	strText = Replace(strText,"%TestDate%",Dic_MyTest.item("TestDate"))
	strText = Replace(strText,"%TestStart%",Dic_MyTest.item("TestStart"))
	strText = Replace(strText,"%TestStop%",Dic_MyTest.item("TestStop"))
	strText = Replace(strText,"%TestUse%",GetTimeDiff(Dic_MyTest.item("TestStart"),Dic_MyTest.item("TestStop")))
	strText = Replace(strText,"%ToTal_TestCase%",Dic_MyTest.item("TotalCases_count"))
	strText = Replace(strText,"%Total_CheckPoints%",Dic_MyTest.item("TotalCheckPoints_count"))
	strText = Replace(strText,"%Pass_TestCase%",Dic_MyTest.item("PassCases_count"))
	strText = Replace(strText,"%Fail_TestCase%",Dic_MyTest.item("FailCases_count"))
	textFile.Close
	Set textFile1 =  fso.OpenTextFile(Str_HtmlReportFile, 2 )
	textFile1.WriteLine strText
	textFile1.Close
	Set textFile1 = Nothing
	Set fso = Nothing
End Function

'-------------------------------------------------------------------------------
'  函数名称 :  GetTimeDiff
'  函数说明 :  获取时间差
'  参数说明:   TimeStart:开始时间
'              TimeStop:结束时间
'  返回结果:   DateTime,时间差
'-------------------------------------------------------------------------------
Function GetTimeDiff(TimeStart,TimeStop)
	H=0
	M=0
	S=0
	TimeDiffSeconds=DateDiff("s",TimeStart,TimeStop)
	If (TimeDiffSeconds\3600)<> 0 Then
		H = (TimeDiffSeconds\3600)
		TimeDiffSeconds = TimeDiffSeconds - H*3600
	End If 
	If (TimeDiffSeconds\60)<> 0 Then
		M = (TimeDiffSeconds\60)
		TimeDiffSeconds = TimeDiffSeconds - M*60
	End If
	S=TimeDiffSeconds
	If H < 10 Then
		H = "0"& H
	End If
	If M < 10 Then
		M = "0"& M
	End If
	If S < 10 Then
		S = "0"& S
	End If
	GetTimeDiff = H&":"&M&":"&S
End Function 

Function Report1(status,objtype, text)
Reporter.Filter = rtEnableAll
Reporter.ReportEvent status, objtype, text
'DetailLogPrint objtype&":"&VBTab&text&"【"& status &"】"
Reporter.Filter = rfDisableAll
End Function 

End Class

 

posted @ 2012-02-16 16:37  Sirrah  阅读(459)  评论(0编辑  收藏  举报