UFT/QTP – HTML DOM

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.

A Sample HTML Document Code

<HTML>

<title>Sample Document</title>

<body>

<h2>A Sample HTML Page</h2>

<a href=”/”>Automationtutorial.com</a>

<a href=”http://www.hp.com/”>HP</a>

</body>

</HTML>

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

 

Leave a Reply Cancel reply