RSS Feed

XPath based object identification in QTP 11

Posted on

Through this article, I would like to show you how to use XPath for identifying an object  especially when there are multiple objects of same type and  properties or if the object properties change dynamically.

Before we go ahead and use XPath in QTP lets understand few basic concepts on XPath

What is XPath?

XPath is a language used to get the information from an xml document.

XPath uses path expressions to navigate and identify the element in the xml document

The advantage of using XPath is to identify any object in the application easily without wasting much time looking for combination of properties to make it unique. If you are working on a commercial application, you would see code like this.

  1. <form>
  2. <label for=“male”>Male</label>
  3. <input type=“radio” name=“sex” id=“male”/><br/>
  4. <label for=“female”>Female</label>
  5. <input type=“radio” name=“sex” id=“female”/>
  6. </form>

The use of these tags makes them easy to manipulate in your QTP test scripts

Frequently, however, this won’t be the case that you always have the luxury of having an id tag. Maybe the developers are too lazy or their framework is generating this data dynamically.For instance, lets consider a case, where you are trying to automate the submission of a form for which the source code looks like –

  1. <form>
  2. <label>Name:</label>
  3. <input id=“_id25:_id22” name=“_id25:_id22” value=“” size=“15” type=“text” />
  4. <labe>Handle:</label>
  5. <input id=“_id25:_id29” name=“_id25:_id29” value=“” size=“10” type=“text” />
  6. </form>

At first sight, name and id tag seems to be making the job easy, but what if these are  obviously randomly generated values in your tests .Those label tags won’t help you either and the size and type attributes of the input tag are subject to change and too vague (respectively).
The smart thing to do here is write an XPath expression.

XPath Syntax

XPath uses path expressions to navigate and find the xml elements

We will use the below sample xml document to understand XPath expressions.


Sample XPath Expressions (Click to enlarge)



Lets consider the below html form. It’s a simple form with two input buttons with labels “Name:” and “Handle:” respectively.


The source code of the form is shown below


Let’s record the steps to input some values in both the text boxes. Now if the id values of the input boxes are generated randomly, every time the page is launched the play back fails as the id values at record time and run time doesn’t match. Hence, in such cases we can use the XPath expression for the unique object identification.

Here is what you are going to do to check the object identification by using Xpath expression in QTP11.0


1. Open Quick Test and create a new test.

2. Disable smart identification for the Button test object by selecting Tools > Object Identification, selecting the Web environment in the Object Identification dialog box, and then selecting the Button test object class from the Test Object classes list.  Uncheck the “Enable Smart Identification” checkbox


3. Disable automatic XPath by selecting Tools > Options > Web > Advanced, and then making sure that the Learn and run using automatic XPath identifiers checkbox is not selected.


4.Create a sample Web Application the source code for which is shown above.

5. Now add the  two input boxes to the object repository and delete the ordinal identifiers and id properties if added.

6.  In the Object Description section, click the Add button, and add the XPath property to the test object description.

7.To identify the first input box copy and paste the following syntax into the Value edit box:


8. To identify the second input box copy and paste the following syntax into the Value edit box:



Select each object and select View > Highlight in Application. QuickTest can now identify each button based on the Web object identifiers you added.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: