4 ways to get & count objects in QTP

4 ways to get & count objects in QTP

Imagine simple and practical QTP tasks:
  • How to count all links on Web page?
  • How to get them and click each link?
  • How to get all WebEdits and check their values?

I'm going to show 4 approaches how to get lists of UI controls and process them (for example get their count).
As an example, I will work with links on Google Labs page. My goal is to get the list of links and count them.

I've added Google Labs page to my Object Repository and now it looks like:
I use Object Repository (OR) to simplify my demo-scripts.
Since the browser & the page were added to OR, we can use them later like:
Browser("Google Labs").Page("Google Labs").

Now we are ready to start!

  1. QTP Descriptive Programming (QTP DP) and ChildObjects QTP function
    The approach uses Description object, which contains a 'mask'
    for objects we would like to get.
    QTP script is:
    Set oDesc = Description.Create()
    oDesc("micclass").Value = "Link"
    Set
    Links = Browser("Google Labs").Page("Google Labs").ChildObjects(oDesc)
    Msgbox "Total links: " & Links.Count
    The result of this QTP script is:
    ChildObjects returns the collection of child objects matched the description ("micclass" is "Link") and contained within the object (Page("Google Labs")).


  2. Object QTP property and objects collections
    QTP can work with DOM:
    Set Links = Browser("Google Labs").Page("Google Labs").Object.Links
    Msgbox "Total links: " & Links.Length
    I use Object property of Page object. It represents the HTML document in a given browser window.
    This document contains different collections - forms, frames, images, links, etc.
    And we use Length property to get the number of items in a collection.

    The result is the same as for the previous QTP script:


  3. Object QTP property and GetElementsByTagName method
    Again, we can get access to
    the HTML document and use its GetElementsByTagName method.
    As the name says,
    GetElementsByTagName method returns a collection of objects with the specified tag.
    Since we are going to get all link, we should use "a" tag.

    QTP script is:

    Set Links = Browser("Google Labs").Page("Google Labs").Object.GetElementsByTagName("a")
    Msgbox "Total links: " & Links.Length
    The result is the following:

    Note: There is another way how to select objects by tag name:
    Set Links = Browser("Google Labs").Page("Google Labs").Object.all.tags("a")
    Msgbox "Total links: " & Links.Length
    The result will be the same. 69 link will be found.


  4. XPath queries in QTP
    The idea of this approach is to use XPath queries on a source code of Web page.
    For example, "//a" XPath query returns all "a" nodes (= links) from XML file.

    There is one problem. Web page contains HTML code, which looks like XML code but actually it is not.
    For example:
    • HTML code can contain unclosed img or br tags, XML code cannot.
    • HTML code is a case-insensitive markup language, XML is a case-sensitive markup language, etc
      More details here.

    So, we have to convert HTML source code into XML. The converted code is named as XHTML.

    You can convert HTML documents into XHTML using an Open Source HTML Tidy utility.
    You can find more info about how to convert HTML code into XHTML code here.

    I will use the final QTP script from this page, a bit modified:

    ' 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
    Note: you can download tidy.exe here for above QTP script.

    This QTP script leads to the same results - 69 links found:
    (Click the image to enlarge it)


  5. Bonus approah
    Why don't you count all Wen page objects manually? :) Open a source code of the page and start counting :)
    Just joking :)

Summary:
  • I shown 4 practical approaches how to count Web page links.
    Similarly you can process images, webedits, etc
  • Each approach gets a list of objects.
  • First approach (QTP DP + ChildObjects) is the most easy
  • Second & third approaches (Object + collections; Object + GetElementsByTagName) will work on Internet Explorer, because they use DOM methods
  • Fours approach is biggest but it is more powerful. It allows to use complex XPath queries.


Related articles:


Do you like this QTP tutorial? Would you like to receive them in the future?
If yes, please subscribe to this blog RSS feed or by Email. (How to subscribe? VIDEO guide)




Do you know that you are free to use/copy/publish all my materials on your site/blog?


10 comments:

Anonymous said...

You Xpath way is way too complicated.. Why we would like to use this method when we already have such an easy method of ChidObjects with DP.

I am disappointed....

Dmitry Motevich said...

@Anonymous (December 16, 2008),
There are 4 ways how to do the same task.
You can use any.
I provided XPath way just for your information. It works too.

Anonymous said...

Thank You Dmitry. Quite a good descriptions.

Unknown said...

Thank you very much Dmitry, it saved my job.Really its very useful for me. you are so great.Thanks once again

May 5th

Unknown said...

Thank you very much. its really interesting and was very helpful to me.

hagait1973 said...

Thank You Dmitry, your articles are very useful.

Eash Chand said...

i am a starter in QTP. will you plz tell me how to add an object in object repository

Eash Chand said...

how to add an object in the repository

Eash Chand said...

plz tell me how to add an object into object repository

Anonymous said...

Dmitry, great job. This was really helpful.