新的Reporter功能
同样有助于我们更灵活地编写测试脚本的是Reporter对象的ReportEvent功能,新的ReportEvent语法定义如下:
Reporter.ReportEvent EventStatus, ReportStepName, Details [, ImageFilePath]
很明显,添加了在测试报告中插入图像的功能。例如下面的代码使用ReportEvent来让测试报告包含运行过程中捕获的图片:
Browser("Browser").Page("WebPage").Image("MyLogo").CaptureBitmap("MyLogo.bmp")
Reporter.ReportEvent micDone, "Display Logo", "This is my logo", "MyLogo.bmp"
而在之前的版本,要实现类似的功能,则需要自己动手编写函数,例如:
Function CaptureBitmapAndSaveToLog( w_Snder, micStatus, titleMessage )
Dim dateTimeNow, fileNameStr, detailsStr
' 用时间作为截图的文件名
dateTimeNow = DotNetFactory.CreateInstance( "System.DateTime" ).Now.ToString( "ddMMyyHHmmss" )
fileNameStr = Reporter.ReportPath & "\" & dateTimeNow & ".png"
' 调用CaptureBitmap方法进行截图
If w_Snder.Exist Then
w_Snder.CaptureBitmap fileNameStr, True
Else
Desktop.CaptureBitmap fileNameStr, True
End If
' 插入到HTML代码中
detailsStr = "<" & "<img src='" & fileNameStr & "'>"
' 通过Reporter展现在测试报告中
Reporter.ReportEvent micStatus, titleMessage, detailsStr
End Function
' Register for WEB
RegisterUserFunc "Browser","CaptureBitmapAndSaveToLog","CaptureBitmapAndSaveToLog"
RegisterUserFunc "Page", "CaptureBitmapAndSaveToLog", "CaptureBitmapAndSaveToLog"
RegisterUserFunc "Frame", "CaptureBitmapAndSaveToLog", "CaptureBitmapAndSaveToLog"
RegisterUserFunc "Dialog", "CaptureBitmapAndSaveToLog", "CaptureBitmapAndSaveToLog"
' Register for Standard Window
RegisterUserFunc "Window","CaptureBitmapAndSaveToLog","CaptureBitmapAndSaveToLog"
' Register for .NET
RegisterUserFunc "SwfWindow", "CaptureBitmapAndSaveToLog", "CaptureBitmapAndSaveToLog"
除了新的ReportEvent,QTP在测试报告方面还有不少的增强,例如"Jump to Step"功能可以让我们通过选择测试结果中的节点,右键单击"Jump to step in QucikTest"来跳转到相关的测试步骤,这对于调试测试脚本和查找定位错误非常有帮助。
在新版本的QTP中,除了HTML格式外,还可以把测试结果导出到Word和PDF。