.

Document Object Model(DOM) and QTP – A complete guide.

What is Document object Model?

Wikipedia defines Document Object Model(DOM) as:

A platform- and language-independent standard object model for representing HTML or XML and related formats.

For QTP’s sake, I would redefine it to make it simpler. DOM is a method for QTP engineers to access the source (IE –> View –> Source) of any webpage direct through VB Scripting.

When can we use DOM?

One of the very important places you can use it is when a QTP web table checkpoint doesn’t show you the desired content in a cell. I mean the content in the cell is in a format that is not supported by the web table checkpoint. Another use can be when you want to access all the HTML tags used in a webpage. You can get the names, the innertext, innerHTML property of all these tags. The possibilities are endless.

How can we use DOM to access the source page?

We can access the source page of any webpage using .object notation.

Any practical example of using DOM?

I have created a demo web page to show you document object model in action. Say you want to find the font color used for writing Happy Holidays Everyone in cells of the webtable given on this page. The algorithm would be:

  1. Access the DOM of webtable using .object notation.
  2. Access the tagname corresponding to the property you wish to find out. In our case tagname is “font” and property to be explored is “color”. Document Object Model QTP
  3. Find the count of total number of tags and loop it find out the font-color.

The corresponding VB script would be:

Using DOM to find the font color

Now it’s quiz time. On the same page find out the src of all the images used in the table. Let me know your script through the comments below.

[Also note that src won’t show up in the webtable checkpoint (for obvious reasons!)]

So, what does src say to you?

I wish you Happy Holidays 2009! :)

Have you downloaded the FREE Optimizing QTP eBook yet? Get It Now!

If you want to keep track of further articles on QTP. I recommend you to subscribe via RSS feed. You can also subscribe by Email and have new QTP articles sent directly to your inbox.

30 comments ↓

#1 sameer on 12.26.08 at 23:06

Hey it was very simple…. But thanks for your inputs on the DOM since we use the webtables to a very great extend here also with the lot of combination. thoguh I have not tried implementing it on my app I am sure this one will make our life easy.

Set Font = Browser(“Browser”).Page(“Page”).WebTable(“Happy”).Object.all.tags(“img”)
msgbox Font.length
For i =0 to Font.length -1
msgbox Font(i).src
Next

#2 Anilkumar on 12.27.08 at 05:55

good.

#3 maddy on 12.27.08 at 08:32

nice work ankur

#4 Sankar on 12.27.08 at 10:08

here is the script to find the src of the images in the given web table “Happy Holidays Everyone!”

Set img = Browser(“Browser”).Page(“Page”).WebTable(“Happy”).Object.all.tags(“img”)

msgbox img.length

For i=0 to img.length-1

msgbox img(i).src

Next

#5 kiran on 12.29.08 at 01:37

Good to know about DOM use in QTP.

#6 Yogindernath on 01.09.09 at 02:46

The concepts explained for DOM are quite interesting.

#7 Ajay on 01.13.09 at 02:25

Hi Ankur,
Your articles are so helpful.
Could you explain about What DOM is all about. And also the code that u gave for comparison of 2 Excel sheets cell by cell, in that u used code like ” For each cell in WorkbookSheet1.usedrange ”
How to use this code and same sort found in finding the names of subfolder. Could explain about this plzz…

Thanks in Advance.
Regards,
AjayKumar

#8 Ankur on 01.13.09 at 02:49

@Ajay: For questions not related to post, please use QTP forums.

#9 Ravee on 02.03.09 at 03:21

Super sir,keep going :-)

#10 Rohit on 03.09.09 at 01:30

Set obj_link = Browser(“ClientLink Cash Entry:”).Page(“ClientLink Cash Entry:”).WebTable(“State”).Object.cols
msgbox obj_link.count

help me this code is not working !!!

#11 Baba on 03.12.09 at 22:46

‘ To get the Source of images for this webpage “http://toankurjain.googlepages.com/DOM.html”

‘Regular method

Dim img

Set img=description.Create
img(“micclass”).value=”Image”

Set brimg=browser(“Browser”).Page(“Page”).ChildObjects(img)
msgbox brimg.count

For i=0 to brimg.count-1

print brimg(i).getroproperty(“src”)

