Friday 29 May 2020

How to compare displayed row count on web page is equals to data table calculated row count in java-selenium ? | Calculate web data table row count in java-selenium

Hi , In this tutorial, we'll learn about how to compare displayed row count on page is equals to data table row count in java selenium.

There are several ways to accomplish this, by the time I write this article, I've come across two ways.
They are
1) Calculate the no. of rows in data table by navigating through the pagination buttons.
2) Calculate the no. of rows in data table by using jQuery "length" function.

We will see the latter implementation in this post.

Tap on to the image to get better visibility of content : 

The approach is as follows.
1. Wait for sometime to load the page for which data table is present.
2. Take an integer variable say "dataTableActualRowCount" with 0 as the default value.
3. Create  JavascriptExecutor object for the driver.
4. Use jQuery "length" function technique within java-selenium code.
5. jQuery stores the value in Object so convert it into integer and store the jquery returned length in
     dataTableActualRowCount
6. Find the displayed count from bottom of the page using WebElement finder technique and store it
    in another integer variable say "displayedCountOfRowsOnPage".
7. Now, Compare the values of "dataTableActualRowCount" and
      "displayedCountOfRowsOnPage".
8. If dataTableActualRowCount == displayedCountOfRowsOnPage then "displayed row count
   on page is equals to data table row count " else "displayed row count on page is NOT equals
   to data  table row count".

dataTableActualRowCount - calculating using jQuery "length" function
int dataTableActualRowCount=0;
  
JavascriptExecutor js=(JavascriptExecutor)driver;
  
dataTableActualRowCount =  ((Number)js.executeScript("return $('#example').DataTable().rows().data().toArray().length;")).intValue();

System.out.println("Data table row count="+dataTableActualRowCount);

displayedCountOfRowsOnPage - finding through WebElement
String displayedCount = driver.findElement(By.id("example_info")).getText().split(" ")[5];
  
int displayedCountOfRowsOnPage = Integer.parseInt(displayedCount);

System.out.println("Data table display count on page ="+displayedCountOfRowsOnPage);

CompareDisplayedRowCountToDataTableRowCount.java
package selenium.datatables;


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

public class CompareDisplayedRowCountToDataTableRowCount {

 public static WebDriver driver;
 public static void main(String[] args) throws Exception {
  
   System.setProperty("webdriver.chrome.driver", "D:\\006_trainings\\chromedriver.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 {
  Thread.sleep(10000);
  
  int dataTableActualRowCount=0;
  
  JavascriptExecutor js=(JavascriptExecutor)driver;
  
  dataTableActualRowCount =  ((Number)js.executeScript("return $('#example').DataTable().rows().data().toArray().length;")).intValue();
  System.out.println("Data table row count="+dataTableActualRowCount);
  
  String displayedCount = driver.findElement(By.id("example_info")).getText().split(" ")[5];
  
  int displayedCountOfRowsOnPage = Integer.parseInt(displayedCount);
  System.out.println("Data table display count on page ="+displayedCountOfRowsOnPage);
  
  if(Integer.compare(dataTableActualRowCount, displayedCountOfRowsOnPage)==0) {
   System.out.println("Displayed count on page is equals to the data table row count ");
  }else {
   System.out.println("Displayed count on page is NOT equals to the data table row count");
   throw new Exception("Displayed count on page is NOT equals to the data table row count");
  }
  }
}


I hope you find this tutorial is useful, stay tuned for more automation.!

- Sadakar Pochampalli

No comments:

Post a Comment