Wednesday, 19 June 2013

How to Use HTML DOM in QTP?

Well - If you are testing a web application then you will know the limitations of 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 you can use HTML DOM to handle such scenarios.


Using html DOM you extract all kind of information from page as you 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("myb").Page("myp").object

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

Now You 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


You 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 you are able to create DOM object...What Next?


Once you have created the DOM Object, you 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

In above examples, I have used only getElementById method. But there are lot of other methods that you can use. For complete list of methods you can visit http://www.w3schools.com/jsref/dom_obj_node.asp

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

How to get the collection of all TD (table data cells) elements of the table using HTML DOM in QTP?
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?
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

********************************************************************************
You can also find below links related to HTML DOM very useful.
  1. previousSibling in QTP
  2. parentNode and childNodes in QTP
  3. getElementsByTagName in QTP
  4. getElementsById in QTP
  5. Get innerText of the web Element in QTP
  6. Find total number of rows in table
  7. Trigger web events in QTP.
  8. Find all web list objects in QTP.
You can visit QTP-UFT Master Page.
If you have any issues in object identification of web objects like Webedit, WebElement etc you can drop your comments below. I will respond to them quickly.


Best QTP Books

Everything About QTP

Hello Friends,
You can find QTP study material, Multiple choice questions (mcq), QTP question bank, QTP question papers, QTP notes, QTP questionnaire, scenario based QTP interview questions, QTP tutorial and QTP training on this site.

If you are a fresher or experienced QTP professional with (1/2/3/4) years of experience, this blog is just for you.