質問

I'm currently working every day with QuickTest Professional 11, which uses VBScript behind the scenes. Lately, I've started developing some of my own functions to handle common situations. I'm pretty new to VBscript, most of my programming experience is in C and Python.

I'm trying to implement Python's Try/Except in VBScript, mostly to wrap around actions like clicking links or selecting values from dropdown boxes. Here's what I have so far:

Class cls_ErrorHandler
Private bWasError

Private Sub Class_Initialize()
    bWasError = False
End Sub

Private Sub IsErr
    If Err.Number <> 0 Then
        bWasError = True
    Else
        bWasError = False
    End If
            Err.Clear
End Sub

' If the command fails, set bWasError
Public Sub Try(strCommandToTry)
    On Error Resume Next
    Execute(strCommandToTry)
    me.IsErr

    On Error Goto 0
End Sub

Public Sub Except(strCommandInCaseOfError)
    If bWasError Then
        Execute(strCommandInCaseOfError)
    End If
    bWasError = False
End Sub
End Class

I'd like to be able to write things like this:

Set oErrorHandler = New cls_ErrorHandler
oErrorHandler.Try(Stringify(Browser("Browser Name").Page("Page Name").WebCheckBox("Checkbox Name").Set "ON"
oErrorHander.Except(Stringify(Browser("Browser Name").Page("Page Name").WebButton("Save").Click))

As far as I can tell, there really isn't any nice way to pass a function as an argument to another function in VBScript. The best way seems to be to pass a string containing the name of a function, and then feed that string into Execute() or Eval(). Objects in QuickTest Professional tend to have lots of quotation marks, so escaping them all by hand would make the code unreadable.

In C, I'd use something like this:

#define Stringify(obj) #obj

and it would be done... but nothing like that seems to exist in VBScript. Is there any way to implement this? Is there anything in VBScript that takes an object as its input and returns a string representation of that object's name? Would it be possible to write a DLL in C/C# that would provide this kind of functionality?

役に立ちましたか?

解決

You can use GetRef to obtain a function/sub pointer, and call the function/sub using that pointer. See online help, it shows an example for an event handler, but you can refer to any global function or sub with GetRef, and use the variable holding the GetRef return value just like an alias for the sub/function.

Be aware, however, that there are cases you won't be able to cover with this:

  • You cannot use a GetRef function pointer if the current calling stack contains a method call that is a function registered to a test object via RegisterUserFunc.

  • You cannot call such a test object method from within a routine call that was adressed via a GetRef function pointer.

Also consider using ExecuteGlobal instead of Execute so the code you pass can set global variables that the ExecuteGlobal caller can access afterwards.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top