Relevant Codes

A Test Development Resource for HP QuickTest Professional.

QTP: Retrieve The Count of Objects in a Browser Efficiently

by Anshoo Arora on August 10, 2009

Counting
Creative Commons License photo credit: danesparza

I have frequently read on forums about automation developers struggling to find the correct number of object count in their applications. The only trick at times is to isolate the number of hidden objects in the application with the number of non-hidden objects. The 2 methods below: GetObjectCount and GetVisibleObjectCount do exactly that. In short:

  • GetObjectCount: Retrieves the total count of objects on the webpage. This includes hidden + non-hidden objects.
  • GetVisibleObjectCount: This retrieves the number of non-hidden objects on the webpage.

Usage

'This is where the objects are being searched for:
Set BaseObject = Browser( "title:=Yahoo.*" ).Page( "micclass:=Page" )
 
' Retrieve the total number of Link Objects
MsgBox GetObjectCount( BaseObject, "Link" )
 
' Retrieve the total non-hidden number of Link Objects
MsgBox GetVisibleObjectCount( BaseObject, "Link" )

How its done:

Class clsClassCount
 
    Public CountType
 
    '————————————————————————————————————————————— 
    ' Name: Function GetClassCount (Public)
    ' 
    ' Purpose: 
    ' 
    ' Input:
    '     BaseObject- Object containing the ClassName objects
    '     ClassName- MicClass for which the count is being retrieved for
    ' 
    ' Output:
    '     Integer
    ' 
    ' Author: Anshoo Arora
    ' 
    '————————————————————————————————————————————— 
    Public Function GetClassCount( BaseObject, ClassName ) ' As Integer
    '————————————————————————————————————————————— 
        BaseObject.Init
        If Not BaseObject.Exist( 0 ) Then
            Reporter.ReportEvent micWarning, "GetClassCount", "BaseObject was not found."
            GetClassCount = -1
            Exit Function
        End If
 
        Dim oDesc, intCount
 
        intCount = 0
 
        Set oDesc = Description.Create
        oDesc( "micclass" ).Value = ClassName
 
        intCount = BaseObject.ChildObjects( oDesc ).Count
 
        If Not CountType = "ALL" Then
            oDesc( "x" ).Value = 0
            intCount = intCount - BaseObject.ChildObjects( oDesc ).Count
        End If
 
        GetClassCount = intCount
    End Function
 
End Class
 
Public refClassCount: Set refClassCount = New clsClassCount
 
'————————————————————————————————————————————— 
' Name: Function GetObjectCount (Public)
' 
' Purpose: Retrieve object count (visible + hidden)
' 
' Input:
'     BaseObject- Object containing the ClassName objects
'     ClassName- MicClass for which the count is being retrieved for
' 
' Output:
'     Integer
'————————————————————————————————————————————— 
Public Function GetObjectCount( BaseObject, ClassName ) ' As Integer
'————————————————————————————————————————————— 
    refClassCount.CountType = "ALL"
    GetObjectCount = refClassCount.GetClassCount( BaseObject, ClassName )
End Function
 
'————————————————————————————————————————————— 
' Name: Function GetVisibleObjectCount (Public)
' 
' Purpose: Retrieve visible objects count
' 
' Input:
'     BaseObject- Object containing the ClassName objects
'     ClassName- MicClass for which the count is being retrieved for
' 
' Output:
'     Integer
'————————————————————————————————————————————— 
Public Function GetVisibleObjectCount( BaseObject, ClassName ) ' As Integer
'————————————————————————————————————————————— 
    refClassCount.CountType = ""
    GetVisibleObjectCount = refClassCount.GetClassCount( BaseObject, ClassName )
End Function

I hope you find this article useful.

If you have any questions, please ask them in the comments section. If your query is confidential, please use the Contact Form to send me an e-mail instead.

{ 19 comments… read them below or add one }

1 PRH_Blogger August 11, 2009 at 9:15 pm

I get Run time error with your code

“object required refClasscount
refClasscount.CountType=”ALL”

I am usingg IE 7.0
I checked for Link and Webedit objects
Results for both were same

Please could you check this out ?

Reply

2 Anshoo Arora August 11, 2009 at 9:16 pm

I tested this code on several PCs and it seems to be working well.. can you please recheck and let me know if the same issue still exists?

Reply

3 Prasanna Hegde August 13, 2009 at 2:07 am

Hi Anshoo,
I too tested on my home PC and office PC on both machines I still get Error

for Code
‘============================================
Public refClassCount: Set refClassCount = New clsClassCount
‘==============================================

But If I make change as
‘==============================================
Public Function refClassCount
Set refClassCount = New clsClassCount
end function
‘==============================================
It works well

It is very strange that we are getting differences with same code on different machines
wondering what could be the reason ?

Reply

4 Prasanna Hegde August 13, 2009 at 2:15 am

Hi Anshoo
I observed that If I call the function before that class like

‘case1
‘==========================================================
Set BaseObject = Browser( “title:=Yahoo.*” ).Page( “micclass:=Page” )
‘ Retrieve the total number of Link Objects
MsgBox GetObjectCount( BaseObject, “Link” )
‘ Retrieve the total non-hidden number of Link Objects
MsgBox GetVisibleObjectCount( BaseObject, “Link” )
then
Class code …

In QTP ’s acion code I get the error that I specified …………
‘=======================================================
‘case 2:
================================================
But If I call the class first then use code like
Class code

then
Set BaseObject = Browser( “title:=Yahoo.*” ).Page( “micclass:=Page” )
‘ Retrieve the total number of Link Objects
MsgBox GetObjectCount( BaseObject, “Link” )
‘ Retrieve the total non-hidden number of Link Objects
MsgBox GetVisibleObjectCount( BaseObject, “Link” )

