Tuesday, 20 November 2012

How to read values from Excel in Selenium Webdriver

One of the most aspect and beneficial of any automation tool is how well it can support parameterization. When one means parametization it is how effectively it can read and write values from excel. The below program illustrates an example to read values from excel, before executing the program we need to add jxl jars to java build path.


Download jars from here    jxl jars


package exchange.skills.com;

import java.io.File;
import java.io.IOException;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class Data_driven {
public String[][] getXLData(String location, String sheetname)
    {
            Workbook w = null;
            try {
                    w = Workbook.getWorkbook(new File(location));
            } catch (BiffException e) {
                    e.printStackTrace();
            } catch (IOException e) {
                    e.printStackTrace();
            }
            Sheet s = w.getSheet(sheetname);
            String a[][] = new String[10][10];
            try
            {
            for (int j=0;j<s.getColumns();j++)
            {
                    for (int i=0;i<s.getRows();i++)
                    {
                            a[j][i] = s.getCell(j, i).getContents();
                            System.out.println(j+" and "+i+" "+a[j][i]);//Prints cell content one by one
                    }
            }


            }
            catch(Exception e)
            {
                    e.printStackTrace();
            }
            return a;
 
}
public static void main(String[] args) {
Data_driven n= new Data_driven();
//n.getXLData("location of excel file stored","sheet name")
n.getXLData("C:\\tc.xls", "sheet");//File where excel is placed-specify 2007 or save as excel workbook
}
}

2 comments:

  1. while running this program, getting null pointer exception.can you please any one help me..

    ReplyDelete