使用POI HSSF在Excel中使用多个数据迭代TestScripts

时间:2015-07-20 16:33:41

标签: java apache selenium apache-poi

我在selenium中编写了一个excel中包含一组数据的测试用例,但我需要使用多组数据运行。我试过循环我的测试用例但没有运气。专家请提供我用多个数据集循环我的测试用例的代码。以下是我的代码供您参考。 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);
    }

}

1 个答案:

答案 0 :(得分:1)

使用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
    }

}