您的位置:首页 > 编程语言 > Qt开发

QTP场景恢复之用例失败自动截图

2014-01-13 10:59 316 查看
以下代码是在QC里运行QTP来执行脚本过程,当执行过程中发现用例失败后就会自动截图,然后把用例返回到最初始的状态,模拟了场景恢复的机制

Class QCImageErrorCapture
Dim qtpApp
Sub Class_Initialize()
Set qtpApp = CreateObject("QuickTest.Application")
If qtpApp.CurrentDocumentType = "Test" Then
qtpApp.Test.Settings.Run.DisableSmartIdentification = False'False
qtpApp.Test.Settings.Run.OnError = "Stop" '"NextStep"      "stop"
else
qtpApp.BusinessComponent.Settings.Run.OnError = "Stop"
End If
End Sub
Sub Class_Terminate()
'Check if the current test has failed. If failed then only capture screenshot
If Reporter.RunStatus = micFail Then 'and qtpApp.CurrentDocumentType = "test" 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.Test.Settings.Run.DisableSmartIdentification = True
'        msgbox "display"
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
'        Browser("title:=视博云业务全流程管理平台").Close

'        SystemUtil.CloseProcessByName "iexplore.exe"
'        isFail = True

Reporter.ReportEvent micFail, "失败截图", "失败截图", Reporter.ReportPath & "/Report/ErrorImage.png"

If qtpApp.CurrentDocumentType = "Test" Then
Browser("title:=视博云业务全流程管理平台").Page("title:=视博云业务全流程管理平台").Frame("html id:=FM_Logo").Image("file name:=icon_exit.png").Click
wait 1
Browser("title:=视博云业务全流程管理平台").Dialog("text:=来自网页的消息", "nativeclass:=#32770").WinButton("text:=确定").Click
end if

'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"
'                    print "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
'        print "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


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: