2016-06-09 77 views
0

我想在html表中測試一個數據,並用包含員工信息的數據庫驗證它。以下是代碼。現在我堅持創建斷言,如何收集表的表的行或列表值如何測試硒中的數據表

@Test

public void TestingReport() 
{ 

    driver.findElement(By.xpath(".//*[@id='content']/h2[2]/a")).click(); // click on admin 

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

    driver.findElement(By.xpath("//*[@id='content']/h2[2]/a")).click(); // click on staff member 

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

    //  ArrayList<ArrayList> ActualReport= new ArrayList<ArrayList>(); 

    List<WebElement> tablerow = driver.findElements(By.xpath("//table//tr")); 

    ArrayList <String> cellsStr = new ArrayList<>(); 

    ArrayList<List> table = new ArrayList<>(); 
    List<WebElement> cells= null ; 

    // System.out.println(tablerow.size()); 
    for(int i=1;i<tablerow.size();i++) { 


     cells = driver.findElements(By.xpath("//tr["+i+"]//td")); 


     // table.add(cells); 

     for(int j=4;j<cells.size();j++) 
     { 
      System.out.println(cells.get(j).getText()); 
      cellsStr.add(cells.get(j).getText()); 

     } 
     System.out.println(cellsStr); 

     table.add(cellsStr); 
     System.out.println(table); 


     System.out.println(cellsStr); 


    } 


    System.out.println(table); 



} 

在這裏,我想獲得刺痛的ArrayList到一個字符串,但在下一次迭代之前,我正在清除字符串的Arraylist,但它使數組列表的列表也爲空。

+0

要獲取單元格的值,請使用以下xpath定位器。 List cells = tablerow.get(i).findElements(By.xpath(「// table/tr [」+(i + 1)+「]/td」)); –

+0

嗨,我已經修改了上面的代碼,請再次回答這個問題 –

回答

0

下面有助於從值列表中獲取數據。

List<WebElement> tablerow = driver.findElements(By.xpath("//table//tr")); 
for(int i=1;i<tablerow.size();i++) 
{ 
List<WebElement> cells= tablerow.get(i).findElements(By.tagName("td")); 

for(int j=4;j<cells.size();j++){ 
List<WebElement> cellSubRow= cells.get(i).findElements(By.tagName("td")); 
System.out.println(cellSubRow.get(j).getText());}}