Facebook

Course Name Start Date Time Duration Registration Link
No Training Programs Scheduled ClickHere to Contact
Please mail To sudhakar@qtpsudhakar.com to Register for any training

Sunday, January 8, 2012

Using X Path for Object Identification in QTP



X- Path Introduction

QTP uses many ways to find an object. X-Path is one of that. In any XML or HTML document there is a concept called tagging. If you want to create HTML document you need to go by tags.
For example to create a HTML document you will first create <html> <body> </body> </html> Tags.

This is the standard for HTML. Like this we have to use TAGS to create any object. Following to that automatically for every object a hierarchy will be created. This hierarchy is nothing but XPath.

The x-path for body is /html/body

Example

<html> <body> <table> </table> <table> </table> </body> </html> 

In the above code the x-path for the first table is /html/body/Table[1]

XPath (XML Path) is a language used to define the structure of elements in XML documents. You can define an XPath identification property to help identify a Web object in your application based on its location in the hierarchy of elements in the Web page. Because of the flexible nature of the language, you can define the XPath according to the unique way your Web page is structured.


<html><body>

<table id="maintab" border=1> 

<tr id=BPT> 
<td>HP Business Process Testing</td> 
<td> <input type="button" value="Buy"> </td> 
</tr> 

<tr id=QC> 
<td>HP Quality Center</td> 
<td><input type="button" value="Buy"></td> 
</tr> 

<tr id=QTP> 
<td >HP QuickTest Professional</td> 
<td> <input type="button" value="Buy"> </td> 
</tr> 

</table></body></html> 


The view of above html code is like below
To find the objects of above html we can use complete hierarchy of objects. We can even simplify the XPaths using X Path Syntax.


XPath Syntax
Expression
Description
/
Selects from the root node
//
Selects nodes in the document from the current node that match the selection no matter where they are
*
Matches any element node
@*
Matches any attribute Node
@
Selects attributes
|
Selects multiple X-Paths

Below table displays path expressions and the result of expressions based on above html document:

Path Expression
Result
/html
Selects the element html
Note: If the path starts with a slash ( / ) it always represents an absolute path to an element!
//table
selects all table elements in html document
//TR[@id='QTP']
Selects HP QuickTest Professional table row
//TR[@id='QTP']/*/INPUT
Select input tag in the QTP table row and skips elements between input and table row
//Table | //TR
Selects Table and TR tags

Giving X-Path Property to Identify Objects
To identify “Buy” button besides Quick Test Professional row we have to use below X-Path

//TR[@id="QTP"]//INPUT[@value="Buy"]

In this //TR[@id="QTP"] selects Quick Test Professional Row from all table rows  available in html, //INPUT[@value="Buy"] selects “Buy” button of all buttons available in Quick Test Professional Row.

Follow below steps to know how to use XPATH in QTP
  1. Save above html code with a html file
  2. Open the saved html file
  3. Open QTP
  4. Disable smart identification for WebButton class in Tools-> Object Identification window
  5. Read any of the “Buy” button to Object Repository
  6. Remove Ordinal Identifier Index for the added button in Object Repository
  7. Click on Property Add (+) in the Object Description Place
  8. Select XPath property
  9. Click on OK
  10. Enter //TR[@id="QTP"]//INPUT[@value="Buy"] at value
  11. Highlight the object in Application

9 comments :

  1. Nice, lucid article on XPATH and its usage in QTP

    ReplyDelete
  2. Can we use the same concept when we have DIV tags as well on the webpage?
    Will the XPATH still remain so simple, as it was in the case of Web Tables?

    ReplyDelete
  3. Yes. You can use it for DIV tags

    ReplyDelete
  4. can you show one example with DIV and using any attribute like style

    ReplyDelete
  5. Hi Sudhakar,

    PLease can share Log Tracking concept in QTP11 with example.

    ReplyDelete
  6. Hi,

    i have query who can identify the xpath of object on a web page?

    ReplyDelete
  7. Can anybody help me, having problem in scripting with qtp - as the application is identified as a page but does not see any objects in there like editbox, buttons dropdown box etc...

    ReplyDelete
  8. In your example ,we have 3 Buy button ,so how we can identify these object individually.write now its identifying only third object with given xpath

    ReplyDelete