In Espresso, when do you use onData instead of onView, and how do you click an off-screen RecyclerView row with RecyclerViewActions?
answer
- rows that do not exist yet
- match the data, not the view
- RecyclerView is not an AdapterView
- act through the list itself
- espresso-contrib
basics
~20 sUse onData for AdapterView lists such as ListView, GridView and Spinner: it matches the adapter's data object and loads that row before acting. RecyclerView is not an AdapterView, so you call onView on the RecyclerView and perform a RecyclerViewActions action from espresso-contrib.
solid answer
~40 s`onView` only searches views that exist, and a long list creates rows only for what is on screen. For an `AdapterView` (`ListView`, `GridView`, `Spinner`), `onData(matcher)` matches the **data object** the adapter returns, scrolls that item into the hierarchy and then acts; `inAdapterView`, `atPosition` and `onChildView` narrow it further. `RecyclerView` is not an `AdapterView`, so `onData` cannot reach it. Instead you match the `RecyclerView` itself and pass an action from `RecyclerViewActions` in `espresso-contrib`: `actionOnItemAtPosition(12, click())`, or `actionOnItem<VH>(hasDescendant(withText("Invoice #1042")), click())`, which binds adapter data to a view holder per item type to find the row, scrolls to it and clicks. If the matcher hits more than one item it throws, and `atPosition(n)` picks one.
code
kotlin · 12 lines// Spinner popup is a ListView, an AdapterView: match the data, not the row
onView(withId(R.id.country_spinner)).perform(click())
onData(allOf(`is`(instanceOf(String::class.java)), `is`("Portugal"))).perform(click())
onView(withId(R.id.country_spinner)).check(matches(withSpinnerText("Portugal")))
// RecyclerView: perform the action on the list itself
onView(withId(R.id.invoice_list)).perform(
RecyclerViewActions.actionOnItem<RecyclerView.ViewHolder>(
hasDescendant(withText("Invoice #1042")),
click()
)
)go deeper
Recall that long lists only create visible rows, that onData is for ListView, GridView and Spinner, and that RecyclerView uses RecyclerViewActions.
Explain that onData matches adapter data through getItem, how inAdapterView, atPosition and onChildView refine it, and how RecyclerViewActions finds an off-screen row by binding view holders.
Fix ambiguous row matches with atPosition or tighter descendant matchers, and know that RecyclerViewActions only acts, so assertions need a scroll followed by a unique onView.
Push for list rows that carry stable, distinct text or content descriptions so list tests stay short and do not rely on positions that shift as data changes.
## Why onView is not enough for long lists `onView` searches the **current view hierarchy**. A list widget does not put every row in that hierarchy: it creates views only for the items on screen, plus a few around them, and reuses them as you scroll. So in a list of 500 items, `onView(withText("Item 300"))` usually fails with `NoMatchingViewException`, not because the item is missing but because its row has not been created. When the hierarchy contains an `AdapterView`, Espresso's error message even adds a warning listing those adapter views and suggests `onData`. ## onData for AdapterView lists An `AdapterView` is the older family of list widgets backed by an `Adapter`: `ListView`, `GridView` and `Spinner`. For these, Espresso's second entry point, `onData`, works on the **data** rather than on views: 1. You pass a matcher for the data object the adapter returns from `getItem()`, for example `allOf(instanceOf(String::class.java), equalTo("Portugal"))`, or a matcher over a `Map` or a custom model class. 2. Espresso finds that item in the adapter, asks the `AdapterView` to bring it into the hierarchy, and only then runs the action or assertion on its row view. `onData` returns a `DataInteraction` with refinements: - `inAdapterView(withId(R.id.countries))` when the screen holds more than one adapter view; - `atPosition(n)` when the data matcher matches several items; - `onChildView(withId(R.id.delete))` to act on a view inside the row instead of the row itself. The Spinner case is the one most people meet: click the spinner with `onView`, then pick an entry from its popup list with `onData`, because that popup is a `ListView`. Android's basics guide also notes that items already on screen could be reached with `onView`, but that `onData` is the safer habit for adapter views. Custom `AdapterView` subclasses that break the `getItem()` contract may need a custom `AdapterViewProtocol`. ## RecyclerView needs RecyclerViewActions `RecyclerView` is not an `AdapterView`, and Android's lists guide states plainly that `onData()` cannot be used with it. The tool for it is `RecyclerViewActions`, in the `espresso-contrib` artifact. Each member is a `ViewAction` that you perform **on the `RecyclerView` itself**: | Action | What it does | |---|---| | `scrollToPosition(n)` | Scrolls the list to adapter position `n` | | `actionOnItemAtPosition(n, action)` | Scrolls to position `n`, then performs `action` on that row | | `scrollTo(itemViewMatcher)` | Scrolls to the row whose item view matches | | `actionOnItem(itemViewMatcher, action)` | Scrolls to the matching row, then performs `action` on it | | `scrollToHolder(viewHolderMatcher)` / `actionOnHolderItem(...)` | The same, matching on the `ViewHolder` | | `scrollToLastPosition()` | Scrolls to the last item; added in Espresso 3.5.0 | The matcher-based actions work even for rows far off screen because of how they search. For each item type, the action creates one `ViewHolder` and binds adapter data into it position by position, testing your matcher against the bound view. The matching position tells it where to go; it then scrolls there and acts on the real row. Two details trip people up: - **Exactly one match by default.** If the item matcher matches more than one item, the action throws a `PerformException`. The returned `PositionableRecyclerViewAction` has `atPosition(n)`, a zero-based index into the matching items, to choose one. - **Kotlin needs the type argument.** The methods are generic in the view-holder type, so Kotlin calls are written `RecyclerViewActions.actionOnItem<RecyclerView.ViewHolder>(...)`. ## Checking a row after scrolling `RecyclerViewActions` only performs actions. To assert on a row's content, scroll it into view with `scrollTo` or `scrollToPosition`, then use an ordinary `onView` with a matcher that is unique once the row exists, for example `onView(withText("Invoice #1042")).check(matches(isDisplayed()))`. Avoid matching row children by a shared id alone, because every visible row has that id and the result is an `AmbiguousViewMatcherException`. ## Why this matters on CI A test that uses `onView` for a row that happens to be on screen on a developer's large device can fail on a smaller emulator in CI, where the row sits below the fold and was never created. Reaching rows through `onData` or `RecyclerViewActions` removes that dependence on screen size, which is the practical reason the guide calls `onData` the safer habit. ## Summary - Plain views: `onView`. - `ListView`, `GridView`, `Spinner` popups: `onData`, matching the adapter's data. - `RecyclerView`: `onView(withId(...))` on the list, `perform(RecyclerViewActions...)`. - A row's content after scrolling: an ordinary `onView` with a matcher that is unique once the row exists.
- How do you tap the delete button inside a specific ListView row?Locate the row by its data, then narrow to the child. For an adapter that returns maps: `` onData(allOf(`is`(instanceOf(Map::class.java)), hasEntry(equalTo("number"), `is`("1042")))).onChildView(withId(R.id.delete)).perform(click()) ``. Add `inAdapterView(withId(R.id.invoices))` if the screen has more than one adapter view. Matching `withId(R.id.delete)` with `onView` alone would hit every visible row and throw `AmbiguousViewMatcherException`.
- RecyclerViewActions.actionOnItem throws a PerformException saying several items match; how do you fix it?By default the matcher must select exactly one item. Make the item matcher more specific, for example `hasDescendant(allOf(withId(R.id.number), withText("Invoice #1042")))`, or keep it and call `atPosition(n)` on the returned `PositionableRecyclerViewAction` to act on the n-th match, counting from zero.
- onData works on RecyclerView because it is also a list
- Every list row is in the view hierarchy, so onView always finds it
- RecyclerViewActions ships in espresso-core, so no extra dependency is needed
- onData takes a ViewMatcher for the row view
- Swipe the list with swipeUp in a loop until the row appears
Assumes Espresso 3.7.0; RecyclerViewActions.scrollToLastPosition() was added in Espresso 3.5.0.