Accessing web elements through DOM

Although UFT provides a rich interface that encapsulates the most common operations required to manipulate web elements (for example, click, double-click, set, select, and so on), it has some limitations. For instance, UFT does not give us an obvious way to verify the style of the text. In such a case, we will refer to the Document Object Model (DOM) to get access to the native methods and properties of the elements. Another situation that would justify such usage would be when the performance of the test run is hindered when a huge amount of elements needs to be processed. For example, processing a table with lots of rows and columns through UFT's WebTable interface takes much longer than accessing these same elements through DOM. In this recipe, we will see how to get the style of an element and how to get a collection of elements with a particular HTML tag.

How to do it...

Proceed with the following steps:

  1. First, we will get a reference to the document object:
    Dim document
    
    Set document = Browser("title:=.*Advanced QTP.*").Page("title:=.+").Object

    The InternetExplorer.Application COM object is an alternative to the previous code to get the document object:

    Dim IE
    
    Set IE = CreateObject("InternetExplorer.Application")
    
    IE.Navigate "http://www.advancedqtp.com"
    
    Set document = IE.Document
  2. Let us see how we can get the style of an element.

    The style of an element is actually an object that contains different attributes. First, we will get a reference to a specific object:

    Dim searchBox
    
    Set searchBox = document.getElementByName("s")

    We will then be able to access the style attributes:

    Dim searchBox
    
    Print searchBox.style.backgroundColor
    Print searchBox.style.fontSize
    Print searchBox.style.fontFamily
  3. Next, let's see how we can get a collection of elements with a particular HTML tag:

    Here we will see how to get a collection of elements through DOM instead of using UFT's ChildObjects method:

    Dim allImages
    
    Set allImages = document.getElementsByTagName("img")

    It is then possible to loop through the individual elements and perform different verifications and operations. Keep in mind that these are native HTML objects and not UFT TOs.

How it works...

When we work through DOM, it is like stepping out of UFT. When we get a reference to the document, all native methods and properties are available to us. In general, working through DOM should be faster than working through the UFT interface. However, utilities provided by UFT, such as the Object Repository, serve as a database to store the descriptions of the objects that appear in AUT.

See also

Refer QTP Descriptive Programming Unplugged, KnowledgeInbox, by Anshoo Arora and Tarun Lalwani

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset