Friday, 5 June 2020

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

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.!

Tip : How to fix --> DEBUG cache:45 - Couldn't find template in cache for "index.ftl"("en", UTF-8, parsed); will try to load it. Or TemplateLoader.findTemplateSource("index_en.ftl"): Not found

Hi,

When you receive the following DEBUG for Maven based cucumber java selenium project that is configured with extent reports, it can be fixed in log4j.properties by adding the following property.

Fix : in log4j.properties
log4j.logger.freemarker.cache = INFO, CONSOLE

Issue:
2020-06-05 09:13:30 DEBUG cache:45 - Couldn't find template in cache for "index.ftl"("en", UTF-8, parsed); will try to load it.
2020-06-05 09:13:30 DEBUG cache:45 - TemplateLoader.findTemplateSource("index_en.ftl"): Not found
2020-06-05 09:13:30 DEBUG cache:45 - TemplateLoader.findTemplateSource("index.ftl"): Found
2020-06-05 09:13:30 DEBUG cache:45 - Loading template for "index.ftl"("en", UTF-8, parsed) from "jar:file:/C:/Users/sadakarp/.m2/repository/E1SmapiUiAutomation/E1SmapiUiAutomation/0.0.1-SNAPSHOT/E1SmapiUiAutomation-0.0.1-SNAPSHOT-jar-with-dependencies.jar!/com/aventstack/extentreports/view/html-report/index.ftl"
2020-06-05 09:13:31 DEBUG cache:45 - Couldn't find template in cache for "head.ftl"("en", UTF-8, parsed); will try to load it.
2020-06-05 09:13:31 DEBUG cache:45 - TemplateLoader.findTemplateSource("head_en.ftl"): Not found
2020-06-05 09:13:31 DEBUG cache:45 - TemplateLoader.findTemplateSource("head.ftl"): Found
2020-06-05 09:13:31 DEBUG cache:45 - Loading template for "head.ftl"("en", UTF-8, parsed) from "jar:file:/C:/Users/sadakarp/.m2/repository/E1SmapiUiAutomation/E1SmapiUiAutomation/0.0.1-SNAPSHOT/E1SmapiUiAutomation-0.0.1-SNAPSHOT-jar-with-dependencies.jar!/com/aventstack/extentreports/view/html-report/head.ftl"
2020-06-05 09:13:31 DEBUG cache:45 - Couldn't find template in cache for "nav.ftl"("en", UTF-8, parsed); will try to load it.
2020-06-05 09:13:31 DEBUG cache:45 - TemplateLoader.findTemplateSource("nav_en.ftl"): Not found
2020-06-05 09:13:31 DEBUG cache:45 - TemplateLoader.findTemplateSource("nav.ftl"): Found
2020-06-05 09:13:31 DEBUG cache:45 - Loading template for "nav.ftl"("en", UTF-8, parsed) from "jar:file:/C:/Users/sadakarp/.m2/repository/E1SmapiUiAutomation/E1SmapiUiAutomation/0.0.1-SNAPSHOT/E1SmapiUiAutomation-0.0.1-SNAPSHOT-jar-with-dependencies.jar!/com/aventstack/extentreports/view/html-report/nav.ftl"
2020-06-05 09:13:31 DEBUG cache:45 - Couldn't find template in cache for "test-view/test-view.ftl"("en", UTF-8, parsed); will try to load it.
2020-06-05 09:13:31 DEBUG cache:45 - TemplateLoader.findTemplateSource("test-view/test-view_en.ftl"): Not found
2020-06-05 09:13:31 DEBUG cache:45 - TemplateLoader.findTemplateSource("test-view/test-view.ftl"): Found
2020-06-05 09:13:31 DEBUG cache:45 - Loading template for "test-view/test-view.ftl"("en", UTF-8, parsed) from "jar:file:/C:/Users/sadakarp/.m2/repository/E1SmapiUiAutomation/E1SmapiUiAutomation/0.0.1-SNAPSHOT/E1SmapiUiAutomation-0.0.1-SNAPSHOT-jar-with-dependencies.jar!/com/aventstack/extentreports/view/html-report/test-view/test-view.ftl"
2020-06-05 09:13:31 DEBUG cache:45 - Couldn't find template in cache for "test-view/test-view-charts.ftl"("en", UTF-8, parsed); will try to load it.
2020-06-05 09:13:31 DEBUG cache:45 - TemplateLoader.findTemplateSource("test-view/test-view-charts_en.ftl"): Not found
2020-06-05 09:13:31 DEBUG cache:45 - TemplateLoader.findTemplateSource("test-view/test-view-charts.ftl"): Found
2020-06-05 09:13:31 DEBUG cache:45 - Loading template for "test-view/test-view-charts.ftl"("en", UTF-8, parsed) from "jar:file:/C:/Users/sadakarp/.m2/repository/E1SmapiUiAutomation/E1SmapiUiAutomation/0.0.1-SNAPSHOT/E1SmapiUiAutomation-0.0.1-SNAPSHOT-jar-with-dependencies.jar!/com/aventstack/extentreports/view/html-report/test-view/test-view-charts.ftl"
2020-06-05 09:13:31 DEBUG cache:45 - Couldn't find template in cache for "test-view/bdd.ftl"("en", UTF-8, parsed); will try to load it.
2020-06-05 09:13:31 DEBUG cache:45 - TemplateLoader.findTemplateSource("test-view/bdd_en.ftl"): Not found
2020-06-05 09:13:31 DEBUG cache:45 - TemplateLoader.findTemplateSource("test-view/bdd.ftl"): Found
2020-06-05 09:13:31 DEBUG cache:45 - Loading template for "test-view/bdd.ftl"("en", UTF-8, parsed) from "jar:file:/C:/Users/sadakarp/.m2/repository/E1SmapiUiAutomation/E1SmapiUiAutomation/0.0.1-SNAPSHOT/E1SmapiUiAutomation-0.0.1-SNAPSHOT-jar-with-dependencies.jar!/com/aventstack/extentreports/view/html-report/test-view/bdd.ftl"
2020-06-05 09:13:31 DEBUG cache:45 - "test-view/bdd.ftl"("en", UTF-8, parsed) cached copy not yet stale; using cached.
2020-06-05 09:13:31 DEBUG cache:45 - "test-view/bdd.ftl"("en", UTF-8, parsed) cached copy not yet stale; using cached.
2020-06-05 09:13:31 DEBUG cache:45 - Couldn't find template in cache for "category-view/category-view.ftl"("en", UTF-8, parsed); will try to load it.
2020-06-05 09:13:31 DEBUG cache:45 - TemplateLoader.findTemplateSource("category-view/category-view_en.ftl"): Not found
2020-06-05 09:13:31 DEBUG cache:45 - TemplateLoader.findTemplateSource("category-view/category-view.ftl"): Found
2020-06-05 09:13:31 DEBUG cache:45 - Loading template for "category-view/category-view.ftl"("en", UTF-8, parsed) from "jar:file:/C:/Users/sadakarp/.m2/repository/E1SmapiUiAutomation/E1SmapiUiAutomation/0.0.1-SNAPSHOT/E1SmapiUiAutomation-0.0.1-SNAPSHOT-jar-with-dependencies.jar!/com/aventstack/extentreports/view/html-report/category-view/category-view.ftl"
2020-06-05 09:13:31 DEBUG cache:45 - Couldn't find template in cache for "author-view/author-view.ftl"("en", UTF-8, parsed); will try to load it.
2020-06-05 09:13:31 DEBUG cache:45 - TemplateLoader.findTemplateSource("author-view/author-view_en.ftl"): Not found
2020-06-05 09:13:31 DEBUG cache:45 - TemplateLoader.findTemplateSource("author-view/author-view.ftl"): Found
2020-06-05 09:13:31 DEBUG cache:45 - Loading template for "author-view/author-view.ftl"("en", UTF-8, parsed) from "jar:file:/C:/Users/sadakarp/.m2/repository/E1SmapiUiAutomation/E1SmapiUiAutomation/0.0.1-SNAPSHOT/E1SmapiUiAutomation-0.0.1-SNAPSHOT-jar-with-dependencies.jar!/com/aventstack/extentreports/view/html-report/author-view/author-view.ftl"
2020-06-05 09:13:31 DEBUG cache:45 - Couldn't find template in cache for "exception-view/exception-view.ftl"("en", UTF-8, parsed); will try to load it.
2020-06-05 09:13:31 DEBUG cache:45 - TemplateLoader.findTemplateSource("exception-view/exception-view_en.ftl"): Not found
2020-06-05 09:13:31 DEBUG cache:45 - TemplateLoader.findTemplateSource("exception-view/exception-view.ftl"): Found
2020-06-05 09:13:31 DEBUG cache:45 - Loading template for "exception-view/exception-view.ftl"("en", UTF-8, parsed) from "jar:file:/C:/Users/sadakarp/.m2/repository/E1SmapiUiAutomation/E1SmapiUiAutomation/0.0.1-SNAPSHOT/E1SmapiUiAutomation-0.0.1-SNAPSHOT-jar-with-dependencies.jar!/com/aventstack/extentreports/view/html-report/exception-view/exception-view.ftl"
2020-06-05 09:13:31 DEBUG cache:45 - Couldn't find template in cache for "dashboard-view/dashboard-view.ftl"("en", UTF-8, parsed); will try to load it.
2020-06-05 09:13:31 DEBUG cache:45 - TemplateLoader.findTemplateSource("dashboard-view/dashboard-view_en.ftl"): Not found
2020-06-05 09:13:31 DEBUG cache:45 - TemplateLoader.findTemplateSource("dashboard-view/dashboard-view.ftl"): Found
2020-06-05 09:13:31 DEBUG cache:45 - Loading template for "dashboard-view/dashboard-view.ftl"("en", UTF-8, parsed) from "jar:file:/C:/Users/sadakarp/.m2/repository/E1SmapiUiAutomation/E1SmapiUiAutomation/0.0.1-SNAPSHOT/E1SmapiUiAutomation-0.0.1-SNAPSHOT-jar-with-dependencies.jar!/com/aventstack/extentreports/view/html-report/dashboard-view/dashboard-view.ftl"
2020-06-05 09:13:31 DEBUG cache:45 - Couldn't find template in cache for "logs-view/testrunner-logs-view.ftl"("en", UTF-8, parsed); will try to load it.
2020-06-05 09:13:31 DEBUG cache:45 - TemplateLoader.findTemplateSource("logs-view/testrunner-logs-view_en.ftl"): Not found
2020-06-05 09:13:31 DEBUG cache:45 - TemplateLoader.findTemplateSource("logs-view/testrunner-logs-view.ftl"): Found
2020-06-05 09:13:31 DEBUG cache:45 - Loading template for "logs-view/testrunner-logs-view.ftl"("en", UTF-8, parsed) from "jar:file:/C:/Users/sadakarp/.m2/repository/E1SmapiUiAutomation/E1SmapiUiAutomation/0.0.1-SNAPSHOT/E1SmapiUiAutomation-0.0.1-SNAPSHOT-jar-with-dependencies.jar!/com/aventstack/extentreports/view/html-report/logs-view/testrunner-logs-view.ftl"