noname1101
noname1101

Reputation: 11

Espresso can not distinguish between identical buttons

I am attempting to write a test using espresso. I am getting stuck because espresso can not distinguish between two buttons (admin_server_trash) that are on the screen because they appear to be identical. Within the actual code base the buttons act based on what is to the left of them. How can I implement this in Espresso?

The error that shows up is below, and the line that causes the error is also below.

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

Also, here are the 2 buttons with the error. They seem to be identical, so I can not figure out how to get espresso to distinguish them.

+---------->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****

Upvotes: 0

Views: 197

Answers (2)

noname1101
noname1101

Reputation: 11

I figured it out! It was surpringly simple. You just also use the hasSibling matcher that comes with Espresso to figure out what it is matched with.

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

Upvotes: 1

dominicoder
dominicoder

Reputation: 10175

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

Try the isLeftOf matcher.

Nice cheat sheet with overview of all(?) available Espresso matchers. https://google.github.io/android-testing-support-library/assets/espresso-cheat-sheet-2.1.0.png

Upvotes: 0

Related Questions