首页 > 解决方案 > 无法单击删除按钮,多视图

问题描述

无法单击删除按钮,出现错误:

android.support.test.espresso.AmbiguousViewMatcherException: '(with id: cit:id/delete_button and has parent matching: with id: cit:id/count_and_delete and is displayed on the screen to the user)' matches multiple views in the hierarchy.

我正在使用以下代码单击删除图标:

ViewInteraction appCompatImageView222 = onView(
                allOf(withId(R.id.delete_button),
                        withParent(withId(R.id.count_and_delete)),
                        isDisplayed()));
        appCompatImageView222.perform(actionOnItemAtPosition(0, click()));


ViewInteraction appCompatImageView222 = onView(
                    allOf(withId(R.id.delete_button),
                            withParent(withId(R.id.count_and_delete)),
                            isDisplayed()));
            appCompatImageView222.perform(click());

在此处输入图像描述

在此处输入图像描述

标签: automationui-automationandroid-espressoandroid-espresso-recorder

解决方案


您的匹配器allOf(withId(R.id.delete_button), withParent(withId(R.id.count_and_delete)), isDisplayed()))仍然不够独特,因为它仍然在您的屏幕上找到两个视图匹配器。相反,尝试匹配其祖父视图中的文本:

onView(allOf(
        withId(R.id.delete_button),
        withParent(withParent(withChild(withText("Count 1"))))))
                .check(matches(isDisplayed()))
                .perform(click())

推荐阅读