How to achieve Method Overriding & Reuse using RegisterUserFunc in QTP
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 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 objControl.Click End If End If End Function '===== Sample code in your action ===== Browser("Browser").Page("Page").WebButton("btnSearch").fnBtnClick
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) objControl.Click End Function '=====Code in Action ===== Browser("Browser").Page("Page").WebButton("btn1").fnClick Browser("Browser").Page("Page").Link("lnk1").fnClick Browser("Browser").Page("Page").Image("img1").fnClick
In the above example, we have created a single function fnClick which is being used by different controls like Web Button, Link, Image etc.