How to count RecyclerView items with Espresso

后端 未结 7 1111
时光说笑
时光说笑 2020-12-25 09:49

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

7条回答
  •  悲&欢浪女
    2020-12-25 10:12

    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))))
    

提交回复
热议问题