Friday Jul 25, 2008

Reverse XPath - Finding the xpath of a element using bottom up approach

Assume that we need to find the xpath of a link "Login" appearing in a webpage. Usually when we find xpath of a GUI element, we normally follow a top-down approach, meaning the xpath is recorded from the parent level to the child. But there is also a bottom up approach, where in you can begin from a inner child element and navigate all the way up till the GUI element is reached. 

Below is the corresponding xml code and various method of finding XPath of "Login" link. The bottom-up approach is explained by note (e) in the below table.

 Sample XML Code
XPath

(a) Using Top Down approach starting from a top node 
//table/tr/a[text()='Login']

(b) Directly accessing the child node using its text value
//a[text()='Login']

(c) Directly accessing the child node using child's attribute value
//a[@href='1.html']

(d) Directly accessing the child node using wildcard search. All occurences of text "Logi\*" is matched by below xpath.It will work in this example since there is only one text "Login" which has occurence "Logi" in it.
//a[contains(text(),'Logi')] 

(e) Using Bottom Up approach starting from another child node and navigating to the actual node. Note: Here the DOM structure should be maintained (as it is in the xml) between the start node and end node
//a[text()='Home']/../../../table/tr/a[text()='Login']


dsds



Tuesday Jul 08, 2008

Identifying XPath of a element using XPath Query

Below are some of the common xpath query which you will need while doing your selenium automation. Using xpath query, your selenium testing much stable since the slightest change of the GUI wont break your automation.

GUI Element
Corresponding XPath
Remarks
<input type="submit" value="Save"/> //input[@value="Save"] Exactly matches the text "Save"

<input type="submit" value="Save1"/>

<input type="submit" value="Save2"/>

(//input)[1]   matches the 1st input element, "Save1"

(//input)[2]   matches the 2nd input element, "Save2"


<a href="somelink">Structure</a>
//a[text()="Structure"]
Exactly matches the value within a given tag. Here the text "Structure" which is in between the <a> tag (anchor tag)
<a href="somelink">x_structure_y</a> //a[contains(text(),'structure')]
Like wildcard search, matches the input element which has the text "structure" within a given tag
<input type="button" name="ccd_ccd_81533739_removeevent"/>

//input[contains(@name,concat("ccd","_","ccd","_")) and contains(@name,"removeevent")]

Note: The above xpath can be used when the attribute value has a part which is dynamic in nature (here in this case the text in between "ccd_" and "_removeevent" '81533739" keeps on changing everytime the GUI element is accessed)

Matching multiple text(s) pattern of a attribute value
<input type="text" value="Search..."/>
//input[@type='text' and contains(@value,'Search')]
Matches based on multiple attributes of a GUI element . Here the xpath of the text box is derived  based on its 'type' and 'value' attributes

Monday May 12, 2008

XPath: Matching a node element having a specific text

 

 

Problem : To identify uniquely a GUI element having a text "Enterprise Sample" within its <a> tag

Solution : The page source of the web page shows the element as  <a href="clickme.html">Enterprise Sample</a> . Use the below command in xpath checker to uniquely identify this element

 //a[contains(text(),"Enterprise")]

In layman's term, the above command will look 

  1. first for all occurrence of <a> tag within the web page
  2. then matches the value of <a> tag with user's text (here "Enterprise")

 

 

 

Monday Mar 03, 2008

XPath : Finding GUI element by matching multiple text(s) pattern of a attribute value

Recently i had to spend my whole day searching for ways to find a elements' XPath (a ajax based button) by matching multiple text pattern within a single attribute value. I was using Selenium IDE to automate testing of Sun Portal Server GUI which is based on Ajax.

My Requirement : Uniquely Identify a ajax based button having the below XPath

//a[@id,'ccd_ccd_81533739_removeevent']

 

My Problem : Attribute(@id) value has both constant and dynamic texts which makes it difficult to uniquely identify the element based on its value

If you look at my ajax button's xpath , there is a numeral "81533739" which is not constant and it changes everytime but the first part(ccd_ccd_) and the second part(_removeevent) of the @id value is constant . I want to uniquely identify @id attribute based on these constant text alone

Solution:The below xpath will uniquely identify my element using concat() and contains() XPath methods

//a[contains(@id,concat("ccd","_","ccd","_")) and contains(@id,"removeevent")]

 

Below are few links which i found helpful for this solution:

About

I'm a Quality Engineer at Sun Microsystems. Here in this blog, you may find information about Software Testing, Portal Server, Virtualization, Web2.0 and Misc Technical topics. The views expressed here are personal and does not reflect that of my employer

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today