Handle Pagination and Infinite Scroll in Phoma Flows with Loops and Conditions
Why Pagination and Infinite Scroll Break Simple Flows
Most Phoma tutorials show a single tap or a short swipe. Real‑world apps, however, present data in long lists that load incrementally — either by a “Load more” button (pagination) or by automatically fetching new items when the user reaches the bottom (infinite scroll). A flow that only swipes once will miss the majority of the data, and a flow that repeats a fixed number of swipes will either stop too early or keep swiping after the list ends, causing errors or infinite loops.
The root cause is that the UI tree changes after each load: new ViewHolder objects appear, old ones may be recycled, and the scroll container’s bounds shift. To automate reliably you need a flow that observes the UI after each interaction, decides whether more content exists, and stops only when a genuine end‑of‑list signal appears.
Core Concepts: Loops, Conditions, and Wait Steps in Phoma
Phoma’s visual builder provides three primitives that map directly to this problem:
| Primitive | Purpose | Typical Setting for Lists |
|---|---|---|
| Loop | Repeat a block while a condition holds | While element "listContainer" exists |
| Condition | Branch based on a UI state | If element "noMoreItems" is visible → Break |
| Wait for Element | Pause until a selector matches (with timeout) | Wait for new item selector, timeout 5 s |
All three operate on element selectors you define once (see the selector section below). Because the flow runs on your Windows machine, the loop runs locally — no cloud latency — and you can watch the mirrored screen to verify each iteration.
Step‑by‑Step: Building a Flow That Scrolls a RecyclerView Until the Last Item
Prerequisites
- Windows PC with Phoma installed (free tier is enough).
- Android device connected via USB (USB debugging on) or Wi‑Fi ADB.
- Target app installed and the screen you want to scrape reachable from the home screen.
1. Connect the Device and Open the App
- In Phoma, click Add Device → select your phone → Connect.
- Add a Launch App step: package
com.example.app, activityMainActivity. - Add a Wait for Element step for the list container so the flow doesn’t start scrolling before the UI is ready.
{
"type": "waitForElement",
"selector": {
"strategy": "uiautomator",
"value": "new UiSelector().resourceId(\"com.example.app:id/recyclerView\")"
},
"timeoutMs": 8000
}
2. Identify the List Container and a Representative Item Selector
Open the Element Inspector (mirrored screen → click the inspector icon). Click the RecyclerView → copy its selector (resource‑id is ideal). Then click a single list item → copy its selector. You’ll use the container for the loop condition and the item for data extraction.
Container selector (stable across updates):
new UiSelector().resourceId("com.example.app:id/recyclerView")
Item selector (relative to container, used each iteration):
new UiSelector().resourceId("com.example.app:id/itemTitle")
3. Add a Loop Step That Continues While the Container Exists
Drag a Loop block onto the canvas. Set its condition to Element Exists → paste the container selector. Phoma will re‑evaluate this selector at the start of every iteration.
4. Inside the Loop: Swipe, Wait, Extract
- Swipe Up – choose the container element as the swipe target, direction Up, distance 80 % of screen height, duration 300 ms.
- Wait for New Items – add a Wait for Element step using the item selector with a short timeout (3–5 s). This ensures the flow pauses until the next batch renders.
- Extract Data – add a Read Text step (or Get Attribute) on the item selector. Enable “All matches” so Phoma returns an array of titles for the current viewport.
- Store Results – use a Set Variable step (name
collectedTitles) with expression{{collectedTitles}} + {{lastReadText}}to accumulate across iterations.
5. Add a Break Condition for the End of List
Most apps show a footer like “No more items” or hide the scrollbar when the list ends. Inspect that element and add a Condition step inside the loop (after the wait):
{
"type": "condition",
"if": {
"strategy": "uiautomator",
"value": "new UiSelector().text(\"No more items\")"
},
"then": [ { "type": "breakLoop" } ],
"else": []
}
If your app doesn’t show an explicit message, you can break when the scroll position stops changing. Phoma exposes a Get Scroll Position action on the container; compare the value from the previous iteration (store in a variable) and break when they’re equal for two consecutive loops.
6. Finish the Flow
After the loop ends, add a Log or Export Variable step to write collectedTitles to a CSV file on your PC (Phoma’s Write File action). Optionally, close the app.
Handling Infinite Scroll with a “Load More” Button
Some screens use a button instead of auto‑load. The pattern is identical, only the interaction changes:
- After the Wait for Element step, add a Condition that checks for the button selector (
resourceId="loadMoreBtn"). - If present → Click the button → Wait for Element (new items) → continue loop.
- If absent → Break (end of data).
This keeps the flow deterministic even when the button appears only after a certain scroll offset.
Dealing with Dynamic Content and Stale Elements
Because Android recycles ViewHolders, an element reference captured in iteration 1 may become stale in iteration 2. Phoma solves this by re‑querying selectors on every step. Ensure you never store a raw element handle in a variable; always store selector strings and let Phoma resolve them at runtime. If you need to act on a specific item (e.g., click the 5th title), compute the index inside the loop and use a Child Selector:
new UiSelector().resourceId("com.example.app:id/recyclerView")
.childSelector(new UiSelector().resourceId("com.example.app:id/itemTitle").instance(4))
Best Practices for Stable Selectors Across App Updates
- Prefer resource‑id over
classNameorindex. - When an id is missing, use content‑desc (accessibility label) – it’s rarely changed.
- Avoid absolute XPath‑like paths (
/android.widget.FrameLayout/...). - Test selectors in the Element Inspector after each app release; Phoma highlights matches in real time.
Troubleshooting Common Issues
| Symptom | Likely Cause | Fix |
|---|---|---|
| Loop never exits | Break condition selector never matches | Verify the “no more” text exactly (case‑sensitive) or add a scroll‑position comparison fallback. |
| Swipe does not move list | Swipe target is a child view that doesn’t scroll | Set swipe target to the RecyclerView container, not an item. |
| “Element not found” after a few iterations | Selector uses instance(n) that shifts as items recycle |
Switch to a relative selector (e.g., childSelector without fixed instance) or use textContains for the title. |
| Flow runs very slowly | Timeout on Wait for Element too high | Reduce timeout to 3 s; the list usually renders within 1 s on modern devices. |
Extending the Pattern
- Horizontal carousels – change swipe direction to Left and use a horizontal
RecyclerViewselector. - Grid layouts – swipe up still works; just ensure the container selector matches the
GridLayoutManagerview. - Nested lists (e.g., expandable sections) – add an outer loop over section headers, inner loop over each section’s items.
Summary Checklist
- Capture stable container and item selectors via Element Inspector.
- Build a Loop → Swipe → Wait → Extract → Condition → Break block.
- Accumulate data in a variable; export at the end.
- Test on at least two device models (different screen densities).
- Add the flow to your team’s shared library (Team/Business plan) for reuse.
With this structure you can automate any paginated or infinite‑scroll screen — product catalogs, chat histories, log viewers — without writing a single line of code. The flow lives entirely on your Windows machine, runs on your own phones, and can be scheduled or triggered manually whenever you need fresh data.
