QTP: Get Object Count

by Anshoo Arora ON August 10, 2009 · Posted In All, QTP · 27 comments

This article was revised (and rewritten) on 03/16/2012.

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 code in this article shows how to pass the test object itself to get the number of its occurrences in the target app.

The function below takes the target test object as input and returns the number of its occurences found in the app.

Code
' Args:
'    Object - Test Object
'
' Output:
'    Integer

Function GetObjectCount(ByVal Object)
    GetObjectCount = -1
 
    'If Object exists, that means only a single instance is present
    If Object.Exist(0) Then
        GetObjectCount = 1
        Exit Function
    End If
 
    Dim Parent, ClassName, TOProperties, oDesc, i
 
    'Gets the parent object
    Set Parent = Object.GetTOProperty("parent")
    'Gets the className
    ClassName = Object.GetTOProperty("micclass")
 
    'Returns a collection of type DispPropertyCollection
    Set TOProperties = Object.GetTOProperties()
 
    'If with index:=0 the object exists, means there are multiple instances present
    Set oDesc = Description.Create
 
    For i = 0 To TOProperties.Count - 1
        oDesc.Add TOProperties(i).Name, TOProperties(i).Value
    Next
 
    GetObjectCount = Parent.ChildObjects(oDesc).Count
End Function
Usage
Set target = Browser("title:=Google").Page("micclass:=Page").WebEdit("name:=q")
 
MsgBox GetObjectCount(target)

Similarly, you can use RegisterUserFunc and register this function to the target classes, as shown below.

'RegisterUserFunc TOClass, TOMethodName, FunctionName, [optional] SetAsDefault
RegisterUserFunc "WebEdit", "GetObjectCount", "GetObjectCount"
 
MsgBox Browser("title:=Google").Page("micclass:=Page").WebEdit("name:=q").GetObjectCount()

Subscribe to Relevant Codes (by Anshoo Arora)

Hello! We're always posting interesting articles on Relevant Codes. Why not subscribe so you don't miss out?

Leave a Comment

{ 27 comments… read them below or add one }

Sai June 21, 2011 at 6:08 am

Hi Anshoo,
While I was going through your article, I got a doubt about what the following statements in the above code will check for?
1. If Not CountType = “ALL” Then____
2. refClassCount.CountType = “ALL” and refClassCount.CountType = “”
Please explain. Thank you.

Reply

Anshoo Arora June 24, 2011 at 11:11 am

Sai: “ALL” means the method is trying to retrieve the count for all objects as opposed to only the visible ones..

Reply

Arun December 28, 2010 at 6:58 am

But for images wat to do?

Reply

Anshoo Arora January 2, 2011 at 7:07 pm

For images:

'This is where the objects are being searched for:
Set BaseObject = Browser( "Browser" ).Page( "micclass:=Page" )

' Retrieve the total number of Image Objects
MsgBox GetObjectCount( BaseObject, "Image" )

' Retrieve the total non-hidden number of Image Objects
MsgBox GetVisibleObjectCount( BaseObject, "Image" )

Reply

Mahesh Upadhyay December 20, 2010 at 3:48 am

Hi Anshoo,
Nice article.
Could you please let me know how we can get the browser Title with the same?
Regards
Mahesh

Reply

Anshoo Arora December 23, 2010 at 3:28 pm

Mahesh,

Please see below:

Dim desc, colBrowser, ix

Set desc = Description.Create
desc("micclass").Value = "Browser"

Set colBrowser = Desktop.ChildObjects(desc)

For ix = 0 to colBrowser.Count - 1
	MsgBox colBrowser(ix).GetROProperty("title")
Next

Set desc = Nothing
Set colBrowser = Nothing

Reply

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

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

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

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

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

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

Anshoo Arora March 24, 2010 at 1:50 pm

:)

Reply

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

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

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

Anshoo Arora February 24, 2010 at 8:45 am
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

PJ July 8, 2011 at 10:16 am

I had similar problem when i have writen class defination in .vbs and tried to create the object of that class inside qtp action
though i had associated function library in test

But when i write statement ExecuteFile “filename.vbs” i didnt get that object reqiured error

Reply

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

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

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

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

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

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

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

Anshoo Arora July 16, 2011 at 11:46 am

PJ: You can also create the instance as a Public Function which will be visible in all Actions. I have seen ExecuteFile sort this issue, but unfortunately, you lose on all debugging capabilities.

Reply

Previous post:

Next post: