Different Ways To Get And Count Objects In QTP

Getting and counting the number of objects on a web page is important factor as part of Automation testing. This article explains the different ways available to get and count the objects.

Descriptive Programming:
This approach uses Description Object, which contains a mask for objects we would to get.
 
Code Snippet:
 Set oDesc = Description.Create
 oDesc("micClass").Value="Link"
 Set Links = Browser("aaa").Page("bbb").Page("ccc").ChildObjects(oDesc)
 MsgBox "Total Links" &Links.count

The method ChildObjects returns the collection of child objects matched the description ("micclass" is "Link") and containted within the object Page("ccc").

Object QTP property Collections:
As we know that QTP can work with Document Object Model(DOM).
 
Code Snippet:
 Set Links = Browser("aaa").Page("bbb").Page("ccc").Objects.Links
 MsgBox "Links Count" & Links.Length
 We used Object property of a page object which represents the HTML Document in a given browser. The HTML document contains different collections like forms, frames, images, linls. etc. We used Length property to get the number of items in the collection.

Object QTP Property & GetElementByTagName:
We can get access to the HTML Document by using Object property of the page and use its GetElementByTagName method to get the number of links on the page.
 
Code Snippet:
Set Links = Browser("aaa").Page("bbb").Object.GetElementByTagName("a")
MsgBox "Total Links" & Links.Length
 
We can use the above methods for finding the images,webedits, list of objects on a web page.