Thursday 18 April 2013

Getting XPath of Web Elements in Internet Explorer

Testing applications in Internet explorer can be tricky,for one it does not have a tool like Firebug and hence XPath finding can be difficult.But since Javascript has full power over the DOM we can write some scripts to get the element XPath by ourselves.

1)Open Internet Exploer
2)Type about:blank in the address bar and hit enter
3)From Favorites main menu select--->Add favorites
4)In the Add a favorite popup window enter name GetXPATH1.
5)Click add button in the add a favorite popup window.
6)Open the Favorites menu and right click the newly added favorite and select properties option.
7)GetXPATH1 Properties will open up. Select the web Document Tab.
8)Copy and Paste the JavaScript Snippet one code from my Git hub repository in the URL field.

9)Click Ok. Click YES on the popup alert.
10)Add another favorite by following steps 3 to 5, Name this favorite GetXPATH2 (step4)
11)Repeat steps 6 and 7 for GetXPATH2 that you just created.
12)Copy and Paste the JavaScript Snippet two code from my Git hub repository in the URL field for GetXPATH2.

13)Repeat Step 9.
 


Javascript Snippet one code:

https://github.com/Madusudanan/Selenium/blob/master/GetXPATH_init.js

Javascript Snippet two code :
https://github.com/Madusudanan/Selenium/blob/master/GetXPATH_final.js

 

You are all done!!

Now to get the XPATH of an element just select the element with your mouse. This would involve clicking the left mouse button just before the element (link, button, image, checkbox, text etc) begins and dragging it till the element ends. Once you do this first select the favorite GetXPATH1 from the favorites menu and then select the second favorite GetXPATH2. At his point you will get a confirmation, hit allow access button. Now open up a notepad file, right click and select paste option. This will give you the XPATH of the element you seek.


This has a limitation however on selecting web elements which  are like menu elements within a drop down box,hidden elements etc.But comes in very handy on a general usage scenario.

By no means this is the best way to work with locators in IE.Good stable locators are often hand crafted and carefully chosen using Xpath functions and other advanced strategies.But this is good as a starting point and to build upon further


 

Wednesday 17 April 2013

Using Apache POI for Data driven testing in Selenium Webdriver

Selenium is probably the most famous open source tool for Web testing.This blog post is how to use POI in selenium web driver so that data driven testing can be performed.

Why Excel?

Excel is probably the most commonly used tool for working with test data.


You can find more about Apache POI at http://poi.apache.org/












Code is self-explanatory,documented for understanding and is hosted at Github


Code for XLS files:

https://github.com/Madusudanan/Selenium/blob/master/readExcel.java



Code for XLSX files : 

https://github.com/Madusudanan/Selenium/blob/master/readExcelXLSX.java

The above code assumes that you have a structure to your excel files.The two-dimensional array gives you great control over the way you can iterate over the rows and columns of the excel file and this control comes when you have structure to your test data file,but if there is any blank cell in the middle, there is a null pointer exception that is thrown since the code assumes that the file is structured.We can add a handler here by means of a missing cell policy, but that would make the very purpose of using the two dimensional array as a backend structure meaningless.


If you dig into Apache POI a little more, you would be tempted to use its capabilities such ignoring blank cells and so on,but its a lot easier to maintain the excel file in a specified format rather than writing code to handle the brittleness of the excel file itself.The two dimensional structure forms a model through which you can build a data driven framework on top of excel using Apache POI.


If you face any issues,do let me know by commenting here or dropping me an email.