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"
    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 - formsframesimageslinks, 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

    1. 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)

      ' 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 

      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 :)


    • 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 + collectionsObject + 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.

    No comments: