Test Diary

Archives


Social Profile


Recent Comments


My fourth Selenium WebDriver Test (Tables)

Olufemi Ade-OlusileOlufemi Ade-Olusile

Menu Next Topic

In this post, we will be using Selenium to loop through a table, by using the iterator design pattern to iterate over a collection of objects.

Please ensure that you have covered my previous tutorial on Selenium IDE Quick Learning, which will give you a clear and easy introduction on how to locate complex Web Elements with Xpath.

For this tutorial we will be using our sample test page.

Tables

On our test page we have a table with some information. To get the locator of this table we need to right click on it and inspect element. This will enable you view the html code for that element as displayed in the below.

We have an id for the table which is “testtable”. But to be able to loop through this table, we will need more than just the id. We will need an Xpath to provide us with the table body (tbody) “//table[@id=’testtable’]/tbody”. “The easiest way to get Xpaths is to follow my training on Selenium IDE Quick Learning.”  

<tbody>
<tr>
<td>James</td>
<td>Jones</td>
<td>e132ds</td>
</tr>
<tr>
<td>John</td>
<td>Houston</td>
<td>g172ds</td>
</tr>
</tbody>

Notice that this Xpath (“//table[@id=’testtable’]/tbody”) has the tbody, which means that we can locate every element in the table located in the tbody.  For example John, James Jones, e132ds e.tc.

Now lets look at the following test script below, to iterate over a table, get the size of its rows and columns and check if a column contains a particular text.

Fourth Test (Tables) 
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Testdiary_Tables {

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

WebDriver driver = new FirefoxDriver();

driver.manage().window().maximize();

String contactUrl = "http://www.testdiary.com/training/selenium/selenium-test-page/";

driver.get(contactUrl);

(new WebDriverWait(driver, 10))
.until(ExpectedConditions.visibilityOfElementLocated(By.id("Shirts")));

// Declaring local variables of type "int" to
// initialize the value of size. We use this
// because we want to store the size of the table rows and columns. It
// has nothing to do with the iteration over the table.
int colSize = 0;
int rowSize = 0;

// Declaring a local variable of type "WebElement" to initialize the
// value of the webElement (table body)
WebElement tableBody = driver.findElement(By
.xpath("//table[@id='testtable']/tbody"));

// Declaring a local variable of type "List<WebElement>" initialize the
// value of all webElements associated with
// the tag name "tr" (Table row). Therefore all table rows in the table
// body will be stored in a list called "tableRow"
List<WebElement> tableRow = tableBody.findElements(By.tagName("tr"));

// .size() used to return the number of elements in a list.
rowSize = tableRow.size();
System.out.println("The size of my table rows is: " + rowSize);

// Using the for each loop to iterate over each table row.
for (WebElement row : tableRow) {

// Declaring a local variable of type "List<WebElement>" to
// initialize the value of all webElements associated with
// the tag name "td" (Table cell). Therefore all table columns in
// each table row will be stored in a list called "col"
List<WebElement> col = row.findElements(By.tagName("td"));

// size() used to return the number of elements in a list.
colSize = col.size();

// get() used to get a particular column. Our table has 3 columns
// starting from
// 0 - 2. We are saying at every first column check if the text is
// equal to "James" using by
// the getText()
if (col.get(0).getText().equals("James")) {
System.out.println("test passed");
}
}
System.out.println("The size of my table column is: " + colSize);

driver.close();

System.exit(0);

}

}

In conclusion, we have looked at iterating over tables with Selenium using the “for each” loop, and how it can be used effectively in our test script.You can download or clone this test from the Testdiary Github.

Click: Tables Happy Testing!!!

Menu Next Topic

“My name is Olufemi Ade-Olusile, and I am a Software Developer in Test. I am the owner of "Test diary" which is aimed at inspiring and teaching software testers.