2

I have several buttons in my app with the same Id, I would like Espresso to click on all of them. These are basically collapse/expand buttons and thus I want the UI test to expand all elements in the view.

I am always getting:

android.support.test.espresso.AmbiguousViewMatcherException: 'with id: com.myapp.android:id/liveLinearLayout' matches multiple views in the hierarchy.
Problem views are marked with '****MATCHES****' below.

EDIT: tried this in accordance to a reply:

onView(allOf(withId(R.id.footage_layout_expand_group))).perform(click());

Got this again:

android.support.test.espresso.AmbiguousViewMatcherException: '(with id: com.myapp.android:id/footage_layout_expand_group)' matches multiple views in the hierarchy.
Problem views are marked with '****MATCHES****' below.
MichelReap
  • 5,630
  • 11
  • 37
  • 99

1 Answers1

4

If you know how many items with the same id there are you could use:

public static Matcher<View> nthChildOf(final Matcher<View> parentMatcher, final int childPosition) {
    return new TypeSafeMatcher<View>() {
        @Override public void describeTo(Description description) {
            description.appendText("with "+childPosition+" child view of type parentMatcher");
        }
        @Override public boolean matchesSafely(View view) {
            if (!(view.getParent() instanceof ViewGroup)) {
                return parentMatcher.matches(view.getParent());
            }
            ViewGroup group = (ViewGroup) view.getParent();
            return parentMatcher.matches(view.getParent()) && group.getChildAt(childPosition).equals(view);
        }
    };
}

To getevery child and click on them. Then, to use it:

onView(nthChildOf(withId(R.id.parent_container), itemNumber)
    .perform(click());

On a loop increasing the itemNumber

jeprubio
  • 17,312
  • 5
  • 45
  • 56