Espresso - 单击嵌套回收器视图中的项目

时间:2021-05-25 17:55:29

标签: java android-espresso

所以,我在一个 recyclerview 中有一个 recyclerview。我想要做的是单击子回收器视图中第一项(位置 0)内的图像。我该怎么做?每个答案都不起作用。谢谢。

2 个答案:

答案 0 :(得分:0)

<块引用>

所以,我在一个 recyclerview 中有一个 recyclerview。我想要做的是单击子回收器视图中第一项(位置 0)内的图像。

咨询 the documentation 我们发现有几个选项。

所以你想对主回收器执行一个操作。该操作是对子回收器执行另一个回收器视图操作。然后那个点击一个图片。用 Espresso 术语来说,这意味着:

// Starting from the bottom up, you want to click on the image
val clickOnImage = actionOnItem(withId(R.id.image_view), click())

// Next you want to do the above click action on the specific view in the child recycler
val clickOnImageOnChildRecycler = actionOnItemAtPosition(0, clickOnImage)

// Next you want to do the above on the specific child recycler
val clickOnChildReycler = actionOnItemAtPosition(INDEX_OF_CHILD_RECYCLER, clickOnImageOnChildRecycler)

// Finally, you want to do the clicking of a child on the main recycler
onView(withId(R.id.parent_recycler_view)).perform(clickOnChildReycler)

然后你可以简化和内联:

onView(withId(R.id.parent_recycler_view)) // On the parent recycler ...
    .perform(actionOnItemAtPosition(INDEX_OF_CHILD_RECYCLER, // On the specific child recycler ...
        actionOnItemAtPosition(0, // On the first item ...
            actionOnItem(withId(R.id.image_view), click()))) // Click the image ...

不言而喻,我没有测试过这个,但希望它有效,或者至少让你走上正确的道路。

答案 1 :(得分:0)

我是这样做的:

//滚动到Parent Recyclerview的第2个位置 onView(withId(PARENT_RECYCLERVIEW_ID)).perform(actionOnItemAtPosition(1, scrollTo()));

//添加这一行只是为了确保我已经在子回收器视图中 onView(withId(PARENT_RECYCLERVIEW_ID)).perform(actionOnItemAtPosition(1, CustomViewAction.clickChildViewWithId(CHILD_RECYCLERVIEW_ID)));

//点击子recyclerview中第一项里面的图片

onView(allOf(withId(CHILD_RECYCLERVIEW_ID), withParent(withRecyclerView(PARENT_RECYCLERVIEW_ID).atPosition(1)))).perform(actionOnItemAtPosition(0, click()));