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 Re
You can create a custom BoundedMatcher
:
object RecyclerViewMatchers {
@JvmStatic
fun hasItemCount(itemCount: Int): Matcher {
return object : BoundedMatcher(
RecyclerView::class.java) {
override fun describeTo(description: Description) {
description.appendText("has $itemCount items")
}
override fun matchesSafely(view: RecyclerView): Boolean {
return view.adapter.itemCount == itemCount
}
}
}
}
And then use it like this:
onView(withId(R.id.recycler_view)).check(matches((hasItemCount(5))))