Add New Methods to Objects
Posted by admin - May 30, 2008 Articles, Meir Bar-Tal 0 0 Views : 601 Receive Updates For This Category
Article Tools
- Print this page
- Add Comment
- Send to Friend
- Last Updated on :
Aug 17, 2011
This short article complements a previous one I have published on how to Override the Object Exist Property. As previously indicated, one of the most powerful features QuickTest Professional (QTP) provides is the ability to override methods of object classes with custom functions tailored to serve specific purposes (Bar-Tal, 2008b). In this brief article we will see that it is also technically possible to add object methods, thus expanding the ways by which objects can be handled. For example, one might wish to get the background color of a row in a WebTable or ListView, or to check that if specific values are displayed, the font color and style are as expected. This is obviously very useful, since again we can gain from registering such generic functions and reusing them afterwards across tests, applications and projects.
The method for implementing such an extensibility follows the same lines outlined in Yaron Assa’s “A Fresh Look on RegisterUserFunc” (2008) and my own previous article (Bar-Tal, 2008b) in which I explained the motivation to override the objects’ Exist property.
The implementation steps are:
-
Analyze the requirement (to which object classes it’s relevant, etc.);
-
Design the solution;
-
Code the function (keep in mind that any function to be registered to an object class must get a ByRef obj argument as in the example below);
-
Test the function;
-
Register the function to the required object classes;
-
Test that it is added to the object class or classes using the QTP editor’s intellisense or by inserting a step (pressing F7 in QTP GUI during design time). Test that using the function this way works properly.
The example below shows a NotExists function that complements the Exist property overriding mentioned in Bar-Tal, 2008b. Addressing the Analyze phase suggested above, it might be questioned why one needs a separate registered function to check for object inexistence instead of using the aforementioned Exists function with a Negation (Not) command. The need is fundamental, as when one wishes to check for a Window or Browser having closed properly. Well, again the motivation stems from QTP’s weird and unreliable behavior with respect to this basic object detection functionality. I tried to use it this way (i.e., by putting a Not obj.Exists(0) statement) and actually QTP got stuck in too many cases. My conclusion so far is that since using the Exist functionality is basically for the purpose of detecting the existence of an object (i.e., that it is indeed loaded to memory), then to check for object inexistence one needs a similar mechanism to the proposed Exists, but with a reversed logic.
So, the following function emerged:
'*******************************************************************************
Public Function NotExists(ByRef obj, ByVal intTimeoutMSec)
'*******************************************************************************
'Function : NotExists
'Waits until an object is unloaded or the specified timeout expires.
'
'Comment : It's possible to register this function to any object class using:
' RegisterUserFunc <Class>, "NotExists", "NotExists"
'
'Arguments : ByRef obj - Any Object
' ByVal intTimeoutMSec
'
'Returns : True - if object unloaded within the specified timeout.
' False - if the timeout expired.
'
'Developed By: Meir Bar-Tal
'
'Date : 14-May-2008
'
'*******************************************************************************
Dim objTimer
If Not IsNumeric(intTimeoutMSec) Then
intTimeoutMSec = Environment("DEFAULT_TIMEOUT_MSEC")
End If
'Ensure the Timeout argument is an integer (Environment vars are parsed as strings)
intTimeoutMSec = CInt(intTimeoutMSec)
Set objTimer = MercuryTimers.Timer("ObjectExistTimer")
objTimer.Start
Do
NotExists = Not obj.Exist(0)
If NotExists Then
objTimer.Stop
Exit Do
End If
Wait Environment("DEFAULT_SAMPLING_INTERVAL_SEC"), _
Environment("DEFAULT_SAMPLING_INTERVAL_MSEC")
Loop Until objTimer.ElapsedTime > intTimeoutMSec
objTimer.Stop
'*******************************************************************************
End Function
'*******************************************************************************
“Actually here the Exist property is used in a special way: an immediate response is requested every time, and the sampling interval (i.e., the delay between each time QTP checks for object existence) is controlled using Environment variables” (Bar-Tal, 2008b). As far as observed, this function works reliably and QTP does not get stuck when using the function.
As pointed out in the articles mentioned above, one great benefit of method registration is the ability to maintain code very efficiently. So, to be able to continue to use this function across tests, applications and projects without changing a single line of code, all we need to do is, taking the Browser class for example:
RegisterUserFunc "Browser", "NotExists", "NotExists"
Of course, this must be done for each object class as required.
To summarize, QTP’s RegisterUserFunc method enables also to add object methods. The case of adding an overridden version of checking for unloaded objects with the unreliable Exist property was briefly introduced and a way of implementing and registering such a method was exemplified.


