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
- Save above html code with a html file
- Open the saved html file
- Open QTP
- Disable smart identification for WebButton class in Tools-> Object Identification window
- Read any of the “Buy” button to Object Repository
- Remove Ordinal Identifier Index for the added button in Object Repository
- Click on Property Add (+) in the Object Description Place
- Select XPath property
- Click on OK
- Enter //TR[@id="QTP"]//INPUT[@value="Buy"] at value
- Highlight the object in Application
Nice, lucid article on XPATH and its usage in QTP
ReplyDeleteCan we use the same concept when we have DIV tags as well on the webpage?
ReplyDeleteWill the XPATH still remain so simple, as it was in the case of Web Tables?
Yes. You can use it for DIV tags
ReplyDeletecan you show one example with DIV and using any attribute like style
ReplyDeleteHi Sudhakar,
ReplyDeletePLease can share Log Tracking concept in QTP11 with example.
Good concept
ReplyDeleteHi,
ReplyDeletei have query who can identify the xpath of object on a web page?
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...
ReplyDeleteIn 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