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。