DEV Community

Styrow.dev
Styrow.dev

Posted on Originally published at styrow.dev

How do you robustly validate all dynamically loaded content on a page utilizing infinite scroll with Selenium and Java?

⚠️ Infinite Scroll Test Automation Nightmare?
90% of engineers struggle with dynamic content validation in Selenium-Java. Here's how to master it.


📌 Problem Statement

Infinite scroll pages dynamically load content, making robust UI test automation a significant challenge. Testers often encounter:

  • ❌ Flaky tests due to content not yet loaded.
  • ❌ Incomplete validation because not all elements were present.
  • ❌ Fixed waits (e.g., Thread.sleep) are unreliable and inefficient.

The goal is to reliably detect and validate all content on a page utilizing infinite scroll, ensuring tests are stable and comprehensive.

💡 Solution & Code Walkthrough

A robust strategy involves iterative scrolling, dynamic waiting for content to appear, and intelligent termination. We'll use JavascriptExecutor to scroll and WebDriverWait for smart waiting.

  • • Iterative Scrolling: Repeatedly scroll to the page bottom.
  • • Dynamic Waiting: After each scroll, wait for new elements to appear.
  • • Unique Collection: Store all discovered elements in a Set to handle duplicates.
  • • Smart Termination: Stop scrolling when no new unique elements appear after a wait period.
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import java.util.HashSet;
import java.util.Set;

public static Set<WebElement> collectDynamicScrollContent(WebDriver driver, By itemLocator) {
    Set<WebElement> allUniqueItems = new HashSet<>();
    JavascriptExecutor js = (JavascriptExecutor) driver;
    int previousTotalCount = 0; // Total unique items collected

    while (true) {
        js.executeScript("window.scrollTo(0, document.body.scrollHeight);"); // Scroll

        try { // Wait for new items to appear (up to 5s)
            new WebDriverWait(driver, Duration.ofSeconds(5))
                .until(d -> d.findElements(itemLocator).size() > previousTotalCount);
        } catch (org.openqa.selenium.TimeoutException e) {
            // No new items within wait, likely end of content
        }

        Set<WebElement> currentItemsOnPage = new HashSet<>(driver.findElements(itemLocator));

        if (currentItemsOnPage.size() == previousTotalCount) { // No new unique items
            break; // All content loaded
        }

        allUniqueItems.addAll(currentItemsOnPage); // Add new unique elements
        previousTotalCount = allUniqueItems.size(); // Update count
    }
    return allUniqueItems;
}
Enter fullscreen mode Exit fullscreen mode

🔑 Key Takeaways

  • • Iterative Scrolling: Use JavascriptExecutor to scroll window.scrollTo(0, document.body.scrollHeight);.
  • • Dynamic Waits: Leverage WebDriverWait with ExpectedConditions (or a custom lambda) to detect new elements or a change in element count.
  • • Unique Element Collection: Utilize a HashSet<WebElement> to efficiently store and ensure uniqueness of all collected elements across multiple scrolls.
  • • Robust Termination: The loop breaks when, after scrolling and waiting, no new unique elements are found, indicating the end of dynamically loaded content.

❓ Quick Summary Q&A

  • Q: Why avoid Thread.sleep()?
    • A: It's an unreliable fixed wait; content load times vary, leading to flakiness or slow tests.
  • Q: How do you know when all content is loaded?
    • A: By comparing the number of unique elements found before and after a scroll and wait. If the count doesn't increase, loading has likely stopped.
  • Q: What if the page has a "Load More" button instead?
    • A: The strategy would adapt to click the button repeatedly and wait for new content until the button disappears or becomes disabled.

TAGS: selenium, java, automation, ui-testing, infinite-scroll, dynamic-content
────────────────────────────────────────
Ready to level up your automation skills? Download our app for more advanced tutorials!
────────────────────────────────────────

📲 𝐅𝐑𝐄𝐄 𝐌𝐎𝐁𝐈𝐋𝐄 𝐀𝐏𝐏 — 𝟔𝟎𝟎+ 𝐒𝐃𝐄𝐓 𝐐&𝐀𝐬
Practice real-world interview scenarios offline on the free QA Automation & SDET Prep app:

🤖 𝐆𝐨𝐨𝐠𝐥𝐞 𝐏𝐥𝐚𝐲 (𝐀𝐧𝐝𝐫𝐨𝐢𝐝):
https://play.google.com/store/apps/details?id=com.app.seleniuminterviewquestions&referrer=utm_source%3Ddevto%26utm_medium%3Darticle%26utm_campaign%3Dselenium_20261111

🍎 𝐀𝐩𝐩 𝐒𝐭𝐨𝐫𝐞 (𝐢𝐎𝐒):
https://apps.apple.com/app/id6786760948?pt=128640464&ct=devto_selenium_20261111&mt=8

────────────────────────────────────────
────────────────────────────────────────

Top comments (0)