Selenium Web驱动程序从Excel工作表传递参数

时间:2016-01-21 18:08:23

标签: java selenium testng

我使用TestNg dataprovider和Apache POI api从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);

                }

            }

    }

1 个答案:

答案 0 :(得分:0)

问题是您尝试将$this->Form->create('Model', array( 'onsubmit' => 'return submit();', //other create options ) 作为String传递。如果您有一个需要整数的方法,但是传递了int,那么您将收到此错误。您可以更改参数的类型,重载方法或传递String而不是Integer.parseInt(foo)

编辑:

最后获得了有关该问题的更多信息。问题是参数foo是int并且传递了dateOfBirth。简单的解决方案是将String的类型更改为dateOfBirth,因此最后不会出现类型冲突。根据需要,最好在方法和调用方法的位置使用专门为此目的设计的String,如Date