如果在测试运行时,将每一步的截图都上传至QC,那么将大量耗费QC的系统资源。通常,我们只想查看那些发生错误时的截图。当调试和运行失败后,需要查找原因时,这些截图可以迅速帮助定位问题。
通过下述步骤,可以很简单的实现上述需求。
- 在公共函数库中创建一个类,并此类的一个实例
- 在类的析构函数中判断是否发生错误并截图
- 将截图作为附件上传到QC中当前测试集的实例中。
以下是这个类的完整代码:
' Class: QCImageErrorCapture ' WebSite: http://KnowledgeInbox.com ' Author: Tarun Lalwani ' Description: This class captures the screen in case the current component fails or the current ' test fails. This is for QC only ' Parameters: N/A Class QCImageErrorCapture Sub Class_Terminate() 'Check if the current test has failed. If failed then only capture screenshot If Reporter.RunStatus = micFail Then CaptureAndAttachDesktop End If End Sub Private Sub CaptureAndAttachDesktop() 'QC is not connected If QCUtil.IsConnected = False then Exit Sub 'The test is not running from Test Lab If QcUtil.CurrentRun is Nothing Then Exit Sub On error resume next 'Hide QTP to make sure we don't get QTP in snapshot Set qtpApp = CreateObject("QuickTest.Application") qtpApp.visible = False 'GIve time for QTP to get hidden Wait 2 'Capture the screenshot to the report folder Desktop.CaptureBitmap Reporter.ReportPath & "/Report/ErrorImage.png", True qtpApp.visible = True 'Add the capture to QC Set oAttachments = QCutil.CurrentRun.Attachments Set oAttachment = oAttachments.AddItem(null) oAttachment.FileName = Reporter.ReportPath & "/Report/ErrorImage.png" oAttachment.Type = 1 'File 'Check if the current test is a QTP Test or Business Component Select Case LCase(qtpApp.CurrentDocumentType) Case "test" oAttachment.Description = "Name: " & qtpApp.Test.Name & vbNewLine & "Error: " & qtpApp.Test.LastRunResults.LastError Case "business component" oAttachment.Description = "Name: " & qtpApp.BusinessComponent.Name & vbNewLine & "Error: " & qtpApp.BusinessComponent.LastRunResults.LastError 'We can also add the Business COmponent to a User Defined Field 'QCUtil.CurrentTestSetTest.Field("TC_USER_01") = qtpApp.BusinessComponent.Name 'QCUtil.CurrentTestSetTest.Post End Select 'Add the attachment oAttachment.Post End Sub End Class 'Create the object in one of the attached libraries. When the Test or Business component ends 'the screenshot will be captured Set oErrorCapture = new QCImageErrorCapture
将上述代码直接复制到函数库中并关联当前QTP脚本,那么在运行过程中发生的错误截图就会自动上传至QC。