How to Use RegisterUserFunc in UFT with Examples

RegisterUserFunc (register user function) is one of the most powerful features of UFT enabling you to add new methods to test object classes or change the behavior of an existing test object method during a run session. In simple terms using RegisterUserFunc we can override the functionality of inbuilt methods of UFT One. When RegisterUserFunc is used, UFT gives precedence to your user-defined function over the inbuilt function of UFT as a method of the specified test object class for the rest of the run session, or until you unregister the method.

Let’s try to understand it with an example. Consider a scenario of a list box, wherein you want to select an item from it. Before selecting the item you want to perform the following checks.

  • Check whether list box exists
  • Check whether the list box is enabled
  • Check whether the required item exists in the list box

This can be easily achieved by temporarily overriding the functionality of the Select method of UFT that will perform the required checks and if everything is okay UFT will select the required item from the list box.

The syntax of RegisterUserFunc is as follows
RegisterUserFuncTOClass, MethodName, FunctionName, SetAsDefault

Argument Type Description
TOClass String The test object class for which you want to register the method. e.g: Weblist, WebButton, etc
MethodName String The method you want to register. e.g: For a WebList, it can be Select
FunctionName String The name of your user-defined function.e.g SelectWebList for the Select method. You can provide any name. The function can be located in your action or in any function library file associated with your test.
SetAsDefault Boolean This is an optional valueIt indicates whether the registered function should be used as the default operation for the test object.

Default value = False.

After executing the RegisterUserFunc statement, your user-defined method is recognized by UFT as a method of the specified test object class for the remainder of the run session, or until you unregister the method.

You can also register the same function to more than one test object class, using the same operation name for different test object classes, or different names.

How to use RegisterUserFunc in UFT

In this article, I will create two user-defined functions, one for selecting an item from the list box and another one to click an object like WebButton, Image, or WebRadioGroup (In this case, we will be registering the same function to more than one test object class). Put the below functions in a function library file and associate it with your test.

User-defined Function to Select an item from WebList

The following custom SelectWebList function will first check whether the list box object exists and it is enabled.After this it will check whether the required item is present in the listbox.If the required value is present in the list box,UFT will select it.

User-defined Function to Click an object

The following custom click method will first check whether or not the specified object exists and is visible.If all the conditions are met,UFT will click the object.

Now we have defined the user-defined functions. The next step would be to register them to override the UFT inbuilt method Select and Click.
Place the following code at the beginning of the function library. The Registeruserfunc statement must be executed at the beginning of your test run so that the method is immediately available for use in any test or component using that function library.

The following is the sample snapshot for reference.

RegisterUserFunc UFT

After running the below steps at Find a Flight page of Mercury application the result will look like the following snapshot. You would notice that the reporting did inside the user-defined functions is displaying in the Result Viewer.

Find Flight application uft

RegisterUserFunc UFT 2

UnregisterUserFunc | Unregister a Registered Function

You can instruct UFT at any step to stop using the current registration of the method. After unregistering the method, the functionality of the method returns to the standard UFT functionality.

UnRegisterUserFuncTOClass, MethodName
To unregister the Select method write the following code.
UnRegisterUserFunc “Weblist”,“Select”

*Note: If you register a function within a reusable action, it is recommended that you unregister the method at the end of the action

Recommended Posts

Leave a Reply