In below program, we are going to use Data provider and Excel sheet.
Data provider will take the data from Excel sheet.
Java Class-
public class DataProvider1 {
static String filename="F:\\eclipse\\Trip\\TestFile1.xlsx";
static String sheet1="Sheet1";
static int xrows;
static int xcols;
@Test(dataProvider="sheetdata")
public void fillform(String name, String middlename,String lastname, String age, String city, String nationality, String mobile,String email)
{
System.out.println("UserName: "+ name);
System.out.println("Middlename: "+ middlename);
System.out.println("Lastname: "+ lastname);
System.out.println("Age: "+ age);
System.out.println("CityName: "+ city);
System.out.println("Address: "+ nationality);
System.out.println("Mobile: "+ mobile);
System.out.println("Email: "+ email);
System.out.println("*********************");
}
@DataProvider(name="sheetdata")
public Object[][]recods() throws IOException
{
Object[][] data=exlread(filename,sheet1);
return data;
}
public static String[][] exlread(String filepath, String sheet1) throws IOException
{
File fpath=new File(filepath);
FileInputStream fis= new FileInputStream(fpath);
XSSFWorkbook wb= new XSSFWorkbook(fis);
XSSFSheet sheet= wb.getSheet(sheet1);
xrows= sheet.getLastRowNum();
xcols=sheet.getRow(0).getLastCellNum();
String[][]xdata=new String[xrows][xcols];
int Count=0;
for(int i=1;i<=xrows; i++)
{
XSSFRow row=sheet.getRow(i);
for(int j=0; j<xcols;j++)
{
XSSFCell cell= row.getCell(j);
String value= cell.toString();
xdata[Count][j]=value;
}
Count++;
}
return xdata;
}
}
Excel sheet
Output-
This is really helpful
ReplyDelete