Monday, 21 May 2012

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.

1 comments:

Raj Kumar said...

to get an HTML source code of Web page
HtmlCode = Browser("Google Labs").Page("Google Labs").Object.documentElement.outerHtml

' save HTML code to a local file
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.CreateTextFile("C:\HtmlCode.html", True, -1)
f.Write(HtmlCode)
f.Close()

' run tidy.exe to convert HTML to XHTML
Set oShell = CreateObject("Wscript.shell")
oShell.Run "C:\tidy.exe --doctype omit -asxhtml -m -n C:\HtmlCode.html", 1, True ' waits for tidy.exe to be finished

' create MSXML parser
Set objXML = CreateObject("MSXML2.DOMDocument.3.0")
objXML.Async = False
objXML.Load("C:\HtmlCode.html")

XPath = "//a" ' XPath query means to find all links
Set Links = objXML.SelectNodes(XPath)
Msgbox "Total links: " & Links.Length

Post a Comment

 
Back To Top