TestPartner脚本错误处理的例子

The following test script is an example of TestPartner as well as VBA error handling:

Sub Main()

  On Error Resume Next

  Dim oe As TOnError
  Set oe = OnError("MyErrorFunction")

  ' Attach to Program Manager Window
  Window("Program Manager Window").Attach

  ' Try to perform the bitmap select and get the return code.
  ' Since this a failure of a method, TestPartner can control
  ' the creation of the error condition using TestPartner's
  ' error mechanism.
  Dim b As Boolean
  b = Window.BitmapSelect("MetapadIcon")
  MsgBox "Return from BitmapSelect = " & CStr(b)

  ' This statement will require the VBA version of the On Error
  ' handler because the Window() statement returns an object that
  ' will be used to Attach.  If the object is not created because
  ' the attach name could not be found in the object map, VBA
  ' generates an error so the message has to be trapped.
  On Error GoToObjectErrorSub
  b = Window("this object map does not exist").Attach
  MsgBox "Return from Window.Attach = " & CStr(b)

Exit Sub

ObjectErrorSub:

  ' Display and/or handle VBA error
  MsgBox "Err.Number = " & CStr(Err.Number) & vbCrLf & _
  "Err.Source = " & Err.Source & vbCrLf & _
  "Err.Description = " & Err.Description
  Resume Next

End Sub

Function MyErrorFunction() As tpOnErrorType

  ' Display an error
  MsgBox "Message = " & Error.Message & vbCrLf & _
  "Function = " & Error.Function & vbCrLf & _
  "SourceFile = " & Error.SourceFile & vbCrLf & _
  "SourceLine = " & CStr(Error.SourceLine)

  ' Tell TestPartner that it should execute the next statement
  MyErrorFunction = tpResumeNext

End Function

posted on 2011-04-08 23:37  TIB  阅读(300)  评论(0编辑  收藏  举报

导航