Espresso finding row in RecyclerView that could be off screen

Issue

I have a RecyclerView which could contain any number of items. I need to be able to get a single row from that list so i can check its contents or click/long click on that row.

I am struggling greatly to figure out how to do this as unlike ListView’s “onData” there is no built in functionality to scroll through the RecyclerView to find the row. It appears that with RecyclerView if the view is not actually ON SCREEN you can not verify that rows UI.

In the examples I have seen they are using the number of items in the list to scroll to each row. However I do not know in advance how many rows my list has as the number is not fixed.

This solution looked very promising, but that assumes you know the items in your list to pass into the espresso objects which is not applicable to me.

https://medium.com/@_rpiel/recyclerview-and-espresso-a-complicated-story-3f6f4179652e

This example also assumes it knows which rows are off screen https://github.com/googlesamples/android-testing/blob/master/ui/espresso/RecyclerViewSample/app/src/androidTest/java/com/example/android/testing/espresso/RecyclerViewSample/RecyclerViewSampleTest.java

Likewise this example knows which row to move to http://blog.forkingcode.com/2016/06/espresso-and-recyclerview.html

I need to do something like

onView(withId(R.id.recyclerView)).perform(findRowWhichHasText("name1");

where “findRowWhichHasText” does something like

  • find the number of items in the list

  • scroll to each one in turn (scrollToPosition(i) )

  • check if that row has the item I want. Something like

    StringUtils.equals(viewHolder.nameView.getText().toString(), assetName);
    

Solution

I found the solution in this wiki page. http://alexander-thiele.blogspot.com/2016/01/espresso-ui-tests-and-recyclerview.html

His code onView(withId(R.id.recyclerView)).perform(RecyclerViewActions.scrollToHolder(withHolderTimeView(getActivity().getString(R.string.all_day_event))));

Where withHolderTimeView is a matcher which matches a Viewholder to the item you are looking for.

Answered By – se22as

Leave a Comment