在 Selenium WebDriver 中使用 Apache POI 使用 TestNG 进行 DataDriven 测试 [英] DataDriven testing with TestNG using Apache POI in Selenium WebDriver

查看:31
本文介绍了在 Selenium WebDriver 中使用 Apache POI 使用 TestNG 进行 DataDriven 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 TestNG 框架的新手.请指导如何使用Apache POI(Excel)参数化测试用例.

I am new to TestNG framework. Please guide how to parameterise the test cases using Apache POI(Excel).

我有一个代码可以从 Excel 的第二行读取.

I have a code to read from second row from Excel.

public class spreadData {

private transient Collection data = null;

public spreadData(final InputStream excelInputStream) throws IOException {
    this.data = loadFromSpreadsheet(excelInputStream);
}

public Collection getData() {
    return data;
}

private Collection loadFromSpreadsheet(final InputStream excelFile)
        throws IOException {
    HSSFWorkbook workbook = new HSSFWorkbook(excelFile);

    data = new ArrayList();
    Sheet sheet = workbook.getSheetAt(0);

    int numberOfColumns = countNonEmptyColumns(sheet);
    List rows = new ArrayList();
    List rowData = new ArrayList();

   /*for (Row row : sheet) {
        if (isEmpty(row)) {
            break;
        } else {
            rowData.clear();
            for (int column = 0; column < numberOfColumns; column++) {
                Cell cell = row.getCell(column);
                rowData.add(objectFrom(workbook, cell));
            }
            rows.add(rowData.toArray());
        }
    }*/


    int rowStart = 1;
 //int rowEnd = Math.max(1400, sheet.getLastRowNum());
    for (int rowNum = rowStart; rowNum <=  sheet.getLastRowNum(); rowNum++) {
        //Row r = sheet.getRow(rowNum);
        Row read = sheet.getRow(rowNum);
        if (isEmpty(read)) {
            break;
        } else {
            rowData.clear();
            for (int column = 0; column < numberOfColumns; column++) {

                Cell cell = read.getCell(column);
                rowData.add(objectFrom(workbook, cell));
            }
            rows.add(rowData.toArray());
        }
    }
    return rows;
}

private boolean isEmpty(final Row row) {
    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(final Sheet sheet) {
    Row firstRow = sheet.getRow(0);
    return firstEmptyCellPosition(firstRow);
}

private int firstEmptyCellPosition(final Row cells) {
    int columnCount = 0;
    for (Cell cell : cells) {
        if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
            break;
        }
        columnCount++;
    }
    return columnCount;
}

private Object objectFrom(final HSSFWorkbook workbook, final Cell cell) {
    Object cellValue = null;

    if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
        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;
}

}

我的问题是如何使用 TestNG 参数化测试用例?意味着通过使用@DataProvider TestNG 注释.请帮助我提供示例代码或解释.

My question is how to parameterise the test cases using TestNG? means by using @DataProvider TestNG annotation. Kindly help me with a sample code or explanation.

推荐答案

我不确定您想要哪组数据...但这里是 dataProvider 的工作原理:

I'm not sure which set of data you want...but here's how a dataProvider works:

假设我有一个测试,它需要一个 String 和一个像这样的 int:

Say I have a test that takes a String and then an int like this:

@Test(dataProvider = "excelData")
public void executeTest(String name, int value){}

我的数据提供者看起来像这样:

My data provider would look something like this:

@DataProvider(name = "excelData")
public Object[][] data(){
    return new Object[][]{
               {"Test",1},
               {"More Testing",7},
               {"Last Test",-5}}
}

测试将运行 3 次,数组的每一行都是要传入的数据集.您需要将您的 excel 数据转换为这样的格式.

The test will be run 3 times, with each row of the array is the set of data that is to be passed in. You will need to convert your excel data into such a format.

注意,如果您愿意,您也可以返回 Iterator.

Note, you can also return an Iterator<Object[]> if you prefer.

这篇关于在 Selenium WebDriver 中使用 Apache POI 使用 TestNG 进行 DataDriven 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