|
| 1 | +--- |
| 2 | +description: "Test infinite scroll in Selenium: scroll to the bottom, then assert the new items appear with a retrying count check instead of Thread.sleep." |
| 3 | +id: scroll-to-load-more |
| 4 | +title: Scroll to load more |
| 5 | +sidebar_label: Scroll to load more |
| 6 | +--- |
| 7 | + |
| 8 | +# Scroll to load more |
| 9 | + |
| 10 | +Infinite-scroll pages fetch the next batch only after you reach the bottom, and the new items arrive a moment later. A `Thread.sleep` is either too short (flaky) or too long (slow). Scroll with `BasePage`, then assert on the item count. `assertThat(...).count(n)` retries until it matches or `timeouts.explicit` runs out. |
| 11 | + |
| 12 | +```java title="FeedPage.java" |
| 13 | +import com.seleniumboot.test.BasePage; |
| 14 | +import org.openqa.selenium.By; |
| 15 | +import org.openqa.selenium.WebDriver; |
| 16 | + |
| 17 | +import static com.seleniumboot.assertion.SeleniumAssert.assertThat; |
| 18 | + |
| 19 | +public class FeedPage extends BasePage { |
| 20 | + |
| 21 | + private static final By ITEMS = By.cssSelector("#feed .item"); |
| 22 | + |
| 23 | + public FeedPage(WebDriver driver) { |
| 24 | + super(driver); |
| 25 | + } |
| 26 | + |
| 27 | + public int itemCount() { |
| 28 | + return driver.findElements(ITEMS).size(); |
| 29 | + } |
| 30 | + |
| 31 | + public void loadMore(int batchSize) { |
| 32 | + int before = itemCount(); |
| 33 | + scrollToBottom(); |
| 34 | + assertThat(ITEMS).count(before + batchSize); // retries until the batch lands |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +```java |
| 40 | +feed.loadMore(10); // 10 -> 20 items |
| 41 | +feed.loadMore(10); // 20 -> 30 items |
| 42 | +``` |
| 43 | + |
| 44 | +Record the count **before** you scroll, so the assertion is relative to what was there. That keeps it correct no matter how many times you call it. |
| 45 | + |
| 46 | +If the loader is triggered by a specific element (a "sentinel" or the last row) rather than the page bottom, use `scrollTo(By)` on that element instead of `scrollToBottom()`. It scrolls the element into view. |
| 47 | + |
| 48 | +**Deeper reference:** [BasePage](/docs/guides/base-page): the page-object base class with the scroll helpers. |
0 commit comments