我有一个登录页面,其中包含用户名和密码。我有一个XcelParserTestNGLogin类,用于创建,更新和从Excel工作表方法加载。另一个类Login是TestNG类。我正在使用DataProvider从Excel传递数据。但是我遇到异常数据提供者试图传递4个参数,但是该方法需要2个。

这是我的TestNG代码:

public class Login {

    private static WebDriver driver;
    XcelParserTestNGLogin login1 = new XcelParserTestNGLogin();
    Object[][] data1;

    /*public Login() throws IOException, InterruptedException {
        FileInputStream fis = new FileInputStream("Data//LoginPage.xls");
        XcelParserTestNGLogin login1 = new XcelParserTestNGLogin(fis, "Login");

        //this.data1 = login1.loadFromSpreadsheet(fis, "Login");
    }*/

    @BeforeClass
    public void test() throws Exception {
        System.setProperty("webdriver.chrome.driver",
                "C:\\Chrome\\chromedriver_win_26.0.1383.0\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("Any Url");
    }
    @DataProvider
     public Object[][] dp() throws IOException {
        //login1.fileName = "Data//Login.xls";
        //login1.sheetName = "Sheet1";
        FileInputStream fis = new FileInputStream("Data//LoginPage.xls");
        String sheetName = "Login";
        login1.loadFromSpreadsheet(fis,sheetName);
        return login1.getData();
    }

    @Test(dataProvider = "dp")
    public void devLogin(String UserName,String PassWord) throws InterruptedException, IOException {

        driver.findElement(By.name("txtUserName")).sendKeys(UserName);
        driver.findElement(By.name("txtPwd")).sendKeys(PassWord);
        driver.findElement(By.name("btnSignIn")).click();
        Thread.sleep(5000);

        if (driver.findElement(By.linkText("DashBoard")).isDisplayed()) {
            List<String> arrayList = new ArrayList<String>();
            arrayList.add("Pass");
            HSSFWorkbook workbook = new HSSFWorkbook();
            login1.createSheet("Login", workbook, arrayList);
        }
        else {
            try{
            Alert alert=driver.switchTo().alert();
            String alertText=alert.getText();

            Assert.assertEquals("invalid username or password,please try again",alertText);
            alert.accept();
            }catch(UnhandledAlertException e){
                e.printStackTrace();
            }
            List<String> arrayList = new ArrayList<String>();
            arrayList.add("Fail");
            HSSFWorkbook workbook = new HSSFWorkbook();
            login1.createSheet("Login", workbook, arrayList);
        }
    }
}


这是我的XcelParserTestNGLogin()代码

public class XcelParserTestNGLogin {
    private transient Object[][] data;
    String fileName,sheetName;

    public XcelParserTestNGLogin() {

    }

    public XcelParserTestNGLogin(InputStream excelInputStream, String sheetName)
            throws IOException {
        this.data = loadFromSpreadsheet(excelInputStream, sheetName);
    }

    public Object[][] getData() {
        return data;

    }

    Object[][] loadFromSpreadsheet(InputStream excelFile, String sheetName)
            throws IOException {
        // TODO Auto-generated method stub
        HSSFWorkbook workbook = new HSSFWorkbook(excelFile);
        Sheet sheet = workbook.getSheet(sheetName);

        int numberOfColumns = countNonEmptyColumns(sheet);
        int numberOfRows = sheet.getLastRowNum() + 1;

        data = new Object[numberOfRows - 1][numberOfColumns - 1];

        for (int rowNum = 1; rowNum < numberOfRows; rowNum++) {
            Row row = sheet.getRow(rowNum);
            if (isEmpty(row)) {
                break;
            } else {
                for (int column = 1; column < numberOfColumns; column++) {
                    Cell cell = row.getCell(column);
                    if (cell == null
                            || cell.getCellType() == Cell.CELL_TYPE_BLANK) {
                        data[rowNum - 1][column - 1] = "";
                    } else {
                        data[rowNum - 1][column - 1] = objectFrom(workbook,
                                cell);
                    }
                }
            }
        }

        return data;
    }

