Excel Reader:
package com.Excel.Read;
公共类ReadFile { static Sheet excelWSheet;
static Workbook excelWBook;
static Row row;
public static String[][] getExcelData(String fileName, String sheetName, int startRow,int targetRow, int startCol, int targetCol) throws EncryptedDocumentException, InvalidFormatException, IOException {
String[][] arrayExcelData = null;
try {
FileInputStream fs = new FileInputStream(fileName);
excelWBook = WorkbookFactory.create(fs);
int rowCount = excelWBook.getSheet(sheetName).getLastRowNum();
int colCount;
System.out.println(rowCount);
arrayExcelData = new String[targetRow-startRow+1][targetCol-startCol+1];
for(int i = startRow ; i < targetRow; i++) {
colCount = excelWSheet.getRow(i).getLastCellNum();
System.out.println(colCount);
for (int j = startCol; j <= targetCol; j++) {
arrayExcelData[i-startRow][j-startCol] = row.getCell(j).getStringCellValue();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return arrayExcelData;
}
}
识别TestClass:
package com.Excel.Read;
import org.testng.annotations.DataProvider; import org.testng.annotations.Test;
公共类TestDataProvider {
@Test(dataProvider="getlogincred")
public void setcred(String sUserName, String sPassword, String sexpectedMsg)
{
System.out.println("UserName : " + sUserName);
System.out.println("Password : " + sPassword);
System.out.println("Msg : " + sexpectedMsg);
System.out.println();
}
@DataProvider(name="getlogincred")
public String[][] getlogincred() throws Exception
{
String[][] testObjArray = ReadFile.getExcelData("C:\\Users\\u6035997\\Desktop\\Book1.xlsx", "Sheet1",2,5,2,4);
return (testObjArray);
}
}`
答案 0 :(得分:0)
数据提供返回对象数组。所以你必须通过加载testObjArray来返回Object of Object。以下是简单示例帮助您
@Test(dataProvider="my data")
public void GoogelSeacrhTest(Hashtable<String, String> h) throws InterruptedException{
//my code here
}
@DataProvider(name="my data")
public Object[][] testdata(){
Hashtable<String, String> h=new Hashtable<>();
h.put("search", "murali selenium");
Object[][] ob={{h}};
return ob;
}
所以,你需要改变
public static String[][] getExcelData(String fileName, String sheetName, int startRow,int targetRow, int startCol, int targetCol) throws EncryptedDocumentException, InvalidFormatException, IOException { }
返回对象Object [] []而不是String [] [],然后,只返回比在数据提供者中传递必需参数的对象
@DataProvider(name="getlogincred")
public Object[][] getlogincred() throws Exception
{
return ReadFile.getExcelData("C:\\Users\\u6035997\\Desktop\\Book1.xlsx", "Sheet1",2,5,2,4);
}
请参阅文档here以获取更多信息。
谢谢你, 穆拉利