java - 使用 POI HSSF 在 Excel 中迭代具有多个数据的测试脚本

标签 java apache selenium apache-poi

我已经用 excel 中的一组数据在 selenium 中编写了测试用例,但我需要使用多组数据运行。我尝试过循环测试用例,但没有运气。专家请为我提供使用多组数据循环测试用例的代码。下面是我的代码供您引用。ExcelUtil 文件代码

public static XSSFCell getCellData(int RowNum, int ColNum) throws Exception {

        try {
         Cell = sheet.getRow(RowNum).getCell(ColNum);
         if (Cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
         Cell.getNumericCellValue();
         } else if (Cell.getCellType() == Cell.CELL_TYPE_STRING) {
         Cell.getStringCellValue();
         }

        } catch (Exception e) {
        e.printStackTrace();
    }

        return Cell;

    }

测试方法

public static void Accountcreation() throws Exception {

        orprop = testobjects.get_testobjects();
        sheet = xlsdata.xls_Reading("Sheet2");


    XSSFCell name = xlsdata.getCellData(1,0);
        XSSFCell email = xlsdata.getCellData(1, 1);
        XSSFCell website = xlsdata.getCellData(1, 2);
        XSSFCell billingaddress = xlsdata.getCellData(1, 3);
        XSSFCell city = xlsdata.getCellData(1, 4);
        XSSFCell state = xlsdata.getCellData(1, 5);
        XSSFCell postalcode = xlsdata.getCellData(1,6);
        XSSFCell country = xlsdata.getCellData(1, 7);
        XSSFCell description = xlsdata.getCellData(1, 8);
        XSSFCell siccode = xlsdata.getCellData(1, 9);


        commonutilities.click_button("xpath",
                orprop.getProperty("crm_menu_account_css"), wd);
        commonutilities.implicity_wait(30, wd);
        commonutilities.click_button("xpath",orprop.getProperty("crm_account_createaccount_xpath"), wd);

        Assert.assertEquals(wd.getTitle(), "Accounts");
        commonutilities.get_text_from_excel("xpath",
                orprop.getProperty("crm_account_name_xpath"), name, wd);
        commonutilities.get_text_from_excel("xpath",
                orprop.getProperty("crm_account_email_xpath"), email, wd);
        commonutilities.select_list_items("xpath",
                orprop.getProperty("crm_account_phone_xpath"), "Office", wd);
        commonutilities.get_text_from_excel("xpath",
                orprop.getProperty("crm_account_website_xpath"), website, wd);
        commonutilities.get_text_from_excel("xpath",
                orprop.getProperty("crm_account_billingaddress_xpath"),
                billingaddress, wd);
        commonutilities.get_text_from_excel("xpath",
                orprop.getProperty("crm_account_city_xpath"), city, wd);
        commonutilities.get_text_from_excel("xpath",
                orprop.getProperty("crm_account_state_xpath"), state, wd);
        commonutilities.get_text_from_excel("xpath",
                orprop.getProperty("crm_account_postalcode_xpath"), postalcode,
                wd);
        commonutilities.get_text_from_excel("xpath",
                orprop.getProperty("crm_account_country_xpath"), country, wd);
        commonutilities.click_button("Xpath",
                orprop.getProperty("crm_account_copybilling_xpath"), wd);
        // details
        commonutilities.get_text_from_excel("xpath",
                orprop.getProperty("crm_account_description_xpath"),
                description, wd);
        commonutilities.get_text_from_excel("xpath",
                orprop.getProperty("crm_account_details_siccode_xpath"),
                siccode, wd);
        commonutilities.select_list_items("xpath",
                orprop.getProperty("crm_account_details_type_xpath"),
                "Investor", wd);
        commonutilities.select_list_items("xpath",
                orprop.getProperty("crm_account_details_industry_xpath"),
                "Education", wd);
        commonutilities.click_button("xpath",
                orprop.getProperty("crm_account_save_xpath"), wd);
    }

}

最佳答案

使用sheet.getLastRowNum()获取总行数并循环遍历,如下所示。

public static void Accountcreation() throws Exception {

    orprop = testobjects.get_testobjects();
    sheet = xlsdata.xls_Reading("Sheet2");

    for(i=0;i<sheet.getLastRowNum();i++){
       XSSFCell name = xlsdata.getCellData(1,0);
       XSSFCell email = xlsdata.getCellData(i, 1);
       XSSFCell website = xlsdata.getCellData(i, 2);
       XSSFCell billingaddress = xlsdata.getCellData(i, 3);
       XSSFCell city = xlsdata.getCellData(i, 4);
       XSSFCell state = xlsdata.getCellData(i, 5);
       XSSFCell postalcode = xlsdata.getCellData(i,6);
       XSSFCell country = xlsdata.getCellData(i, 7);
       XSSFCell description = xlsdata.getCellData(i, 8);
       XSSFCell siccode = xlsdata.getCellData(i, 9);

       // Other Code Section
    }

} 

关于java - 使用 POI HSSF 在 Excel 中迭代具有多个数据的测试脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31521728/

相关文章:

java - 无法使用selenium下载任何文件

google-chrome - 从 chrome 下载栏中打开下载文件的快捷键(对于 selenium)

java - 使用 Eclipse 创建新项目时出错

java Nested If 或 single if

java - 使用 Java 将文件从 Linux 复制到 NAS Share (Apache FileUtils FileCopy)

php - 从 URL Laravel 5 中删除 index.php

java - 编写 jBehave 示例步骤

java.lang.IndexOutOfBoundsException : Invalid index 13, 大小为 13

java - SLF4J Log4J Jboss 4.3 和 EJB 3.0

docker - 系统尚未使用 systemd 作为 init 系统 (PID 1) 进行引导。无法操作。尝试在 docker 容器中运行 apache2 时