How to click on an item inside a RecyclerView in Espresso

前端 未结 10 2322
逝去的感伤
逝去的感伤 2020-12-01 05:33

I have a RecyclerView (R.id.recyclerView) where each row has an image (R.id.row_image) and a TextView. I want to click on the image in the first row.
I\'ve tried to use

10条回答
  •  悲哀的现实
    2020-12-01 05:54

    With this code, you can scroll the recycler view to find your item withText and perform click or other action on it.

    dependencies

    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.3.0'
    

    Code

    @Test
    public void scrollRecyclerViewAndClick() {
        onView(withId(R.id.recycler_view)).
                perform(RecyclerViewActions.
                actionOnItem(withText("specific string"), click()));
    }
    

提交回复
热议问题