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 value. It 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Function SelectWebList(ObjWebList,strText) Reporter.ReportEvent micDone,"Using override Weblist function","Temporarily Override function being called" If ObjWebList.Exist And ObjWebList.GetROProperty("disabled") = False Then Reporter.ReportEvent micPass,"Weblist Should Exist and should be Enabled","Weblist object found" If INSTR(1,ObjWebList.GetROProperty("all items"),strText,1) >0 Then Reporter.ReportEvent micPass,"Weblist Item Check","Required value "& strText &" exists in the Weblist" Else Reporter.ReportEvent micFail,"Weblist Item Check","Required value "& strText &" does not exists in the Weblist" Exit Function End If ObjWebList.Select strText 'Selecting the required item from the WebList Reporter.ReportEvent micPass,"Weblist Item should be selected","Weblist Item " & strText & " was selected" Else Reporter.ReportEvent micFail,"Weblist Should Exist","Weblist object does not exist" End If End Function |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Function CustomClick(TestObject) Reporter.ReportEvent micDone,"Using override Click function","Temporarily Override function being called" If TestObject.Exist Then Reporter.ReportEvent micPass,"TestObject Should Exist","TestObject object found" If TestObject.GetROProperty("visible") = True Then Reporter.ReportEvent micPass,"TestObject should be visible","Required TestObject is visible" Else Reporter.ReportEvent micFail,"TestObject should be visible","Required TestObject is not visible" Exit Function End If TestObject.Click Reporter.ReportEvent micPass,"TestObject should be Clicked","TestObject was Clicked" Else Reporter.ReportEvent micFail,"TestObject Should Exist","TestObject object does not exist" End If End Function |
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.
1 2 3 4 5 6 7 |
'**********Overriding Select method*************** RegisterUserFunc "Weblist","Select","SelectWebList" '************Overriding Click method*************** RegisterUserFunc "WebButton","Click","CustomClick" RegisterUserFunc "WebCheckBox","Click","CustomClick" RegisterUserFunc "WebRadioGroup","Click","CustomClick" RegisterUserFunc "Image","Click","CustomClick" |
The following is the sample snapshot for reference.
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.
1 2 |
Browser("Welcome: Mercury Tours").Page("Find a Flight: Mercury").WebList("passCount").Select "4" Browser("Welcome: Mercury Tours").Page("Find a Flight: Mercury").Image("findFlights").Click |
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.
Syntax
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
- How to Use Call Stack in UFT to Trace Function Calls with Example
- Capture Screenshots in UFT and Save them in Word Doc
- How to Use LoadAndRunAction in UFT
- How to Use Test Combinations Generator in UFT
- How to Use Recovery Scenario in UFT with Example
- Settings.WebPackage Replaytype in UFT
- Descriptive Programming in UFT with Examples
- How To Use Dictionary Object in UFT With Examples