Implementing function pointers

What is a function pointer? A function pointer is a variable that stores the memory address of a block of code that is programmed to fulfill a specific function. Function pointers are useful to avoid complex switch case structures. Instead, they support direct access in runtime to previously loaded functions or class methods. This enables the construction of callback functions. A callback is, in essence, an executable code that is passed as an argument to a function. This enables more generic coding, by having lower-level modules calling higher-level functions or subroutines.

This recipe will describe how to implement function pointers in VBScript, a scripting language that does not natively support the usage of pointers.

Getting ready

Create a new function library (for instance, cls.FunctionPointers.vbs) and associate it with your test.

How to do it...

  1. Write the following code in the function library:
    Class WebEditSet
        Public Default Function Run(ByRef obj, ByVal sText)
            On Error Resume Next
            Run = 1 'micFail (pessimistic initialization)
            Select Case True
                Case    obj.Exist(0) And _
                        obj.GetROProperty("visible") And _
                        obj.GetROProperty("enabled")           
                        'Perform the set operation
                        obj.Set(sText)                   
                Case Else
                    Reporter.ReportEvent micWarning, TypeName(me), "Object not available."
                 Exit Function 
            End Select
            
            If Err.Number = 0 Then
                Run = 0 'micPass
            End If
        End Function
     End Class
  2. Write the following code in Action:
    Dim pFunctiontion
    Set pFunctiontion = New WebEditSet
    Reporter.ReportEvent pFunctiontion(Browser("Google").Page("Google").WebEdit("q"), "UFT"), "Set the Google Search WebEdit", "Done"

How it works...

The WebEditSet class actually implements the command wrapper design pattern (refer also to the Implementing a generic Login class recipe). This recipe also demonstrates an alternative way of overriding any native UFT TO method without recurring to the RegisterUserFunc method.

First, we create an instance of the WebEditSet class and set the reference to our pFunctiontion variable. Note that the Run method of WebEditSet is declared as a default function, so we can invoke its execution by merely referring to the object reference, as is done with the statement pFunctiontion in the last line of code in the How to do it… section. This way, pFunctiontion actually functions as if it were a function pointer. Let us take a close look at the following line of code, beginning with Reporter.ReportEvent:

Reporter.ReportEvent pFunc(Browser("Google").Page("Google").WebEdit("q"), "UFT"), "Set the Google Search WebEdit", "Done"

We call the ReportEvent method of Reporter, and as its first parameter, instead of a status constant such as micPass or micFail, we pass pFunctiontion and the arguments accepted by the Run method (the target TO and its parameter, a string). This way of using the function pointer actually implements a kind of callback. The value returned by the Run method of WebEditSet will determine whether UFT will report a success or failure in regard to the Set operation. It will return through the call invoked by accessing the function pointer.

See also

The following articles are part of a wider collection at www.advancedqtp.com, which also discusses function pointers in depth:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset