Thursday, June 14, 2012

Function - RegisterUserFunc

   
    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


5 comments:

  1. Thanks a lot for this description, it really helped understand this concept better than in QTP guide !

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Thank you Rameshkumar...

    but one thing what is the use of this registered user...

    can u give more explanation on this?

    ReplyDelete
    Replies
    1. Thanks Arun..!

      In general, we are using in-built functions to perform basic operations on any object in AUT (For Ex: Set:To enter value in text box. Select:To select value in list box).

      If you want to use your own function as a built-in function, you can use RegisterUserFunc. In this case, you don't need to follow the functions call syntax(Call fn_Name (args)), rather you can use it as other built in functions (object_Hierarchy.fn_name(args))

      In some cases, user wants to perform some additional operations along with built-in function. An ideal case would be having a report statement at the end of every set operation. To achieve this, we have to add ReportEvent statement next to every set statement. To avoid this, we can register a function which will perform both set value & report. Please refer the example given above where we do the same.

      If you want any of the built-in function to be overridden by your own function, you can achieve it by RegisterUserFunc. Just register the function with existing built-in function name to ge the existing method to be over ridden by your custom function.

      And at last, using this RegisterUserFunc we can reuse a same function for multiple objects. Say for instance if you create a funcion for click event, you can reuse it for button as well as image.

      Delete