2015-08-13 110 views
0

我創建了我的腳本來驗證我的實際結果和預期結果。從Excel中獲取元素

由於有太多的鏈接到驗證的腳本會得到太多的編碼m,所以我需要將其轉換爲數據驅動的情況。

Webdriver將從excel中獲取URL,xpath,期望值。

但是不知道如何繼續。

的演示代碼是非常讚賞

這裏是我當前的腳本:

public void test() throws Exception 
{ 
    String home_logo_url="158321"; 
    String enough_talk_promo="1057406"; 

    System.out.println("Check for home_logo_url"); 
    driver.get(baseUrl); 
    String SiteWindow = driver.getWindowHandle(); // get the current window handle 
    driver.findElement(By.xpath("//*[@id='logo']/a")).click(); 
    for (String PromoWindow : driver.getWindowHandles()) 
    { 
     driver.switchTo().window(PromoWindow); // switch focus of  WebDriver to the next found window handle (that's your newly opened window) 
    } 
     String script = "return rlSerial;"; 
     String value = (String) ((JavascriptExecutor)driver).executeScript(script); 
     Assert.assertEquals(value,home_logo_url); 
     driver.close(); 
     driver.switchTo().window(SiteWindow); 
     System.out.println("Pass"); 

     System.out.println("Check for enough_talk_promo"); 
     driver.get(baseUrl + "/category/tournaments/"); 
     driver.findElement(By.xpath("//*[@id='content']/div/div[4]/aside/div/div/p[1]/a")).click(); 
     for (String PromoWindow : driver.getWindowHandles()) 
     { 
      driver.switchTo().window(PromoWindow); // switch focus of WebDriver to the next found window handle (that's your newly opened window) 
     } 
     String sr_enough_talk_promo = (String) ((JavascriptExecutor)driver).executeScript(script); 
     Assert.assertEquals(sr_enough_talk_promo,enough_talk_promo); 
     driver.close(); 
     driver.switchTo().window(SiteWindow); 
     System.out.println("Pass"); 


    } 

Here is field i had created

如何迭代到各行,讓我運行測試用例!

如果有人可以將我現有的代碼轉換爲在Excel工作表上工作,這將非常有幫助。

感謝

+0

您是否嘗試閱讀關於遍歷行和單元格的Apache POI文檔(http://poi.apache.org/spreadsheet/quick-guide.html#Iterator)? – Gagravarr

+0

是的,我做了,但沒有得到如何進行,你能請把我現有的腳本轉換爲從excel中讀取所有元素,並做必要的斷言 –

+0

你似乎誤以爲這個網站的某個地方爲你做你的工作...... :( – Gagravarr

回答

0

我工作在春天的一個項目,從Excel(.xls的)讀取,這是我的代碼,如果可以幫助

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 
} 
+0

謝謝。我已經創建了代碼,但沒有得到如何點擊元素指定在Excel表 –

+0

我更新代碼,如果可以幫助,第一個代碼是我如何從Excel中讀取,第二個是我如何將該值插入到我的empoye DB – yb3prod

+0

中的表格我曾嘗試過,但無法根據我的要求修改您的代碼,..可以請您爲我做出讓我可以參考並繼續前進 –