Espresso无法区分相同的按钮

Espresso can not distinguish between identical buttons

我正在尝试使用浓缩咖啡编写测试。我被卡住了,因为 espresso 无法区分屏幕上的两个按钮 (admin_server_trash),因为它们看起来完全相同。在实际代码库中,按钮根据它们左侧的内容进行操作。我如何在 Espresso 中实现它?

显示的错误在下方,导致错误的行也在下方。

android.support.test.espresso.AmbiguousViewMatcherException: 'with id: com.revelwood.quantum:id/admin_server_trash' matches multiple views in the hierarchy.

    @Test
public void TestManageServerAddGood()
{
    ...
    onView(withId(R.id.admin_server_trash)).perform(click());
}

另外,这里有 2 个按钮有错误。它们似乎是相同的,所以我不知道如何得到浓缩咖啡来区分它们。

+---------->AppCompatImageButton{id=2131624221, res-name=admin_server_trash, desc=image, visibility=VISIBLE, width=63, height=63, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=933.0, y=33.0} ****MATCHES****

+---------->AppCompatImageButton{id=2131624221, res-name=admin_server_trash, desc=image, visibility=VISIBLE, width=63, height=63, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=933.0, y=33.0} ****MATCHES****

Within the actual code base the buttons act based on what is to the left of them. How can I implement this in Espresso?

试试 isLeftOf 匹配器。

很好的作弊 sheet 概述了所有(?)可用的 Espresso 匹配器。 https://google.github.io/android-testing-support-library/assets/espresso-cheat-sheet-2.1.0.png

我想通了!这出奇地简单。您还可以使用 Espresso 附带的 hasSibling 匹配器来确定它与什么匹配。

        onView(allOf(is((withId(R.id.admin_server_trash))), hasSibling(withText(nickname))))
            .perform(click());