Skip to content

Commit 599b207

Browse files
shossain786claude
andauthored
docs(recipe): scroll to load more (infinite scroll) (#214)
Closes #124 Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent b1e7f06 commit 599b207

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.

‎docs-site/sidebars.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const sidebars = {
6464
'recipes/handle-iframes',
6565
'recipes/handle-new-windows',
6666
'recipes/handle-javascript-alerts',
67+
'recipes/scroll-to-load-more',
6768
'recipes/drag-and-drop',
6869
'recipes/handle-shadow-dom',
6970
'recipes/read-table-rows',

0 commit comments

Comments
 (0)