2016-01-21 86 views
0

我使用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); 

       } 

      } 

    } 
+0

哪一行是您遇到此问题的行? –

+0

@Lajos Arpad,我只有2排。第一行只是标题,但是,我从第1行开始,因此第0行是标题不计算在内。所以只有行1. 1行16列。 – Speedychuck

回答

0

的问题是,你尝试作为int传递String。如果你有一个方法,期望一个整数,但你通过String,那么你会收到这个错误。您要么更改参数的类型,超载该方法或通过Integer.parseInt(foo)而不是foo

编辑:

终于得到了关于这个问题的更多信息。问题是参数dateOfBirth是int并传递String。简单的解决方案是将dateOfBirth的类型更改为String,因此最终不会出现类型冲突。根据需要,最好使用专门为此目的设计的class,如Date,无论是在方法中还是在调用方法的地方。

+0

所以我会举例如Integer.parseInt(“29/03/1995”)?而不是int dateOfBirth?还会使用XML来存储参数比使用Excel更好的做法? @Lajos Arpad – Speedychuck

+0

@Speedychuck,因为你终于给出了关于这个问题的更多细节,我们知道dateOfBirth不是作为int传递的。它作为日期传递,表示为String。您可以将参数类型更改为字符串,也可以将类型和变量都传递给日期类型。后者更优雅。 –

相关问题