I have an Android application which has a RecyclerView with N elements, and when this RecyclerView reaches to end when scrolling, then more elements are added (so, it's an infinite list which loads data when scroll reached the bottom).
I would like to test this, but I haven't found a way to do this. I use RecyclerViewActions which have scrollToPosition but even if I put the last position, the end is not reached (because the height of each element is high).
Anybody know how could I do this?
And in order for the scrollTo () to work, you need to provide a matcher that uniquely identifies that ItemView. Your current matcher tells espresso to scroll to a ViewHolder that was inflated with a TextView as an itemView.
RecyclerViewActions.scrollTo () matches against the ItemView of the ViewHolder, which is inflated in onCreateViewHolder () of the adapter. And in order for the scrollTo () to work, you need to provide a matcher that uniquely identifies that ItemView.
Your current matcher tells espresso to scroll to a ViewHolder that was inflated with a TextView as an itemView. Which can happen, but usually you have some ViewGroup action going on there to style the view holders in the way you want them to look.
Most RecyclerViewActions are given a matcher to select a particular view / viewholder within the RecyclerView. Performs a ViewAction on a view matched by viewHolderMatcher.
I use the below to scroll to the bottom of my RecyclerView
.
activity = mActivityTestRule.launchActivity(startingIntent);
onView(withId(R.id.recyclerView)).perform(
RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(
activity.recyclerView.getAdapter().getItemCount() - 1
)
);
You'll then have to use idling resources (or Thread.sleep()
) to call this again when more data has loaded.
you can implement ViewAction. like this:
class ScrollToBottomAction : ViewAction {
override fun getDescription(): String {
return "scroll RecyclerView to bottom"
}
override fun getConstraints(): Matcher<View> {
return allOf<View>(isAssignableFrom(RecyclerView::class.java), isDisplayed())
}
override fun perform(uiController: UiController?, view: View?) {
val recyclerView = view as RecyclerView
val itemCount = recyclerView.adapter?.itemCount
val position = itemCount?.minus(1) ?: 0
recyclerView.scrollToPosition(position)
uiController?.loopMainThreadUntilIdle()
}
}
and then use it like this:
onView(withId(R.id.recyclerView)).perform(ScrollToBottomAction())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With