-1
Excel Reader:无法使用数据提供者在测试方法中提取参数
package com.Excel.Read;
public class 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:
包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);
}
}`