以下代码是在QC里运行QTP来执行脚本过程,当执行过程中发现用例失败后就会自动截图,然后把用例返回到最初始的状态,模拟了场景恢复的机制
1 Class QCImageErrorCapture 2 Dim qtpApp 3 Sub Class_Initialize() 4 Set qtpApp = CreateObject("QuickTest.Application") 5 If qtpApp.CurrentDocumentType = "Test" Then 6 qtpApp.Test.Settings.Run.DisableSmartIdentification = False'False 7 qtpApp.Test.Settings.Run.OnError = "Stop" '"NextStep" "stop" 8 else 9 qtpApp.BusinessComponent.Settings.Run.OnError = "Stop"10 End If11 End Sub12 Sub Class_Terminate()13 'Check if the current test has failed. If failed then only capture screenshot14 If Reporter.RunStatus = micFail Then 'and qtpApp.CurrentDocumentType = "test" Then15 CaptureAndAttachDesktop16 End If17 End Sub18 19 Private Sub CaptureAndAttachDesktop()20 'QC is not connected21 If QCUtil.IsConnected = False then Exit Sub22 23 'The test is not running from Test Lab24 If QcUtil.CurrentRun is Nothing Then Exit Sub25 26 On error resume next27 'Hide QTP to make sure we don't get QTP in snapshot28 ' Set qtpApp = CreateObject("QuickTest.Application")29 ' qtpApp.Test.Settings.Run.DisableSmartIdentification = True30 ' msgbox "display"31 qtpApp.visible = False32 33 'GIve time for QTP to get hidden34 Wait 235 36 'Capture the screenshot to the report folder37 Desktop.CaptureBitmap Reporter.ReportPath & "/Report/ErrorImage.png", True38 qtpApp.visible = True39 ' Browser("title:=视博云业务全流程管理平台").Close40 41 ' SystemUtil.CloseProcessByName "iexplore.exe"42 ' isFail = True43 44 Reporter.ReportEvent micFail, "失败截图", "失败截图", Reporter.ReportPath & "/Report/ErrorImage.png"45 46 47 If qtpApp.CurrentDocumentType = "Test" Then48 Browser("title:=视博云业务全流程管理平台").Page("title:=视博云业务全流程管理平台").Frame("html id:=FM_Logo").Image("file name:=icon_exit.png").Click49 wait 150 Browser("title:=视博云业务全流程管理平台").Dialog("text:=来自网页的消息", "nativeclass:=#32770").WinButton("text:=确定").Click51 end if52 53 'Add the capture to QC54 ' Set oAttachments = QCutil.CurrentRun.Attachments55 ' Set oAttachment = oAttachments.AddItem(null)56 ' oAttachment.FileName = Reporter.ReportPath & "/Report/ErrorImage.png" 57 ' oAttachment.Type = 1 'File58 59 'Check if the current test is a QTP Test or Business Component60 ' Select Case LCase(qtpApp.CurrentDocumentType)61 ' Case "test"62 ' print "test"63 ' oAttachment.Description = "Name: " & qtpApp.Test.Name & vbNewLine & "Error: " & qtpApp.Test.LastRunResults.LastError64 ' Case "business component"65 ' oAttachment.Description = "Name: " & qtpApp.BusinessComponent.Name & vbNewLine & "Error: " & qtpApp.BusinessComponent.LastRunResults.LastError66 ' 67 ' 'We can also add the Business COmponent to a User Defined Field 68 ' 'QCUtil.CurrentTestSetTest.Field("TC_USER_01") = qtpApp.BusinessComponent.Name69 ' 'QCUtil.CurrentTestSetTest.Post70 ' End Select 71 ' 72 ' 'Add the attachment73 ' oAttachment.Post74 ' print "post"75 End Sub 76 End Class77 78 'Create the object in one of the attached libraries. When the Test or Business component ends 79 'the screenshot will be captured80 Set oErrorCapture = new QCImageErrorCapture