2015-03-19 88 views
0

我對Selenium非常陌生,我一直試圖讓測試套件從表中收集數據。我對如何做到這一點毫無頭緒。試圖從使用Selenium WebDriver for Java獲取HTML表中的數據

這裏是我與工作表: http://i.imgur.com/vdITVug.jpg

新的任命(日期)在一天中的隨機時間隨機增加。我創建了一個測試套件,將不斷刷新此頁面。下一步,將保存表中的所有日期,創建一個循環來比較刷新後的日期是否與原始存儲日期不同。

如果它們不同,請通知用戶。

下面是我試圖完成的理論示例。

//Navigate to the appointment page 

//Store all the current dates from the table 

    for (until a new appointment pops up) 

    { 
    //Refresh the page 
    // Compare the dates to the stored dates 
     if (the dates =/ stored dates) 
     { 
      notify the user(me in this case) 
     } 
    } 

我也想弄清楚如何找到表的元素ID。

下面是一些HTML代碼的截圖:http://i.imgur.com/GD4yOp9.png

突出顯示的聲明已經存儲了第一次約會。

任何意見將不勝感激,謝謝!

回答

1

嘗試複製一個類似的HTML結構(事實上其中2個,刷新後)。這是一個快速解決方案,可以讓您在刷新後比較HTML表格。

這裏的關鍵是將您的表格數據組織成類似於數據結構的Map<String, List<String>>

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 


public class CheckTables { 

public WebDriver driver; 


public static void main(String[] args) throws Exception { 


    CheckTables objTest = new CheckTables(); 
    objTest.runTest(); 

} 

public void runTest(){ 

    driver = new FirefoxDriver(); 

    driver.navigate().to("file:///D:/00_FX_WorkSpace/X_Hour/RoadTest_1.html"); 
    Map<String, List<String>> objTable_1 = readTable(); 
    System.out.println("TABLE:1" + objTable_1); 

    //event to refresh the table 
    driver.navigate().to("file:///D:/00_FX_WorkSpace/X_Hour/RoadTest_2.html"); 
    Map<String, List<String>> objTable_2 = readTable(); 
    System.out.println("TABLE:2" + objTable_2); 

    compareTables(objTable_1, objTable_2); 

} 

public Map<String, List<String>> readTable(){ 

    Map<String, List<String>> objTable = new HashMap<>(); 

    List<WebElement> objRows = driver.findElements(By.cssSelector("tr#data")); 
    for(int iCount=0; iCount<objRows.size(); iCount++){ 
     List<WebElement> objCol = objRows.get(iCount).findElements(By.cssSelector("td.tableTxt")); 
     List<String> columns = new ArrayList<>(); 
     for(int col=0; col<objCol.size(); col++){ 
      columns.add(objCol.get(col).getText()); 
     } 
     objTable.put(String.valueOf(iCount), columns); 
    } 

    return objTable; 
} 

public void compareTables(Map<String, List<String>> objTable1, Map<String, List<String>> objTable2){ 


    for(int count=0; count<objTable1.size(); count++){ 

     List<String> objList1 = objTable1.get(String.valueOf(count)); 
     System.out.println(objList1); 
     List<String> objList2 = objTable2.get(String.valueOf(count)); 
     System.out.println(objList2); 

     if(objList1.containsAll(objList2)){ 
      System.out.println("Row [" + count + "] is SAME"); 
     } 
     else{ 
      //notify 
      System.out.println("Row [" + count + "] has CHANGED"); 
     } 
    } 
} 
} 

這裏是RoadTest_1.html和RoadTest_2.html的HTML片段 - https://gist.github.com/anonymous/43c3b1f44817c69bd03d/

+0

嘗試了你的建議的代碼。獲取初始化錯誤:readTable()不應該是靜態的。 此外,在readTable()中,'driver'下有一個錯誤,說我無法對非靜態字段進行靜態引用。 – suppressive 2015-03-19 13:51:04

+0

我有一個類中的完整代碼並從main調用方法。你正在創建一個類的對象,然後調用方法?如果是,那麼這些方法不應該是靜態的。 – Dharam 2015-03-19 13:53:12

+0

編輯該片段以避免靜態對象。 – Dharam 2015-03-19 13:58:35

相關問題