Skip to main content

Selenium - Data Driven Testing

In Automation, we need to runs scripts to test the same functionality with different types of data. This data is usually stored in an excel file and accessed via the script to enter in the Application Under Test.
In QTP, this is done using the DataTable. 
In Selenium we can use the Apache POI API to do read an excel workbook and write the output to the excel file.

Using the API is quite simple if you understand the basics.
POI - Poor Obfuscation Implementation. This name was humorously given  because Microsoft code was deliberately made difficult to reverse engineer but still it was reverse engineered.

POIFS - Poor Obfuscation Implementation File System. Used to access the input file.

HSSF - Horrible Spreadsheet Format This is used to read and write .xls files

XSSF - XML Spreadsheet Format This is used to read and write .xlsx files.


FileInputStream fs=new FileInputStream("C:\\Users\\Krishna\\selenium workspace\\Data\\firstone.xls");
POIFSFileSystem poi=new POIFSFileSystem(fs);
HSSFWorkbook hw=new HSSFWorkbook(poi);
HSSFSheet hs=hw.getSheet("Global");
Object data[][]=new Object[2][2];
System.out.println(hs.getLastRowNum());// last row number in excel
System.out.println(hs.getPhysicalNumberOfRows());//number of physically defined rows
System.out.println(hs.getRow(0).getLastCellNum()); // last cell number in row
System.out.println(hs.getRow(0).getPhysicalNumberOfCells()); // number of physically defined cells
for(int i=1;i<=hs.getLastRowNum();i++){
for(int j=0;j<hs.getRow(0).getLastCellNum();j++)
{
data[i-1][j]=hs.getRow(i).getCell(j).toString();
System.out.println(data[i-1][j]);
}
}
fs.close();

Use with Junit:
@Parameters
public static Collection<Object[]>(){
return Arrays.asList(data);
//data should be declared static in this case
}
The above code can be used to read data from excel sheet and store it in the object data array.
To understand the code remember the following hierarchy.

Access File -> Open the accessed file using POIFSFileSystem class -> Access the HSSFWorkBook workbook in the POIFSFileSystem -> Access the HSSFSheet sheet in the workbook using getSheet(sheetname) -> Access the row in the sheet using getRow(rownumber) -> Access the cell in the row using getCell(cellnumber)

getLastRowNum () gets the number last row on the sheet. Owing to idiosyncrasies in the excel file format, if the result of calling this method is zero, you can't tell if that means there are zero rows on the sheet, or one at position zero. For that case, additionally call getPhysicalNumberOfRows() to tell if there is a row at position zero or not.





Comments

Popular posts from this blog

Switch between browsers\windows and pop ups

These commands can be used to switch between windows, iterate through different windows opened by the webdriver, handle pop ups and frames in windows.  GetWindowHandle Command Purpose : To get the   window handle   of the current window. driver.getWindowHandle(); Returns an alphanumeric string GetWindowHandles Command Purpose : To get the   window handle   of   all   the current windows. Set<String> handle=Driver.getWindowHandles(); Returns a set of window handles SwitchTo Window Command Purpose : WebDriver supports   moving   between named windows using the “switchTo” method. For(String handle:driver.getWindowHandles()) Driver.switchto().window(handle); Or Driver.switchto().window(windowname); <A href="newwindow.html" target=" windowname ">click here for a new window </A> SwitchTo Frame Command Purpose : WebDriver supports moving between frames using the “ switchTo ” method. Driver.s...

How to find a WebElement

Find Element & Find Elements Method The difference between “ Find Elemen t” and “ Find Elements ” method is the first returns a WebElement object otherwise it throws an exception and the latter returns a list of WebElements, it can return an empty list if no DOM elements match the query. The “Find” methods take a locator or query object called “ By ”. “ By ” methods are listed below. By ID With this strategy, the first element with the id attribute value matching the location will be returned. If no element has a matching id attribute, a  NoSuchElementException   will be raised. This is the most efficient and preferred way to locate an element, as most of the times IDs are unique. But in some cases UI developers make it having non-unique ids on a page or auto-generating the id, in both cases it should be avoided. Example : If an element is given like this: <input id=”username”></input> WebElement we=driver.findElement(By.id(“username”));  ...

Selenium - What Why Who How?

What is Selenium? Selenium is an open source automated   testing   suite for web applications across different browsers and platforms.   It is quite similar to HP Quick Test Pro ( QTP ) only that Selenium focuses on automating web-based applications. Why use Selenium? Selenium is fast and it supports programming in multiple languages like Java, C#,Python and Ruby.  So development of automation scripts can be done in your language of preference. Selenium works well with AJAX testing. Selenium Grid supports execution of test cases in multiple platforms and multiple browsers in remote systems as well.  The support for different programming languages gave selenium the power to harness the capabilities of those programming languages .  For example, using JAVA as the programming language to code selenium scripts enables selenium to use JDBC for database access and testing.All the APIs supported in Java also become the advantage of Selenium like L...