I am using RecyclerView and i need to get the total count of items in the RecyclerView using android espresso. How can i do it ?
Asked
Active
Viewed 7,674 times
2
-
1Possible duplicate of [How to count RecyclerView items with Espresso](https://stackoverflow.com/questions/36399787/how-to-count-recyclerview-items-with-espresso) – tir38 Feb 26 '19 at 18:34
3 Answers
5
I would do it like this:
@Rule
public ActivityTestRule<MyClass> activityRule = new ActivityTestRule(MyActivity.class);
@Test
public void myTest() {
RecyclerView recyclerView = activityRule.getActivity().findViewById(R.id.my_recyclerview)
int itemCount = recyclerView.getAdapter().getItemCount();
}

TheKarlo95
- 1,144
- 9
- 17
-
Minor comment, `;` is missing in first line of myTest method, not able to edit answer – Naeemahemad May 24 '19 at 12:25
-
1ActivityTestRule is replaced by ActivityScenarioRule. would you please comment on How to do it in ActivityScenarioRule – Rahul Matte Aug 22 '20 at 22:58
0
While Thekarlo95's answer specifically and fully answers the question, I would like to show my example on how to use his method together with Stéphane's method to test a difference in count before and after specific action:
@Test
public void FilterClickShouldChangeRecyclerViewCount() {
// Get items count before action
RecyclerView recyclerView = mActivityTestRule.getActivity().findViewById(R.id.recycler_view);
int countBefore = Objects.requireNonNull(recyclerView.getAdapter()).getItemCount();
Log.e("count", "Items count before: " + countBefore);
// Perform action
ViewInteraction actionMenuItemView = onView(
allOf(
withId(R.id.action_filter),
withContentDescription("Show Favorites")));
actionMenuItemView.perform(click());
// Verify that items count has changed
onView(withId(R.id.recycler_view))
// Instead of 'not', you can use any other hamcrest Matcher like 'is', 'lessThan' etc.
.check(new RecyclerViewItemCountAssertion(not(countBefore)));
}
And below is a code for RecyclerViewItemCountAssertion
class (just put it on a separate file). Note there are two constructors available:
RecyclerViewItemCountAssertion(int expectedCount)
- parameter of type integer is expected, defaultis
matcher is used.RecyclerViewItemCountAssertion(Matcher<Integer> matcher)
- parameter of typeMatcher<Integer>
is expected, likeis(3)
,lessThan(4)
etc.public class RecyclerViewItemCountAssertion implements ViewAssertion { private final Matcher<Integer> matcher; public RecyclerViewItemCountAssertion(int expectedCount) { this.matcher = is(expectedCount); } public RecyclerViewItemCountAssertion(Matcher<Integer> matcher) { this.matcher = matcher; } @Override public void check(View view, NoMatchingViewException noViewFoundException) { if (noViewFoundException != null) { throw noViewFoundException; } RecyclerView recyclerView = (RecyclerView) view; RecyclerView.Adapter adapter = recyclerView.getAdapter(); assert adapter != null; assertThat(adapter.getItemCount(), matcher); } }

Myroslav
- 896
- 12
- 21
0
with ActivityScenarioRule
@get: Rule
val activityScenarioRule = ActivityScenarioRule(ShowListActivity::class.java)
@Test
fun testItemCount(){
activityScenarioRule.scenario.onActivity { activityScenarioRule ->
val recyclerView = activityScenarioRule.findViewById<RecyclerView>(R.id.movieListRecyclerView)
val itemCount = recyclerView.adapter?.itemCount?:0
....
}
}

Bobby Sebastian
- 1
- 1
- 3