automationui-automationandroid-espressoandroid-espresso-recorder

Unable to click delete button, multiple view


Unabel to click on delete button, getting error:

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.

I am using following code to click on delete icon:

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());

enter image description here

enter image description here


Solution

  • Your matcher allOf(withId(R.id.delete_button), withParent(withId(R.id.count_and_delete)), isDisplayed())) is still not unique enough, because it still finds two view matchers on your screen. Instead, try to match with the text in its grand-parent view:

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