Monday, October 27, 2014

Know your x-Path

Inspecting elements using xpath in particular page is the most crucial step while automating web app just because if your xpath is not unique and stable enough then your script is of no further use, in this post I will explain example on the following:-
  • Mixture of structure and attributed xpaths in-order to get stable and unique xpath.
  • Parents/child structure of html and finding xpath from it.
  • Elements having same tabname and info in that case, use of indexing to fetch the required element in page.
  •  Where to use "click" and "click at" method.
  •  Use of storeXpathCount.
lets start with all of this in detail :-

A. How to check that your xpath is stable and unique?  A good answer to this is tester should login and visit the page atleast 2 or 3 times and verify the same xpath.

B. Moving to next point i.e how to inspect element through text between the HTML tags below are the examples:-

//tagname[text()='text in the page']

we can also use with contains method if we have extra space in the heading or text in the page, so to avoid that problem tester should use contains method and below is the example

//tagname[contains(text(),'heading or text in page')]

If we have title attribute available and we want to use it with contains method so below is the example:-

//tagname[contains(@title,'heading or text in page')]

C. For parent child structure I have taken an example  to illustrate:-

 <td class = "searchUI" align= "left">
             <span> search</span>
  </td>

here you can see we do not have any attribute with span only we have tagname i.e span, so we can't say this is stable and unique if the text changes every time say currently it is "search" and on refresh it changes to "find".
So in that case we have to  look at the parent of that span which is "td"tag here, we can create xpath like this :-

//td[@class='searchUI']/span

Now if we have multiple span with some attribute under the same "td" then we can go for attribute like
//td[@class='searchUI']/span[@class='module']

it will look for "td" then it will look for "span" which has class attribute.

Again now what if there is no attribute available and multiple spans under tag 'td':-

xpath=(//td[@class='searchUI']/span)[2]

this is called indexing.

Below is the pictorial representation of Google search page and its her-racy :-











D. In general we do use "click" method over "click at" as the execution of "click at" is slow as comapred to "click", so "click at" is preferred to use when you are not able to click on particular button using "click".

E. Lets discuss one more error in the page i.e you are able to inspect element on page but its not visible when you click i.e  UI has no element but u are able to inspect

Usually in tables we have some hidden buttons or elements  available so we can count the all buttons available and filter out which is visible or which is not in use by using storeXpathCount.

So on the basis of available xpath on page it will give count of the xpath

storeXpathcount ->command in selenium ide

x ->variable to store result of the above command

echo ${x}-> to view the results

Below is the pictorial representation:- 


No comments:

Post a Comment