21

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?

LordWater
  • 345
  • 1
  • 2
  • 6
  • What if you preformed a drag event during the test? something like this - https://qathread.blogspot.com/2014/01/discovering-espresso-for-android-swiping.html – Shmuel Mar 20 '17 at 22:29
  • 1
    The tests go so fast that I don't have enough time to swipe it down. And also I tried using Espresso Recorder, but the drag event are not captured. – LordWater Mar 20 '17 at 22:57

3 Answers3

28

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.

sembozdemir
  • 4,137
  • 3
  • 21
  • 30
Josh Laird
  • 6,974
  • 7
  • 38
  • 69
  • I was missing that RVs have their own scroll. Thank you. – Zeek Aran Feb 28 '18 at 22:20
  • The Question states he is already using this. However the bottom is not reached because the last element is bigger than the screen. scrollToPosition just scrolls to the top edge of the item. An item bigger than the screen would therefore still have parts hidden below the bottom edge of the screen. I am having the same problem, even with this code. I am a little confused why this is accepted as it does only scoll to the top of the last element, not the very bottom of the RecyclerView. – Emil S. Feb 15 '19 at 10:20
  • 1
    ScrollToposition now takes the ViewHolder name as the type. suggested a modification for the same – srv_sud Oct 22 '19 at 17:16
15

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())
chunping mu
  • 151
  • 1
  • 2
4

I use this ;

 // Get total item of myRecyclerView
RecyclerView recyclerView = mActivityTestRule.getActivity().findViewById(R.id.myRecyclerView);
int itemCount = recyclerView.getAdapter().getItemCount();

Log.d("Item count is ", String.valueOf(itemCount));

// Scroll to end of page with position
onView(withId(R.id.myRecyclerView))
    .perform(RecyclerViewActions.scrollToPosition(itemCount - 1));
scll
  • 51
  • 6