Tricentis Tosca 16.0 Released on Feb-2023 ----- UFT has been upgraded from UFT 15.0.1 to UFT One 15.0.2, Beginning at November 2020.

Saturday, 9 June 2018

DOM Objects in UFT/QTP

The Document Object Model (DOM) is an application progamming interface (API) developed by the World Wide Web Consortium (W3C) to create and modify HTML pages and XML documents. The document object model can be used with any programming or scripting languages.


HTML DOM – A Standard object model for HTML documents

XML DOM – A Standard object model for XML documents
DOM is especially useful for indenidentifying:

Event Handlers
  • Parent Nodes
  • Previous and Next Elements
  • Ready State
  • Source Index
When we cannot use DOM?
  • Not for Browser Compatibility Testing
  • Not for FireEvents
  • Not for Edit Box, Combo Box Validation

HTML DOM Objects

A HTML document is made up of frames, Tables, Links, Buttons and Input Data fields.. etc. But  when it comes to DOM, every object is a node in a HTML document. The relations between the nodes are parent, children, previous sibling and next sibling.
A UFT for every web object there is a  property called “object”. Using this property the internal methods and properties can be accessed for any web object.
Syntax: WebObjectClass(“PropertyName:=PropertyValue”).Object
Creating document object for IE (Without UFT)
Set IE = CreateObject(“internetexplorer.application”)
IE.Visible = True
IE.Navigate = “http://google.co.in”
Set PageObject = IE.Document
Creating document object (With UFT)
Set docObj = Browser(“browser”).Page(“page”).Object.Document
Properties
  • activeElement Property – Retrieves the object that has the focus when the parent document has focus.
  • cookie Property – Sets or retrieves the string value of a cookie.
  • documentElement Property – Retrieves a reference to the root node of the document.
  • readyState Property – Retrieves a value that indicates the current state of the object.
  • URL Property – Sets or retrieves the URL for the current document.
  • URLUnencoded Property – Retrieves the URL for the document, stripped of any character encoding.
Collections
  • all – Returns a reference to the collection of elements contained by the object.
  • frames – Retrieves a collection of all window objects defined by the given document or defined by the document associated with the given window.
  • images – Retrieves a collection, in source order, of img objects in the document.
  • links – Retrieves a collection of all objects that specify the HREF property and all area objects in the document.
Methods
  • getElementById Method – Returns a reference to the first object with the specified value of the ID attribute.
  • getElementsByName Method – Retrieves a collection of objects based on the value of the NAME attribute.
  • getElementsByTagName Method – Retrieves a collection of objects based on the specified element name.

How to Use HTML DOM in UFT/QTP ..?

If we are testing a web application then we will know the limitations of UFT/QTP as far as identification of objects is concerned.

Many web objects have same properties. So QTP tries to identify the objects using Index - Ordinal Identifier.

The disadvantage of using index is that It makes the script weak. What I mean here is that If developer adds new objects of similar types, Index of the existing objects also change. So QTP fails to identify the object due to changed index.

So How we can use HTML DOM to handle such scenarios.

Using html DOM we extract all kind of information from page as we can access source code of the page in the form of HTML.

HTML DOM has many method and properties associated with it like getElementById, nextsibling etc.

To access these methods you must create the DOM object first.

Syntax to create a DOM object is - 

Set domObject = Browser("google").Page("google").object

Above statement will create the DOM object for the page called google.

Now we can access the methods of the DOM object

Now let us use the dom object we have created above to get more information of the page currently displayed in the application.

Print domObject.getElementById("myid").innerText         
' Prints the text inside the Element - myid

Print domObject.getElementById("myid").innerHTML     
' Prints the inner HTML of Element - myid

How to access the parent object of given DOM Object

We can also find the parentobject, nextsibling, previoussibling of the given dom object using built in properties as mentioned below.

Set parentObj = domObject.getElementById("myid").parentNode  ' gets the parent object of myid

Set nextsiblingObj = domObject.getElementById("myid").nextsibling 'gets the next sibling object of myid

Set previoussiblingObj = domObject.getElementById("myid").previoussibling


Ok ..Now we are able to create DOM object...What Next?

Once we have created the DOM Object, we can access the text associated with that object. After getting the text you can process that as per your testing requirement.

Print domObject.getElementById("myid").innerText         ' Prints the text inside the Element - myid
Print domObject.getElementById("myid").innerHTML     ' Prints the inner HTML of Element - myid

Some of the example of html dom in QTP are given below.

Accessing HTML element using DOM..?
Dim docObj, objIndex
Set docObj = Browser(“Sample Document”).Page((“Sample Document”).Object.getElementsByTagName(“a”)
msgbox domObj.length
for objIndex=0 to docObj .length-1
         msgbox domObj(objIndex).href

Next
How to get the collection of all TD (table data cells) elements of the table using HTML DOM in QTP/UFT?

Set tdtags =Browser(“abc”).Page(“mypage”).WebTable("mytable").Object.getElementsByTagName("TD")

How to get the collection of all TH (table header cells) elements of the table using HTML DOM in QTP/UFT?

set thtags = Browser(“abc”).Page(“mypage”).WebTable("mytable").Object.getElementsByTagName("TH")

How to get the value of attribute of any element using HTML DOM in QTP?

Set myobj = Browser(“abc”).Page(“mypage”).WebTable("mytable").Object
Print myobj.getAttribute("class") 'This will print the value of class attribute of myobj - table 

How to get the collection of all table rows using HTML DOM in QTP?

Set tableObj =Browser(“abc”).Page(“mypage”).WebTable("mytable").Object
Set trtags = tableObj.getElementsByTagName("tr")

How to find the total number of rows in table Using HTML DOM in QTP?
print trtags.length

How to get the value inside table cell Using HTML DOM in QTP?

Set tdtags =Browser(“abc”).Page(“mypage”).WebTable("mytable").Object.getElementsByTagName("TD")
Print tdtags(0).innerText 'print the text displayed inside first td tag in table


No comments:

Post a Comment

Note: only a member of this blog may post a comment.