Академический Документы
Профессиональный Документы
Культура Документы
TestO'Matic
Posted: November 16, 2011 | Author: upgundecha | Filed under: QTP, Test Automation, Testing | Tags:
CSS, DOM, HTML DOM, QTP, Web Testing, XPath |6 Comments
QTP 11 introduced new features to identify Web objects using HTML DOM, XPath & CSS. These object
identification strategies are widely used in open source tools like Selenium, WATIR etc.
In this series, I will explain how to use HTML DOM, XPath and CSS for identifying objects in your Web
application. In Part 1, I will use HTML DOM for identifying the Web objects.
The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows
programs and scripts to dynamically access and update the content, structure, and style of a HTML
document.
The HTML DOM defines the objects and properties of all HTML elements, and the methods (interface)
to access them. JavaScript uses DOM to add interactivity to HTML pages.
We can use the combination of JavaScript and HTML DOM interfaces to identify objects within Web
Pages and Frames. This strategy can be useful for testing AJAX applications when object are dynamically
added to the Web Page or Frame. While QTP’s Object Repository & AJAX testing features should suffice
your need, this strategy can be used as a workaround for identifying the objects.
Following HTML DOM document object methods (interfaces) can be used in QTP for identifying objects:
https://unmesh.me/2011/11/16/using-dom-xpath-css-for-object-identification-in-qtp-part-1/ 1/5
5/8/2018 Using DOM, XPath & CSS for Object Identification in QTP – Part 1 | TestO'Matic
Method Description
getElementById Returns the first element with the
specified id
getElementsByName Returns all elements with a specified
name in an JavaScript array
getElementsByTagName Returns all elements with a specified
tagname in a JavaScript array
Calling JavaScript Code in QTP
QTP 11 introduced RunScript method to the Page and Frame Test Objects in Web Add-In. We can use
this method to inject and execute JavaScript code in Page or Frame Test Objects. This method has
number of uses for example testing JavaScript code within your page, invoking JavaScript events etc.
This feature comes handy while testing complex AJAX Web applications.
Following example will display a JavaScript popup with Hello There!! message. This example uses
Descriptive Programming to identify Browser and Page objects:
1 Browser("title:=Google").Page("title:=Google").RunScript("alert('Hello There!!'
To access the HTML DOM methods we need to use the document object. We can use getElementById
method to access the element using its HTML ID. This method will return the first element it finds in
HTML DOM with specified ID. For example on Gmail Login Page, we can access the User Name text box
by following way.
This is most recommended method to identify the objects using HTML DOM. However this method
might not work for elements where HTML IDs are not defined. We can use getElementsByName
method to identify the elements by specifying the name a ribute value. In following example we will
use same Gmail Login User Name Text Box, however this time we will identify this element using the
name a ribute value:
The getElementsByName returns a JavaScript array for all the elements whose name matches with the
specified value. In above example we use array index to access the 0th element, which is the User Name
Text Box.
https://unmesh.me/2011/11/16/using-dom-xpath-css-for-object-identification-in-qtp-part-1/ 2/5
5/8/2018 Using DOM, XPath & CSS for Object Identification in QTP – Part 1 | TestO'Matic
However there is another method which comes handy when you don’t have either IDs or Names
specified for HTML elements. Using getElementsByTagName you can identify the object by Tag name.
This is least preferred way to access the elements.
The getElementsByTagName returns a JavaScript array for all the elements whose HTML Tag name
matches with the specified value. In above example we use array index to access the 12th element which
is the User Name Text Box. This method needs efforts to find out exact index of the intended element. If
the index changes, your script will fail.
We can also use combination of HTML DOM methods to identify the objects. In following example a
Text Box is embedded in DIV element, however this Text Box does not have enough identification
properties. We will use getElementById to identify the parent DIV element and getElementsByTabName
to identify the Text Box:
HTML Syntax:
1 </pre>
2 <div id="<span class="hiddenSpellError" pre="">mainArea</sp
3 <pre>
4 <p>Enter Amount: <input type=text></input></p>
5 </div>
We can also identify elements using the relationships. Following table explains using various
relationship identifiers:
https://unmesh.me/2011/11/16/using-dom-xpath-css-for-object-identification-in-qtp-part-1/ 3/5
5/8/2018 Using DOM, XPath & CSS for Object Identification in QTP – Part 1 | TestO'Matic
Reply
2. Shilpa says:
May 15, 2014 at 6:05 am
Concepts are explained with examples is very useful, easy to understand
Reply
3. Vishal Aggarwal says:
November 25, 2014 at 2:28 pm
Thanks for sharing such useful and unique content in QTP/UFT.
Vishal Aggarwal
h p://testocean.blogspot.in
Reply
4. rajesh says:
April 18, 2015 at 9:12 am
when i am running same script i am ge ing error “Object Required”,can u plz provide me solution
Reply
5. vasegowda says:
July 9, 2015 at 1:27 pm
Excellent work, lots of use
Reply
6. ShibaShankar says:
July 29, 2015 at 1:05 pm
Very useful in handling unidentified objects
Reply
https://unmesh.me/2011/11/16/using-dom-xpath-css-for-object-identification-in-qtp-part-1/ 4/5
5/8/2018 Using DOM, XPath & CSS for Object Identification in QTP – Part 1 | TestO'Matic
https://unmesh.me/2011/11/16/using-dom-xpath-css-for-object-identification-in-qtp-part-1/ 5/5