May 18 2008

Override the Object Exist Property

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. For example, the WebEdit.Set method might be expanded to first check if a popup message is open before or after (or both) the set operation took place. This can be helpful to avoid the use of a recovery scenario which brings its performance costs with unreliable results.

This topic has already been addressed by Yaron Assa in “A Fresh Look on RegisterUserFunc” (2008). In this brief article we will see that it is also technically possible to override object properties. True, QTP provides only two properties for the GUI object classes: Exist and Object. So, it might be asked why has one to bother with a technicality that has such a limited potential use. However, the motivation to override the Exist property stems from the complex and unreliable fashion in which QTP behaves when asked about an object’s existence. For example, sometimes it seems that QTP treats the timeout as a mere recommendation, going on and on checking if the object exists, to the point that it simply gets stuck. So, after conducting some experiments the following function evolved:

Public Function Exists(ByRef obj, ByVal intTimeoutMSec)
'Function: Exists
' Waits until an object is loaded or the specified timeout expires.
' It’s possible to register this function to any object class using:
' RegisterUserFunc <Class>, "Exist", "Exists"
' ByRef obj - Any Object
' ByVal intTimeoutMSec
' True - if object is loaded within the specified timeout.
' False - if the timeout expired.
'Developed By:
' Meir Bar-Tal
' 14-May-2008
'****************************************************************************** *
    Dim objTimer

    If Not IsNumeric(intTimeoutMSec) Then
        intTimeoutMSec = Environment("DEFAULT_TIMEOUT_MSEC")
    End If

    Set objTimer = MercuryTimers.Timer("ObjectExist")


        Exists = obj.Exist(0)
        If Exists Then
            Exit Do
        End If
        Wait Environment("DEFAULT_SAMPLING_INTERVAL_SEC"), _
            Environment ("DEFAULT_SAMPLING_INTERVAL_MSEC")
    Loop Until objTimer.ElapsedTime > intTimeoutMSec
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. This function is the last version of my experiments, and as far as observed it works reliably. QTP does not get stuck when using the function.

As Yaron pointed out in the article mentioned above, one great benefit of method overriding is the ability to maintain code very efficiently. So, to be able to continue to use obj.Exist in the scripts without changing a single line of code, all we need to do is, taking the Browser class for example:

RegisterUserFunc "Browser", "Exist", "Exists"

Of course, this must be done for each object class.

To summarize, QTP’s RegisterUserFunc method enables also to override object properties. The case of the unreliable Exist property was briefly introduced and a way of implementing such property overriding was exemplified.

About Meir Bar-Tal

Meir Bar-Tal is an Automation Architect with extensive technical, educational and leadership experience in Software Design, Development and Testing. For many years Meir has helped companies establish solid foundations for their testing activities with a wide variety of software applications and systems and across different technologies and platforms. Meir is a well-known expert in Data-Driven, Keyword Driven and Hybrid methodologies and has published a number of influential articles on the applications of Design Patterns to Test Automation. For the last five years, he has been pushing forward an ambitious program towards the development of an Object Oriented automation framework named Sunscrit, a huge project which is still underway. Meir is the owner of www.AdvancedQTP.com of which he was a co-founder in 2007 and served as its Editor in Chief, Author and Forums Administrator ever since. Meir lives in Israel and cooperates closely with HP R&D and other colleagues to improve the user experience and productivity with HP Software tools.