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.

XPath Syntax

Sample XPath Expressions (Click to enlarge)

XPath in QTP

Example

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

Name and Handle

The source code of the form is shown below

Form Source Code

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

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

Disable Smart Identification

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.

Disable Automatic XPath

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.

Welcome new readers: Download the Free eBook!

I am Ankur Jain, the owner of LearnQTP and founder of a software organization. This site is run, maintained and updated by me and my team. Feel free to take a look around, you may want to start with What is QTP? 

You can connect with me on twitter @ajonit. 

Please enter your first name and email address below to instantly download the eBook on Optimizing QTP Scripts. In future, we will make sure you get new tips & tricks on QTP delivered direct to your email box.

You have Successfully Subscribed!

Share This