In this post, we are going to see how to use RegisterUserFunc function. QTP Supports to register custom functions for a object. To make it more clear, we can create our own function for an object/ Test Object Class and we can register the function. So, these custom functions will be registered with object's function list.
Syntax:
RegisterUserFunc (TOClass, TOMethodName, FunctionName, <Optional> SetAsDefault)
Lets see how to use this function with an example. We are going to create our own function to set a value in the text box.
'Function Name : SetValue
'Parameters : obj, newValue
'Description : To set a value in the text box
'Author : Ramesh
Function SetValue ( obj, ByVal newValue)
obj.Set newValue
Reporter.ReportEvent micPass, obj.GetRoProperty("name"), "Value Entered : " & newValue
End Function
So now our function to set value in to a text box is ready!. We have to register the function to use it.
RegisterUserFunc "WebEdit", "NewSet", "SetValue",True
Here, we are registering the function "SetValue" for the "WebEdit" object class with the name "NewSet". The last parameter "True" will set the registering function as a default function. The keyword 'NewSet' has been registered with the WebEdit class. If we place a dot after WebEdit object, "NewSet" will also be listed in the context menu.We have registered the function and lets see how to use the registered function.
Browser("Mercury Tours").Page("Mercury Tours").WebEdit("userName").NewSet "UserName"
We are using 'NewSet' method instead of 'Set' to enter value in text box which inturn call our custom created function. One important thing need to be keep in mind is that a function should be registered before accessing it. It will be safer if we have all our RegisterUserFunc scripts in the Function library instead of directly place it along with action scripts.
Here is the block of script that can directly executed to see the results!!!!
'Registering User Function 'SetValue'
RegisterUserFunc "WebEdit", "NewSet", "SetValue",True
'Function Name : SetValue
'Parameters : obj, newValue
'Description : To set a value in the text box
'Author : Ramesh
Function SetValue ( obj, ByVal newValue)
obj.Set newValue
Reporter.ReportEvent micPass, obj.GetRoProperty("name"), "Value Entered : " & newValue
End Function
'Set Value in to userName Text box
Browser("Mercury Tours").Page("Mercury Tours").WebEdit("userName").NewSet "UserName"
Also, Please note that If we are registering a function with already existing method name, then custom function will over ride existing method. Say for Instance, If we register with 'Set' method name , then existing set method will be override by custom / User defined function.
RegisterUserFunc "WebEdit", "Set", "SetValue",True