An article by Ravi Kumar Gajul
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.
<form> <label for="male">Male</label> <input type="radio" name="sex" id="male"/><br/> <label for="female">Female</label> <input type="radio" name="sex" id="female"/> </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 -
<form> <label>Name:</label> <input id="_id25:_id22" name="_id25:_id22" value="" size="15" type="text" /> <labe>Handle:</label> <input id="_id25:_id29" name="_id25:_id29" value="" size="10" type="text" /> </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)

Example
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
Here is what you are going to do to check the object identification by using Xpath expression in QTP11.0
Pre-Requisites
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:
/html/body/form/input[1]
8. To identify the second input box copy and paste the following syntax into the Value edit box:
/html/body/form/input[2]
Results
Select each object and select View > Highlight in Application. QuickTest can now identify each button based on the Web object identifiers you added.
Ravi Gajul
Latest posts by Ravi Gajul (see all)
- XPath based object identification in QTP 11 - August 21, 2012



7 comments ↓
Won’t steps 2 and 3 of the pre-reqs need to be repeated on any QTP machine that we would want to run the script on?
I Ravi, Good to See your Artical On Net. Keep it up one day you will achive your Goal.
Thanks Ravi for sharing your knowledge and explaining such complicated concepts in a very simple and a clear way. It helped me alot and can u let me know if you have any books written by you on this so that i can gain more of it. Please
Thanks Ravi,
It was very informative. It looks like i might need to re-identify the objects(Changes to OR) if there were to be any changes in the screen( new fields added which will affect the sequence of the current fields).
Kind regards,
Praveen
@Mike pietsch
Yes,otherwise the script execution time increases as and when smart identification or automatic xpath identification is invoked.
Thanks Praveen, I’m glad you found it informative.As the DOM structure changes, the XPath will also change, which calls out for a change in OR.
Excellent intro to XPath. It can be a hard concept to grasp but we have found it invaluable. If you aren’t aware, we have websites in multiple countries and multiple languages so it was a nightmare testing each site in each browser. Xpath (and to some extent Java) came to our rescue. Now we have one set of components and populate object properties via XPath depending on the site/browser being tested. For our new team members, it was easier to grasp when we told them to think of the object in the OR as an empty box with a name. We populate the object by pulling up the XML that matches the site/language/page we are testing and then using XPath, pull the object’s properties from the XML to fill the box so QTP can recognize the object on the page. When an object’s properties change (and it will), we just change the XML and don’t even touch the script.
The power of XML and XPath.
Leave a Comment