By Anish Pillai Anish Pillai Posted under QTP Concepts

How to achieve Method Overriding & Reuse using RegisterUserFunc in QTP

2 Flares Twitter 0 Facebook 0 Google+ 2 LinkedIn 0 Email -- 2 Flares ×

Method Overriding is one of the core features of Object Oriented Programming where you can replace the original/default implementation of a function with your own implementation. In other ways, your new function would override the code or logic of the original function. As an example, consider the addition operation which would give you the sum of a set of numbers. To override this operation, you can create a function (say ‘fnSum’) which would not only find the sum of numbers, but it would also save the sum in an external file or maybe display in a message box. Let’s see how you can do this in QTP.

Method Overriding & reuse in QTP

Method Overriding in QTP

You can use QTP’s RegisterUserFunc statement to achieve method overriding in your scripts. Consider a scenario where you want to click on a web button in your application. Now you can override the default Click method with a function that first checks whether the web button exists or not. If it exists, then it checks that the button should be enabled. After both these validations are done, comes your code to click on the button. Let’s see the sample code for this scenario –

'===== Code in Function Library =====
RegisterUserFunc "WebButton", "fnBtnClick", "fnBtnClick"

'Function Definition
Function fnBtnClick(objControl)

'Check if object exists
If objControl.Exist Then
 'Check if the object is enabled or not
 If objControl.GetROProperty("disabled") <> 0 Then
  'Click on the button
 End If
End If

End Function

'===== Sample code in your action =====

From the above code, you would see that we have created a function fnBtnClick which overrides the default click operation. This function clicks on the button only when you it exists in the application as well as it is enabled.

Method Reuse in QTP

This is another cool feature that you can actually implement using RegisterUserFunc statement. This concept is slightly different from the normal function reuse where you create a function for a reusable scenario and then call this function wherever required. With RegisterUserFunc, you create a generic function and then bind this function with different controls. For example, you can create a generic fnClick function and then bind the function with a web button, link, image, web element etc. This way you can use the same function for different controls.

'===== Code in Function Library =====
RegisterUserFunc "WebButton", "fnClick", "fnClick"
RegisterUserFunc "Link", "fnClick", "fnClick"
RegisterUserFunc "Image", "fnClick", "fnClick"

'Function Definition
Function fnClick(objControl)
End Function

'=====Code in Action =====

In the above example, we have created a single function fnClick which is being used by different controls like Web Button, Link, Image etc.

If you enjoyed this article, you can join our blog to get free email updates directly in your inbox.

2 Flares Twitter 0 Facebook 0 Google+ 2 LinkedIn 0 Email -- 2 Flares ×
  • Nikhil

    It is a good practise if you create such functions Becuse Evey time Your function will check existance and Enable/ Distable Property before prforming any operation.

    • Anish10110

      Yes.. Even I agree with this. Its always better to include these validations in the code. Also it becomes easy when we add this in custom functions. This way we'll have the code at one place, so making changes will also become easy.

      • dinesh

        hello sir, just now i join this blog…
        registeruserfunc is used for only button this small operations sir, For ex i need to use 20 lines of reuse code in one Action, can we use registeruserfunc?
        for ex: scenario…1) login means we require 2 ips..
        2)for ex: 20 lines of reusable code having edit boxes listboxes.buttons , webelements…etc? for normal function we easy to write this? but how to write this big reusable code Using registeruser func

  • nai

    thank you very much, really appreciated for everything.

  • ramesh


  • kabir

    I follow your article to improve my knowledge as it is simple but full of information.

    I am little confuse about following statement:
    RegisterUserFunc "WebButton", "fnBtnClick", "fnBtnClick"

    For TO "WebButton" one avaliable method is Click, I am wondering if I use the following statement is it correct or not?
    RegisterUserFunc "WebButton","Click","fnBtnClick"

    I don't get why I should use fnBtnClick? Is there any method named 'fnBtnClick' ?

    Your reply would be highly appreciable.

    Thank you in advance.


    • Anish10110

      emailed you..

  • sam

    I am fairly new to QTP and was wondering if intellisense should work when I am writing the function in the function library. In my case I cant see any of the properties for objControl

    • Anish10110

      No, it doesnt work in function library. What I used to do for this was, write code in action and then move it to function library.

  • testrookie

    I used this fnBtnClick function, but when I use it, It only checks if the button exist, and does not actually click the button, why is this so ? Any solution to this please ?

  • Sreenu

    Hi Anish,

    I need a small clarification on how you write custom display messages using ReporterEvent in Method reuse.

    E.g., I've created the following function in my Function Library –

    '———————– Click Operation ——————————–
    'Regsitering User Defined Functions
    RegisterUserFunc "WebButton", "fnWebButtonClick", "fnClick"
    RegisterUserFunc "Link", "fnLinkClick", "fnClick"
    RegisterUserFunc "WebElement", "fnWebElementClick", "fnClick"

    Function fnclick(ObjControl)
    Reporter.ReportEvent micpass, "Some Custom Text saying that the WebButton / Link / WebElement has been clicked", "Passed"
    End Function

    In the above function, how does the script know whether the click operation has been performed on WebButton / Link / WebElement?
    How can we tell the script to display such message?

    Please help me in this regard.


    • Anish10110

      There is a command called ToString() that you can use here. For example you can use the following statement –

      Reporter.Report micPass, objControl.ToString() & " clicked", "Passed"

      The above statement will show if a link or button or something else is clicked. There are some other functions also like toString() but I currently don't remember the names. You can have a look at those from the help files.

  • suresh

    Hi Anish,

    I could not understand the following piece of code

    Function fnClick(objControl)


    End Function

    '=====Code in Action =====


    Here for the function fnClick we are passing ObjControl as argument..But in script we are just writing a fnClick..How it will call that function?..Please explain

  • Pingback: Designing Data Driven Framework in QTP – Part 2 | Tech Lessons()

2 Flares Twitter 0 Facebook 0 Google+ 2 LinkedIn 0 Email -- 2 Flares ×