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 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
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.
If you want to keep track of further articles on UFT (QTP). I recommend you to subscribe by Email and have new UFT articles sent directly to your inbox.