Testcase:
package Test;
import org.testng.annotations.AfterTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import Page.LoginPage;
import Utility.TestDataProvider;
import Utility.TestUtil;
//Testcase to perform login
public class LoginTest {
LoginPage page=new LoginPage();
@Test(dataProvider="ExcelDataProvider",dataProviderClass=TestDataProvider.class)
public void Logintest(String username,String password){
//page.login("user@phptravels.com","demouser");
page.login(username,password);
}
@AfterTest
public void TearDown(){
TestUtil.quit();
}
}
DataProvider:
package Utility;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestDataProvider {
static ExcelReader reader=new ExcelReader();
@DataProvider(name="ExcelDataProvider")
public static Object[][] ExcelDataProvider()
{
Object [][] rest = reader.readDataExcel("UserLogin");
return rest;
}
}
ExcelReader:
package Utility;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFTable;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelReader {
static String fileName="Testdata";
public Object[][] readDataExcel(String TableName){
String filePath="F:\\TravelSite\\TravelSiteAutomation\\Testdata.xlsx";
File file =new File(filePath);
try{
FileInputStream stream=new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(stream);
XSSFSheet sheet=(XSSFSheet) workbook.getSheet("UnitTest");
List<XSSFTable> tables = sheet.getTables();
for(XSSFTable table:tables){
String name=table.getName();
if(name.equals(TableName)){
int ci=0,cj=0;
int rowNum=sheet.getLastRowNum()-sheet.getFirstRowNum();
//System.out.println(rowNum);
String[][] dataArray=new String[rowNum][100];
for(int i = 1; i < rowNum+1; i++) {
Row row = sheet.getRow(i);
//Create a loop to print cell values in a row
int colNum=row.getLastCellNum();
// System.out.println(colNum);
for(int j = 0; j <colNum; j++) {
//Print excel data in console
Cell cell=row.getCell(j);
DataFormatter formatter = new DataFormatter();
String var_name = formatter.formatCellValue(cell);
System.out.print(var_name+" || ");
dataArray[ci][cj]=var_name;
}
System.out.println();
}
workbook.close();
return dataArray;
}
}
Log.info("Table name is incorrect");
return null;
}
catch(Exception e)
{
Log.debug(e.getMessage());
return null;
}
}
}
so when i try to run the testcase i get an error org.testng.TestNGException:
Data Provider public static java.lang.Object[][] Utility.TestDataProvider.ExcelDataProvider() must return either Object[][] or Iterator[], not class [[Ljava.lang.Object;
Nothing seems to resolve it,POI version 3.15, TestNGversion 6.10
The problem is with the ExcelReader
class. Please find below the fixed version of the same which should solve the problem
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFTable;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.util.ArrayList;
import java.util.List;
public class ExcelReader {
public static String[][] readDataExcel(String TableName) {
String filePath = "src/test/resources/Testdata.xlsx";
try {
Workbook workbook = new XSSFWorkbook(filePath);
XSSFSheet sheet = (XSSFSheet) workbook.getSheet("UnitTest");
List<XSSFTable> tables = sheet.getTables();
for (XSSFTable table : tables) {
String name = table.getName();
if (! name.equals(TableName)) {
continue;
}
int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
System.out.println(rowCount);
int columnCount = - 1;
List<List<String>> data = new ArrayList<>();
for (int i = 1; i < rowCount + 1; i++) {
Row row = sheet.getRow(i);
int colNum = row.getLastCellNum();
if (columnCount == - 1) {
columnCount = colNum;
}
List<String> rowData = new ArrayList<>();
for (int j = 0; j < colNum; j++) {
Cell cell = row.getCell(j);
DataFormatter formatter = new DataFormatter();
String var_name = formatter.formatCellValue(cell);
if (var_name != null && ! var_name.trim().isEmpty()) {
rowData.add(var_name);
}
}
if (! rowData.isEmpty()) {
data.add(rowData);
}
}
workbook.close();
String[][] dataArray = new String[rowCount-1][columnCount];
int rowIndex = 0;
for (List<String> row : data) {
int colIndex = 0;
for (String rowData : row) {
dataArray[rowIndex][colIndex++] = rowData;
}
rowIndex++;
}
return dataArray;
}
System.err.println("Table name is incorrect");
return new String[][] {{}};
} catch (Exception e) {
e.printStackTrace();
return new String[][] {{}};
}
}
}