How to assert inside a RecyclerView in Espresso?

后端 未结 11 1829
故里飘歌
故里飘歌 2020-12-02 06:38

I am using espresso-contrib to perform actions on a RecyclerView, and it works as it should, ex:

onView(withId(R.id.recycler_view))
.perform(Rec         


        
11条回答
  •  感情败类
    2020-12-02 07:09

    Modified matcher solution for when you check if your item(Text in TextView) is first in RecyclerView.

    // True if first item in the RV and input text
    fun  customMatcherForFirstItem(name: String, matcher: Matcher): Matcher {
        return object: BaseMatcher () {
            var isFirst = true
            override fun matches(item: Any): Boolean {
                if (isFirst && (item as TextView).text == name) {
                    isFirst = false
                    return true
                }
                return false
            }
            override fun describeTo(description: Description?) {}
        }
    }
    

    Usage: (returns true if child textview in the item has the same text as we expect/search)

        onView(withText("TEXT_VIEW_TEXT")).check(matches(
            customMatcherForFirstItem("TEXT_VIEW_TEXT", withParent(withId(R.id.recycler_view)))))
    

    Based on -> https://proandroiddev.com/working-with-recycler-views-in-espresso-tests-6da21495182c

提交回复
热议问题