I do not get any error

I believe it should work even If use either ways ??

Reply

5 Prasanna Hegde August 13, 2009 at 3:46 am

Hi Anshoo
I got i
Your Public declaration should have come as in the beginning of the code ( Global statement) itself
anyway thanks now I am clear about it….

Reply

6 Anshoo Arora August 13, 2009 at 5:33 pm

Hi Prasanna,

You are right. We must implement the Function after we load the class and that is because when the function definition is being loaded into the system memory, it searches for a Class instance which hasn’t been registered for the function to load successfully. Therefore, the cause of that issue. That is why you will always see Classes executed first before they’re loaded into functions as callers. I am glad it is working for you now. Thanks for your feedback :)

Reply

7 Vineeth February 24, 2010 at 5:21 am

Hi Anshoo,
Nice site. .. Thanks for sharing your knowledge.
I have a got a doubt in the above code? : oDesc( “x” ).Value = 0 is the description which differentiates both the functions.
What is the ‘x’ property of an object? Is this the ‘x’ coordinate? And all the non-visible objects will have ‘x’ propertie as 0?

Reply

8 Anshoo Arora February 24, 2010 at 8:45 am
9 Vineeth February 25, 2010 at 12:59 am

Hi Anshoo,
Thanks for the fast reply.
Sorry to say still my doubt is not cleared after the seeing the section ‘CheckPoints for Hidden Objects’.
I could understand the Childobject method …
But I am not clear with the below code :
If Not CountType = “ALL” Then
oDesc( “x” ).Value = 0 —————-> This statment is my doubt !!!
intCount = intCount – BaseObject.ChildObjects( oDesc ).Count
End If
It will be really great if you could elaborate the propertie ‘X’. & how all the non-visible objects will have ‘x’ propertie as 0?

Thanks in advance for clearing the small doubts.

Reply

10 Anshoo Arora February 28, 2010 at 5:47 pm

Vineeth,

This is quite a QTP specific technique and does not necessary mirror how HTML works. I have usually seen that the hidden objects are seen by QTP having the x coordinate of 0. Thus, to find the hidden objects only, we substitute the value 0 in the description object..

So, let’s say I were to only find the number of hidden links in a webpage. I would create a description object of a link, and introduce a new statement by equating “x” to 0:

oDesc("x").Value = 0

There are other ways to find if the object is hidden away from the user, but this seems to be as a generic solution as there are many ways a web developer can hide objects away from users.

Reply

11 skris123 March 16, 2010 at 5:27 am

Anshoo, need a help on QTP Query.
“My Intention is i want to see all the edit fields and Lables are in same exact position when i run the test every time, so i ensure that the labels and fields are not distorted” . I used the logic

If “”Page Exists Then
absxcurrent=XXX.XXX.XXXX.GetROProperty(“abs_x”)
##Getting RO property abs_x at runtime
absycurrent=XXX.XXXX.XXXX.GetROProperty(“abs_y”)
###Here whatever the abs_x and abs_y values are there at Runtime, i am comparing it with the values that i took it with object spy”
If (absxcurrent=226 and absycurrent=333) Then
Reporter.ReportEvent micPass,
else
Reporter.reportEvent micfail

but this logic is not working on all the machines.. abs_x and abs_y are differing from machine to Machine – How to check this.

Issue 2:##
Even, the same issue is there with Browser also.Browser name is differing from machine to machine.. how to overcome it.

Please advice

Reply

12 Anshoo Arora March 18, 2010 at 1:33 pm

Hi,

First of all, I apologize for the late reply.

In this situation, why don’t you use x and y instead of abs_x and abs_y?

Reply

13 skris123 March 24, 2010 at 11:24 am

Hi Anshoo, Thankyou for the Reply. I will check it and let you know.

Thanks,
Sai

Reply

14 Anshoo Arora March 24, 2010 at 1:50 pm

:)

Reply

15 skris123 March 30, 2010 at 3:00 am

Anshoo,
I tried using x and y , abs_x and abs_y, but these coordinates are varying from machine to Machine – Script getting failed on other machines . I need to check the exact location of a text box or a label. Its a sort of GUI testing i am looking for and want to ensure that all the fields are displayed in the expected position. QTP is not the right tool for this but don’t want to giveup – looking for an option where i can get a solution.
Please suggest..
Thanks
KSK

Reply

16 Anshoo Arora April 1, 2010 at 8:21 pm

Location attributes are always tricky to work with. Instead of hard-coding these values, you can store all the expected values in a DataTable depending upon the resolution of the screen. Use QTP to find out the total resolution and pick values from the correct table at execution.

Reply

17 praveen June 6, 2010 at 9:57 am

last week,
i went for in interview…… and asked me question. in qtp.
1. how would you calculate the left side text box of the page. i am aware about the count, but no idea about the left side count…
2. There is a web table with two column. first having name and second having image. some rows doesn’t have image.. how would you calculate this.

waiting for the response.
sainipra@gmail.com

Reply

18 San August 3, 2010 at 4:14 am

If Browser name . Page name and image name is same for all the images in the 4th column of all row in a webtable. I need to click on any of the image dynamically to delete that row. Only the image’s ROProperty – sourceindex ,abs_x, abs_y is different. How to click on the particular image ??

Reply

19 Anshoo Arora August 5, 2010 at 1:59 pm

San,

Since the images are inside a WebTable, you can loop through all the rows of that particular column and click on one of the images using the ChildItem function.

Reply

Leave a Comment

Previous post:

Next post: