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()
{ 27 comments… read them below or add one }
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.
Sai: “ALL” means the method is trying to retrieve the count for all objects as opposed to only the visible ones..
But for images wat to do?
For images:
Hi Anshoo,
Nice article.
Could you please let me know how we can get the browser Title with the same?
Regards
Mahesh
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 = NothingIf 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 ??
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
ChildItemfunction.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
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
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.
Hi Anshoo, Thankyou for the Reply. I will check it and let you know.
Thanks,
Sai
:)
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
Hi,
First of all, I apologize for the late reply.
In this situation, why don’t you use
xandyinstead ofabs_xandabs_y?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?
Hi Vineeth,
Please see the section CheckPoints for Hidden Objects in QuickTest’s Custom CheckPoints and Beyond. This is more of a QTP-specific implementation than HTML or CSS.
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 :)
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
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….
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 ??
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 ?
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?
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 ?
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.
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
xcoordinate 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 = 0There 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.
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.