    private boolean isEmpty(Row row) {
        // TODO Auto-generated method stub
        Cell firstCell = row.getCell(0);
        boolean rowIsEmpty = (firstCell == null)
                || (firstCell.getCellType() == Cell.CELL_TYPE_BLANK);
        return rowIsEmpty;
    }

    /**
     * Count the number of columns, using the number of non-empty cells in the
     * first row.
     */
    private int countNonEmptyColumns(Sheet sheet) {
        // TODO Auto-generated method stub
        Row firstRow = sheet.getRow(0);
        return firstEmptyCellPosition(firstRow);
    }

    private int firstEmptyCellPosition(Row cells) {
        // TODO Auto-generated method stub
        int columnCount = 0;
        for (Cell cell : cells) {
            if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
                break;
            }
            columnCount++;
        }
        return columnCount;
    }

    private Object objectFrom(HSSFWorkbook workbook, Cell cell) {
        // TODO Auto-generated method stub
        Object cellValue = null;
        if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
            cellValue = cell.getRichStringCellValue().getString();
        } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            cellValue = getNumericCellValue(cell);
        } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
            cellValue = cell.getBooleanCellValue();
        } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
            cellValue = evaluateCellFormula(workbook, cell);
        }

        return cellValue;
    }

    private Object getNumericCellValue(final Cell cell) {
        Object cellValue;
        if (DateUtil.isCellDateFormatted(cell)) {
            cellValue = new Date(cell.getDateCellValue().getTime());
        } else {
            cellValue = cell.getNumericCellValue();
        }
        return cellValue;
    }

    private Object evaluateCellFormula(final HSSFWorkbook workbook,
            final Cell cell) {
        FormulaEvaluator evaluator = workbook.getCreationHelper()
                .createFormulaEvaluator();
        CellValue cellValue = evaluator.evaluate(cell);
        Object result = null;

        if (cellValue.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
            result = cellValue.getBooleanValue();
        } else if (cellValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            result = cellValue.getNumberValue();
        } else if (cellValue.getCellType() == Cell.CELL_TYPE_STRING) {
            result = cellValue.getStringValue();
        }

        return result;
    }
    public void updateExcel(final InputStream excelFile, String SheetName,
            List<String> list) {
        HSSFWorkbook workbook = new HSSFWorkbook();
        Sheet sheet = null;
        if (workbook.getSheetIndex(SheetName) > 0) {
            sheet = workbook.getSheet(SheetName);
            if (list != null && list.size() != sheet.getLastRowNum()) {
                workbook.removeSheetAt(workbook.getSheetIndex(SheetName));
                createSheet(SheetName, workbook, list);
            } else {
                createSheet(SheetName, workbook, list);
            }
        }

    }
    void createSheet(String SheetName, HSSFWorkbook workbook, List<String> list) {
        // TODO Auto-generated method stub
        String[] Heading = {"UserName", "Password",
                "Result" };
        Sheet sheet = workbook.createSheet(SheetName);
        HSSFRow row = null;
        HSSFCell cell = null;

        row = (HSSFRow) sheet.createRow(0);
        for (int cellNum = 0; cellNum < Heading.length; cellNum++) {
            cell = row.createCell(cellNum);
            cell.setCellValue(Heading[cellNum]);
        }
        for (int rowNum = 1; rowNum <= list.size(); rowNum++) {
            String[] cellVals = {"uname",
                    "pswd", list.get(rowNum - 1) };

            row = (HSSFRow) sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < cellVals.length; cellNum++) {
                cell = row.createCell(cellNum);
                if (!(cellNum == cellVals.length))
                    cell.setCellValue(cellVals[cellNum]);
                else
                    cell.setCellValue(true);
            }
        }
        try {
            FileOutputStream out = new FileOutputStream("Data//LoginPage.xls");
            workbook.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    }

最佳答案

请记住,对象数组左边第一个方括号的长度指示将调用您的测试方法的次数。

第二个方括号的长度表示您的测试方法应包含多少个参数。根据测试方法的签名,数组声明应为obj = new Object[maxRows][2]

08-26 19:45