Skip to content Skip to sidebar Skip to footer

How To Count Recyclerview Items With Espresso

Using Espresso and Hamcrest, How can I count items number available in a recyclerView? Exemple: I would like check if 5 items are displaying in a specific RecyclerView (scrolling i

Solution 1:

Here an example ViewAssertion to check RecyclerView item count

publicclassRecyclerViewItemCountAssertionimplementsViewAssertion {
  privatefinalint expectedCount;

  publicRecyclerViewItemCountAssertion(int expectedCount) {
    this.expectedCount = expectedCount;
  }

  @Overridepublicvoidcheck(View view, NoMatchingViewException noViewFoundException) {
    if (noViewFoundException != null) {
        throw noViewFoundException;
    }

    RecyclerViewrecyclerView= (RecyclerView) view;
    RecyclerView.Adapteradapter= recyclerView.getAdapter();
    assertThat(adapter.getItemCount(), is(expectedCount));
  }
}

and then use this assertion

onView(withId(R.id.recyclerView)).check(new RecyclerViewItemCountAssertion(5));

I have started to write an library which should make testing more simple with espresso and uiautomator. This includes tooling for RecyclerView action and assertions. https://github.com/nenick/espresso-macchiato See for example EspRecyclerView with the method assertItemCountIs(int)

Solution 2:

Adding a bit of syntax sugar to the @Stephane's answer.

publicclassRecyclerViewItemCountAssertionimplementsViewAssertion {
    privatefinal Matcher<Integer> matcher;

    publicstatic RecyclerViewItemCountAssertion withItemCount(int expectedCount) {
        return withItemCount(is(expectedCount));
    }

    publicstatic RecyclerViewItemCountAssertion withItemCount(Matcher<Integer> matcher) {
        returnnewRecyclerViewItemCountAssertion(matcher);
    }

    privateRecyclerViewItemCountAssertion(Matcher<Integer> matcher) {
        this.matcher = matcher;
    }

    @Overridepublicvoidcheck(View view, NoMatchingViewException noViewFoundException) {
        if (noViewFoundException != null) {
            throw noViewFoundException;
        }

        RecyclerViewrecyclerView= (RecyclerView) view;
        RecyclerView.Adapteradapter= recyclerView.getAdapter();
        assertThat(adapter.getItemCount(), matcher);
    }
}

Usage:

    import static your.package.RecyclerViewItemCountAssertion.withItemCount;

    onView(withId(R.id.recyclerView)).check(withItemCount(5));
    onView(withId(R.id.recyclerView)).check(withItemCount(greaterThan(5)));
    onView(withId(R.id.recyclerView)).check(withItemCount(lessThan(5)));
    // ...

Solution 3:

To complete nenick answer and provide and little bit more flexible solution to also test if item cout is greaterThan, lessThan ...

publicclassRecyclerViewItemCountAssertionimplementsViewAssertion {

    privatefinal Matcher<Integer> matcher;

    publicRecyclerViewItemCountAssertion(int expectedCount) {
        this.matcher = is(expectedCount);
    }

    publicRecyclerViewItemCountAssertion(Matcher<Integer> matcher) {
        this.matcher = matcher;
    }

    @Overridepublicvoidcheck(View view, NoMatchingViewException noViewFoundException) {
        if (noViewFoundException != null) {
            throw noViewFoundException;
        }

        RecyclerViewrecyclerView= (RecyclerView) view;
        RecyclerView.Adapteradapter= recyclerView.getAdapter();
        assertThat(adapter.getItemCount(), matcher);
    }

}

Usage:

onView(withId(R.id.recyclerView)).check(new RecyclerViewItemCountAssertion(5));
onView(withId(R.id.recyclerView)).check(new RecyclerViewItemCountAssertion(greaterThan(5));
onView(withId(R.id.recyclerView)).check(new RecyclerViewItemCountAssertion(lessThan(5));
// ...

Solution 4:

Based on @Sivakumar Kamichetty answer:

  1. Variable 'COUNT' is accessed from within inner class, needs to be declared final.
  2. Unnecessarily line: COUNT = 0;
  3. Transfer COUNT variable to one element array.
  4. Variable result is unnecessary.

Not nice, but works:

publicstaticintgetCountFromRecyclerView(@IdResint RecyclerViewId) {
    finalint[] COUNT = {0};
    Matchermatcher=newTypeSafeMatcher<View>() {
        @OverrideprotectedbooleanmatchesSafely(View item) {
            COUNT[0] = ((RecyclerView) item).getAdapter().getItemCount();
            returntrue;
        }
        @OverridepublicvoiddescribeTo(Description description) {}
    };
    onView(allOf(withId(RecyclerViewId),isDisplayed())).check(matches(matcher));
    return COUNT[0];
}

Solution 5:

Validated answer works but we can solve this problem with one line and without adapter awareness :

onView(withId(R.id.your_recycler_view_id)).check(matches(hasChildCount(2)))

Replace your_recycler_view_id with your id and 2 with the number to assert.

Post a Comment for "How To Count Recyclerview Items With Espresso"