Saturday, April 20, 2013

Print table data






Locating table rows and cells
 Sometimes we need to iterate through out table and do some operations with that.

Example :

  1. Verify for expected values in table
  2. Sort  table columns in ascending & descending order and verify.
In above both the cases we need to get the table data to verify. Lets see how to get the data from table.
Steps
  • get table instance
  • using above table instance , get all table rows
  • iterate each row and get all columns values.
Logic


@Test
public void testTable()
{
          WebElement table = driver.findElement(By.id("tableId"));
       
          //Get all rows (tr tags)
          List<WebElement> rows = table.findElements(By.tagName("tr"));

           //Print data from each row (Data from each td tag)
           for (WebElement row : rows) {
           List
<WebElement> cols = row.findElements(By.tagName("td"));
                  for (WebElement col : cols) {
                        System.out.print(col.getText() + "\t");
                  }
          System.out.println();
          }
}


Happy Coding :)


Regards,
SantoshSarma




No comments:

Post a Comment

Note: Only a member of this blog may post a comment.