XPath based object identification in QTP 11

Updated On Aug 7, 2014 by

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!

Ankur JainI 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 my Google+ page. 

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.



Share This:

10 Comments

  1. Srinivasan

    I have one doubt in Xpath.

    Chrome browser i need to identify the table object but no value is there. I tried Visual relation identifier also but due to dynamic it not working.

    For xpath also there is no value in Description Properties hence i used developer tool in chrome and copied the value and pasted in description properties value.

    In my xpath value *[@id = pgtemp:region_id_7:0:pgtemp:appcout]/table.

    in above value is table xpath value. only that numeric value “7″ is changed as 8 or 9 next time.
    how to use contains function. Please help me to overcome this problem.

    Note: there is no property for table object only i have html tag property as table.

  2. Vaibhav

    Very Nice Article Ravi, Reading this article clears my concept of object identification using XPath.

  3. murali

    filepath=”C:\CA-Nevada-Tax-APN-Freeform.xml”
    xpathlist=”//tax_param//freeform|//tax_param//tax_qualifier”
    newvaluelist=”murali|uppi”
    Function updatexmlnodevalue(xmlpath,xpath,newvalue)
    ‘crete objpect for XML
    Set xmlDoc = CreateObject(“Microsoft.XMLDOM”)
    Set fso=createobject(“scripting.filesystemobject”)
    If fso.FileExists(filepath)true Then
    ‘reporter.ReportEvent micFail,”specified file does not exist” ,” ”
    Exit Function
    else
    ‘load xmlfile
    xmlDoc.load(xmlpath)
    ‘Locate the desired node

    xpatharray=split(xpathlist,”|”)
    newvaluearray=split(newvaluelist,”|”)

    For i=0 to ubound(xpatharray)
    xpath=xpatharray(i)
    Set nNode = xmlDoc.selectsinglenode (xpath)
    If nNode is Nothing Then
    ‘reporter.ReportEvent micFail,”specified xpath does not exist in xml file” ,” specify correct xpath ”
    Exit Function
    End If
    ‘Set the required value
    nNode.text = newvaluearray(i)
    ‘reporter.ReportEvent micPass,”value of the Node is changed” ,” New value is set ”
    ‘Save the xml document with the new settings.
    Next
    xmldoc.save(“C:\test\CA-Nevada-Tax-APN-Freeform.xml”)
    end if
    End Function

    Call updatexmlnodevalue(filepath,xpathlist,newvaluelist)

  4. 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. :)

  5. Praveen

    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

    • Ravi

      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.

  6. 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

  7. suni

    I Ravi, Good to See your Artical On Net. Keep it up one day you will achive your Goal.

  8. 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?

    • Ravi

      @Mike pietsch
      Yes,otherwise the script execution time increases as and when smart identification or automatic xpath identification is invoked.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>