In java-selenium, how to navigate through web data table using pagination buttons and verify the displayed row count on page is matching with actual table row count

Sadakar Pochampalli
JasperSoft BI Suite Tutorials - Sadakar Pochampalli )

Hi,


In this blog, we'll discuss below real time use cases of data tables automation in selenium.

1) How to navigate through a data table buttons till the last page ?
2) How to calculate the actual row count of data table ?
3) Compare the actual row count to the displayed row count.


Below is the flow..
1)  Declare and initiate the web driver.
2)  Navigate to the data table on web page.
3)  Create a list and store the "names" column data values for the first page (i.e., for first 10 values)
4)  Logic : Traverse through the table until Next button get disabled (use while loop).
5)  Logic : add the elements during the looping to the list (use for loop to add the list created in #3)
6) Calculate the size of the list i.e., actual count of data table.
7) Extract the displayed count i..e, displayed count of data table on web page.
8) Compare the actual count with displayed count.

Take a look at the lines highlighted in light green color in the following code that has the above flow.

Please watch this space for video tutorial to walk through the code



DataTableButtonsNavigationActualVsDisplayedRowCount.java
package selenium.datatables;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

@SuppressWarnings("unused")
public class DataTableButtonsNavigationActualVsDisplayedRowCount {

public static WebDriver driver;

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

System.setProperty("webdriver.chrome.driver", "D:\\006_trainings\\chromedriver_83.exe");
System
.setProperty("webdriver.chrome.silentOutput", "true");

driver
= new ChromeDriver();
driver.get("https://datatables.net/examples/basic_init/zero_configuration.html");
driver
.manage().window().maximize();
compareDispalyedRowCountToActualRowCount
();
}

public static void compareDispalyedRowCountToActualRowCount() throws Exception {

try {
Thread
.sleep(5000);
List<WebElement> namesElements = driver.findElements(By.cssSelector("#example>tbody>tr>td:nth-child(1)"));
System
.out.println("size of names elements : " + namesElements.size());

List<String> names = new ArrayList<String>();
//Adding column1 elements to the list
for (WebElement nameEle : namesElements) {
names.add(nameEle.getText());
}
//Displaying the list elements on console
for (WebElement s : namesElements) {
System
.out.println(s.getText());
}

//locating next button
String nextButtonClass = driver.findElement(By.id("example_next")).getAttribute("class");

//traversing through the table until the last button and adding names to the list defined about
while (!nextButtonClass.contains("disabled")) {
driver.findElement(By.id("example_next")).click();
Thread.sleep(1000);
namesElements = driver.findElements(By.cssSelector("#example>tbody>tr>td:nth-child(1)"));
for (WebElement nameEle : namesElements) {
names.add(nameEle.getText());
}
nextButtonClass = driver.findElement(By.id("example_next")).getAttribute("class");
}
//printing the whole list elements
for (String name : names) {
System
.out.println(name);
}
//counting the size of the list
int actualCount = names.size();
System
.out.println("Total number of names :" + actualCount);

//locating displayed count
String displayedCountString = driver.findElement(By.id("example_info")).getText().split(" ")[5];
int displayedCount = Integer.parseInt(displayedCountString);

System
.out.println("Total Number of Displayed Names count:" + displayedCount);

Thread
.sleep(1000);

// Actual count calculated Vs Dispalyed Count
if (actualCount == displayedCount) {
System
.out.println("Actual row count = Displayed row Count");
} else {
System
.out.println("Actual row count != Displayed row Count");
throw new Exception("Actual row count != Displayed row Count");
}
} catch (Exception e) {
e
.printStackTrace();
}
}
}

References: 
1) https://www.youtube.com/watch?v=jD5eWhNsaRk

Hope you find this is useful, stay tuned for more automation.!

Feedback