Next

‘ DOM Method

Dim obj

Set obj=browser(“Browser”).Page(“Page”).WebTable(“Happy”).Object.all.tags(“img”)

msgbox obj.count

For i=0 to obj.count-1

sourc=obj(i).src
print sourc

Next

#12 TRMR on 03.16.09 at 20:53

How to get font type weather it is bold or italic etc?

#13 nandu on 04.02.09 at 16:04

Thank you so much..
DOM concept is quite interesting

#14 Pagkis on 04.18.09 at 23:20

Ankur,

Thanks for sharing this info abt DOM. I was doing something wrong with src. It is clear now.

Thanks,
~P

#15 Tatyna on 06.04.09 at 09:22

How would you use the same approach if you need to find all images with a particular name? img_expand_node.gif for example?

#16 Mahalakshmi on 08.11.09 at 04:59

set img_no=Browser(“Browser”).Page(“Page”).WebTable(“Happy”).object.all.tags(“img”)
for i=0 to img_no-1
print img_no(i).src
next i

#17 puli jay on 08.11.09 at 09:18

please explain in detail

#18 Kay on 09.27.09 at 08:49

Hi,

Is it possible to use DOM to get any string within the view source of a web page? I’m trying to target just specific strings and put them into an excel sheet.

Any help appreciated,
K

#19 Franklyn on 10.13.09 at 19:37

Hi

i have one query ,

in my assignment i have the following code

here the tag name is ‘INPUT’

i want to retrieve the attribute ‘checked type’ s value ..
in qtp if place the statement msgbox font(i).checked type

QTP throws syntax error, since ‘checked type’ has a space between ‘checked’ & ‘type’ .

Can any one help me how to retrieve the ‘checked type’ value?

#20 Himanshu on 04.23.10 at 10:57

Can neone helpme out by telling the link of the website containing all the methode nd properties that can be used / applied in the DOM method.

#21 Neelakantan on 04.23.10 at 16:47

Perfect!. Very simple but more effective

#22 Kenneth on 05.31.10 at 17:04

Hi when i use the below code it crashes QTP…can you tell me why?

The goal of this code is to capture the name of all the links on the page…

Set a = Browser(“title:=.*”).Page(“title:=.*”).Object.Links

For i = 0 to a.Length -1
print a(i).name
Next

#23 NatTurn3r on 06.09.10 at 04:17

@Kenneth

Try this instead…

For i = 0 to a.Length -1
myString = a(i).name
print myString
Next

#24 KD on 06.10.10 at 23:29

Hi Genius minds,
I have a small question. How I can check format of specific cell in webtable.

For Example, I have a table with 3 row and 3 column.
I want to check what is the cell (2,3) format for 2nd row and 3rd column. I want to verify if its plain text or link.

I hope I will get the answer. :)

Thanks,
KD

#25 Kenneth on 06.16.10 at 18:29

Thanks that worked!! :)

one more thing…

Browser(“hwnd:=” & hwnd).object.document.getElementsByTagName(“meta”).length

Why do i get General run error for this? and how do i fix it.

#26 Kenneth on 06.16.10 at 18:41

The actual other code is..like so…
Browser(“hwnd:=” & hwnd).object.document.getElementsByTagName(“meta”)(i).content

i am trying to capture the contents in in the tag…and not its properties…

was trying another method using getElementsByTagName…

Please adivce…

#27 SanMan on 07.29.10 at 17:11

If i have a tag like tagval how could I retrieve the “tagval” ??

Set objname = Browser(“..”).Page(“…”).Frame(“…”).Object.all.tags(“sometag”)

For i =0 to objname.Length -1
MsgBox objName(i).
Next

Thanks in advance…

#28 SanMan on 07.29.10 at 17:41

@SanMan
Found it…”innertext” does the trick

MsgBox objName(i).innertext

#29 Litan on 08.01.10 at 13:46

Ankur,

Continue your nice work.

#30 venu on 08.02.10 at 17:00

Hi Ankur,
your efferts are fabulous.

could you please suggest me how to handle this,
i have a webtable,the cells in which some times identifying as webelement and some times as webedit , so i am not able to handle the situation, could you please send me any suggestions that helps me.

Leave a Comment