我使用TestNg数据提供者和Apache POI API从Excel表传递参数。Selenium网络驱动程序从Excel中传入参数表
我遇到的麻烦是传入一个数值。我的代码:
@Test(dataProvider = "RegisterPage")
public void registerPage(String foreName, String surName, int dateOfBirth, String addressLine1, String addressLine2, String addressLine3, String City,
String County, String postCode, String nationalInsuranceNO, int telephoneNo, String userName, String confirmUsername, String password,
String confirmPassword, String memorableWord){
我从控制台得到的错误是:无法从数字单元得到的文本值
ExcelUtils类文件代码:
package testNG;
//Excel Function to open, read and write parameters/values from an Excel Document to class files or tests.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtils {
private static XSSFSheet ExcelWSheet;
private static XSSFWorkbook ExcelWBook;
private static XSSFCell Cell;
private static XSSFRow Row;
public static Object[][] getTableArray(String FilePath, String SheetName) throws Exception {
String[][] tabArray = null;
try {
FileInputStream ExcelFile = new FileInputStream("C://Selenium-java-maven//workSpace//SeleniumWebDriver-TestNG//src//regData//RegisterOLPTestData.xlsx");
// Access the required test data sheet
ExcelWBook = new XSSFWorkbook(ExcelFile);
ExcelWSheet = ExcelWBook.getSheet(SheetName);
int startRow = 1; // Starts from row 1 in Excel not row 0
int startCol = 1; // Starts from Columns 1 in Excel not row 0
int ci,cj;
int totalRows = ExcelWSheet.getLastRowNum();
// you can write a function as well to get Column count
int totalCols = 3; //Total Sheet columns
tabArray=new String[totalRows][totalCols];
ci=0;
for (int i=startRow;i<=totalRows;i++, ci++) {
cj=0;
for (int j=startCol;j<=totalCols;j++, cj++){
tabArray[ci][cj]=getCellData(i,j);
System.out.println(tabArray[ci][cj]);
}
}
}
catch (FileNotFoundException e){
System.out.println("Could not read the Excel sheet");
e.printStackTrace();
}
catch (IOException e){
System.out.println("Could not read the Excel sheet");
e.printStackTrace();
}
return(tabArray);
}
public static String getCellData(int RowNum, int ColNum) throws Exception {
try{
Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
int dataType = Cell.getCellType();
if (dataType == 3) {
return "";
}else{
String CellData = Cell.getStringCellValue();
return CellData;
}}catch (Exception e){
System.out.println(e.getMessage());
throw (e);
}
}
}
哪一行是您遇到此问题的行? –
@Lajos Arpad,我只有2排。第一行只是标题,但是,我从第1行开始,因此第0行是标题不计算在内。所以只有行1. 1行16列。 – Speedychuck