2015-07-20 28 views
0

我已經用excel中的一組數據編寫了selenium中的testcase,但是我需要運行多組數據。我嘗試過循環測試,但沒有運氣。專家請給我提供用於循環測試用多組數據的代碼。以下是我的代碼供您參考。 ExcelUtil文件代碼在使用POI的Excel中迭代TestScript與Excel中的多個數據HSSF

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

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

} 
+0

許多感謝普魯斯,其預計工作。 – Kiran

+0

很高興知道。儘可能接受並上傳答案。 – Purus

+0

@Purus上傳? :) – alecxe