从excel Sheet获取元素

时间:2015-08-13 07:27:12

标签: selenium-webdriver apache-poi

我创建了我的脚本以验证我的实际结果和预期结果。

因为有太多链接到经过验证的脚本会得到太多的编码,所以我需要将其转换为数据驱动的案例,。

Webdriver将从excel获取URL,xpath,期望值。

但不知道如何继续,

非常感谢演示代码

这是我目前的剧本:

wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
rpm -ivh epel-release-7-5.noarch.rpm
yum install --enablerepo="epel" php-mcrypt

Here is field i had created

如何迭代到每一行并让我的测试用例运行!!!

如果有人可以将我现有的代码转换为在excel表上工作,那将非常有用。

谢谢

1 个答案:

答案 0 :(得分:0)

我正在开发一个从excel(.xls)读取的Spring项目,这是我的代码,如果有帮助的话

private List<String> extraire (String fileName) throws IOException {
    List<String> liste = new ArrayList();
    FileInputStream fis = new FileInputStream(new File(fileName));
    HSSFWorkbook  workbook = new HSSFWorkbook(fis);
    HSSFSheet spreadsheet = workbook.getSheetAt(0);
    Iterator < Row > rowIterator = null;
    rowIterator = spreadsheet.iterator();
    while (rowIterator.hasNext()) {
        int i = 0;
        row =  (HSSFRow) rowIterator.next();
        Iterator < Cell > cellIterator = row.cellIterator();
        while ( cellIterator.hasNext()) {
            Cell cell = cellIterator.next();
            i++;
            /**
             * For verifying if a line is empty
             */
            if (i % 29 == 0 || i == 1) {
                while ( cellIterator.hasNext() && cell.getCellType() == Cell.CELL_TYPE_BLANK) {
                    cell = cellIterator.next();
                }
            }

            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_NUMERIC:
                    String cellule = String.valueOf(cell.getNumericCellValue());
                    liste.add(cellule);
                    break;
                case Cell.CELL_TYPE_STRING:
                    liste.add(cell.getStringCellValue());
                    break;
                case Cell.CELL_TYPE_BLANK:
                     cellule = " ";
                             liste.add(cellule);
                             break;
            }
        }
    }
        fis.close();
        return liste;
}

在我的控制器中:

List<String> liste = new ArrayList();
liste = extraire(modelnom);

for (int m=0, i=29;i<liste.size();i=i+29) {
    if(i % 29 == 0) {
        // i=i+29 : begin from the second line first coll 
        // m is line
        m++;
    }

String matricule = (String)liste.get(29*m).toString().trim();
float mat = Float.parseFloat(matricul); // From String to float
employe.setMatricule((int)mat); //reading mat as an int

// ... your Code
}