Google Search Results
You arrived here after searching for the following phrases:
Click a phrase to jump to the first occurrence, or return to the search results.
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.
'
'Comment:
' It’s possible to register this function to any object class using:
' RegisterUserFunc <Class>, "Exist", "Exists"
'
'Arguments:
' ByRef obj - Any Object
' ByVal intTimeoutMSec
'
'Returns:
' True - if object is loaded 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
Set objTimer = MercuryTimers.Timer("ObjectExist")
objTimer.Start
Do
Exists = obj.Exist(0)
If Exists 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. 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.
Posted in Meir Bar-Tal's Blog, QTP Hacks


Meir Bar-Tal




May 19th, 2008 at 5:27 pm
This is good. I had never tried this before because I didn’t understand how it would work.
If obj is a Browser object, and browser.exist is registered to exists, I thought the line “Exists = obj.Exist(0)” would just call itself and create an infinite loop. Now that I have tried it, I see that it is actually calling the original exist function.
May 26th, 2008 at 6:42 am
This was very useful and good.
May 29th, 2008 at 1:06 am
Very useful, Thanks guys.
I found that the “unreliability” of the exists method seems to occur alot more when you embed actions within your QTP Tests. i.e. Using a “RunAction” to call another QTP test (usually a component).
I contacted HP (or then Mercury) and they have more of less said it may be a bug.
July 21st, 2008 at 3:59 pm
why can’t we use if else along with exist statement.
My opinion is
if object hierarchy.exist then
‘do the following
….
….
….
else
‘do the following
….
….
….
end if
July 23rd, 2008 at 8:40 pm
I’d like to know what sampling values are suggested for this function. Is there any consequence for letting the function run at top speed?
July 23rd, 2008 at 11:10 pm
I’m using 0 seconds, 250 milliseconds. Too fast is dangerous because QTP needs time to get a response from the OS.
February 10th, 2009 at 3:20 pm
[…] methods of [test] object classes with custom functions tailored to serve specific purposes” (Bar-Tal, 2008b). Whilst the advantages of this feature were already reviewed in the articles mentioned and quoted […]
February 13th, 2009 at 8:42 pm
First to clarify the mistake that beginners usually make. .Exist(1) will not wait one second then move on. There is actually a setting in the Test’s Settings called Object synchronization Timeout or OST. By Default, the OST is set to 20 seconds. So when QTP encounters the .Exist(1), it will actually wait 1 second, then an additional 20 seconds before it fails the Exist, for a total of 21 seconds.
While the above function is great and a great use of overriding existing methods, here is a suggestion that might improve upon it. Use the QTP AOM (Automated Object Model) during runtime to hook into the existing Object Synch Timeout and set it to 0. One benefit I can see is removing the need to maintain the Timeout environment variable. This script below will check if MyApp is open, if not, open it. I think you can incorporate the AOM code into the function to replace your homegrown Timeout Code.
Dim app
Set App = CreateObject(”QuickTest.Application”) ‘create a qtp object (which hooks into the open QTP session)
App.Test.Settings.Run.ObjectSyncTimeOut = 0 ’set the OST to 0
‘Open Internet Explorer
If Browser(”MyWebApp”).Page(”MyWebApp”).Exist(1) = False Then ‘This step causes QTP to only wait the 1 second
SystemUtil.Run “C:\Program Files\Internet Explorer\iexplore.exe”,”",”C:\”,”open” ‘opens IE after (1) second instead of (21)
End If
App.Test.Settings.Run.ObjectSyncTimeOut = 20000 ‘reset the OST to the default.
The reason you don’t want to just set the default timeout to 0 for all objects is because sometimes you want to wait for that default 20 seconds in case your pc is hiccuping, hard drive spinning, dust in the cpu, Gremlins, etc. But its definitely useful when you just want to check quickly if something is there.
February 13th, 2009 at 11:14 pm
Thanks for your comment.
I’ve got several remarks on the points you’ve raised:
1) The example given by you is not appropriate, since you’re expecting the Browser NOT to exist, which is the opposite of the goal of using the Exist property (method). Indeed in this case we would like the function to exit immediately when detecting that the object doesn’t exist. For this reason I’ve designed an additional registered function called NotExists which does exactly this (you can read the article here - http://www.advancedqtp.com/knowledge-base/articles/qtp-tricks4/qtp-hacks/add-new-methods-to-objects/ - or in my blog).
2) Creating a QTP COM object for every such case to reset the OST is time consuming and there are other ways to control it. For instance, I use my own configuration object (which has the default sync time, etc.) instead of the QTP environment and the OST is preset to zero anyway. I don’t let QTP any control over what happens during the run session anyway. And as I answered in the past to someone who talked about, I think, an Elephant running across the screen in pajamas, then let me tell you that an employee that sees Gremlins on his monitor won’t last too long on the job… The test environment needs to be carefully built to minimize the chance of such incidents which may add noise to the test results.
March 4th, 2009 at 8:24 am
Anyhow method exist is Used in the code again>>>>
March 4th, 2009 at 9:21 am
Nir, either I don’t get your point, or you didn’t get mine. All methods registered with RegisterUserFunc in QTP usually call the native QTP method. The idea of overriding doesn’t necessarily mean that the basic functionality should no longer be used, but rather that one can customize the original method according to specific needs (as I said in the article).