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, let’s understand some basic concepts on XPath

What is XPath?

XPath is a query language used to select and navigate through nodes in 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 a consistent idattribute. A framework may generate this data dynamically. For instance, let’s 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" />
<label>Handle:</label>
<input id="_id25:_id29" name="_id25:_id29" value="" size="10" type="text" />
</form>

At first sight, name and id attributes under the input tag seems to be making the job easy. But what if these tag values are randomly generated in your application and change on every run? Those label tags won’t help you either. The size and type attributes of the input tag are subject to change and too vague.

The smart thing to do here is write an XPath expression.

XPath Syntax

XPath uses path expressions to navigate and find the XML elements

Here is a sample XML document.

XPath Syntax

Let us use XPath to navigate and reach various tags and attributes in the sample shown above. In the table below, you would see the XPath Expressions on the left column and the Results on the right column. A blue selection indicates the selected path by the XPath Expressions.

Sample XPath expressions

XPath Example

Let’s consider another html form as shown below. It’s a simple form with two input buttons with labels Name: and Handle:.

Name and Handle

The source code of the form is shown below

Form Source Code
Let’s record few actions to input some values in both the text boxes. If the id attribute values of the input boxes are generated randomly every time the page is launched, the play-back would fail since the id values at record time and run time won’t match. Hence, in such cases we can use the XPath expression for unique object identification.
Here is what you are going to do to check the object identification by using Xpath expression in UFT.

Pre-Requisites

    1. Open UFT and create a new test.
    2. Disable Smart Identification for the Button test object by selecting Tools > Object Identification. Select Web environment, and then select the WebButton test object class from the Test Object classes list.  Un-check the Enable Smart Identification checkbox.
      Disable Smart Identification in UFT
    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 in UFT
    4. Use this XPath demo on jsfiddle or 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. /html/body/form/input[1]
    9. To identify the second input box copy and paste the following syntax into the Value edit box.
    10. /html/body/form/input[2]

    Results of XPath Expression

    Select each object and select View > Highlight in Application. UFT can now identify each button based on XPath expressions you added above.