DataProviders in Testng with ExcelSheet

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.DataProvider;
public class ExcelReader {

@DataProvider(name = "readUserPwd") { 
public static Object[][] readUserPwd() throws IOException {

    FileInputStream fis = new FileInputStream(new File("C:\\Users\\ankushs\\Desktop\\ExcelDemo.xlsx"));

    XSSFWorkbook workbook = new XSSFWorkbook(fis);

    XSSFSheet sheet = workbook.getSheetAt(0);
    // ------------------------------------------------------------

    int lastRow = sheet.getLastRowNum();

    int lastCell = sheet.getRow(0).getLastCellNum();

    System.out.println(lastRow);
    System.out.println(lastCell);

    Object[][] obj = new Object[lastRow][lastCell];

    for (int i = 0; i < lastRow; i++) {

        for (int j = 0; j < lastCell; j++) {

            obj[i][j] = sheet.getRow(i).getCell(j).getStringCellValue();

            //System.out.println(obj[i][j].toString());
        }
    }

    return obj;

}
}

import org.testng.annotations.Test;

public class DataProviderExample {

@Test(dataProvider = "readUserPwd", dataProviderClass = ExcelReader.class)
public static void login(String user, String pwd) {

    System.err.println("UserName :" + user + " and " + pwd);

}

}

Design a site like this with WordPress.com
Get started