繁体   English   中英

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

[英]Selenium Web-driver Passing in parameters from Excel Sheet

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

                }

            }

    }

问题是您尝试将String作为int传递。 如果您有一个期望使用整数的方法,但是您传递了String ,那么您将收到此错误。 您可以更改参数的类型,重载方法或传递Integer.parseInt(foo)而不是foo

编辑:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM