Selenium being open source tools, it has not got many in built features like the commercial tools.But that is not a show stopper at all. There are innumerable methods to achieve the things in selenium. As being rightly said by one of the test managers in Microsoft company – the need of the day to is to use a test tool which will bring several features scattered away, in one place. Selenium is exactly doing that in my opinion.
So, coming to the topic-question. If the question is whether we can implement Data driven framework? the answere is Yes. Selenium has any in-built function to do it? NO.
I will discuss here the process that i adopted to achieve it.
1. use apache POI to read/write the excel.
2. Use the TestNG framework – to drive the test method with the data from excel.
Step#1 – How to read data from excel. There are several options. One way is to use JXL. But i felt that it requires lot of coding to read the data, store it in matrix and supply the data. So i went for Apache POI APIs.
Step#2 – Why TestNG? It makes life simple for data driven framework!!! What you need to do is only this –
1. create a method which will read the data from excel and store it in matrix (using Apache poi).
2.Using test annotation, give a name to this method : @DataProvider (name = ‘dp’)
3.Tell your @Test that this is your dataprovider : @Test (DataProvider = ‘dp’). This will make the Test to run as many number of times as the number of rows or data are there in the excel.
I will share the code that i wrote to read the data from excel and store it in an array, using Apache Poi
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
public class apacheExcel {
static int r = 0, c =0, t=0;
static String[][] tabArray = new String[100][100];
public static String[][] getArrayTable (String xlPath) throws Exception{
InputStream inputStream = new FileInputStream(xlPath);
POIFSFileSystem fileSystem = new POIFSFileSystem (inputStream);
HSSFWorkbook workBook = new HSSFWorkbook (fileSystem);
HSSFSheet sheet = workBook.getSheetAt(0);
Iterator<Row> rows = sheet.rowIterator();
while (rows.hasNext())
{
Row row = rows.next();
Iterator<Cell> cells = row.cellIterator();
while (cells.hasNext())
{
Cell cell = cells.next ();
tabArray[r][c] = cell.getStringCellValue ();
++c;
}
t=c;
c=0;
++r;
}
return tabArray;
}
public static void printArray(String[][] array) throws Exception
{
for (int i=0; i<r;i++)
{
for(int j=0;j<t; j++)
System.out.println(array[i][j]+ ” “);
}
}
public static void main (String[] args) throws Exception
{
getArrayTable (“C:\\selDataFolder\\googleTest.xls”);
printArray(tabArray);
}
}
I hope the code is simple for everyone to understand. If anyone needs any help just drop a comment.