Espresso match first element found when many are in hierarchy
Asked Answered
T

5

32

I'm trying to write an espresso function to match the first element espresso finds according to my function, even when multiple matching items are found.

Ex: I have a list view with cells which contain item price. I want to be able to switch the currency to Canadian dollars and verify item prices are in CAD.

I'm using this function:

    onView(anyOf(withId(R.id.product_price), withText(endsWith("CAD"))))
        .check(matches(
                isDisplayed()));

This throws the AmbiguousViewMatcherException.

In this case, I don't care how many or few cells display CAD, I just want to verify it is displayed. Is there way to make espresso pass this test as soon as it encounters an object meeting the parameters?

Thumbtack answered 3/9, 2015 at 23:48 Comment(0)
B
42

You should be able to create a custom matcher that only matches on the first item with the following code:

private <T> Matcher<T> first(final Matcher<T> matcher) {
    return new BaseMatcher<T>() {
        boolean isFirst = true;

        @Override
        public boolean matches(final Object item) {
            if (isFirst && matcher.matches(item)) {
                isFirst = false;
                return true;
            }

            return false;
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("should return first matching item");
        }
    };
}
Benia answered 26/4, 2016 at 13:40 Comment(2)
I want to match nth element, (i.e. second / third etc) how to achieve it?Judie
I suggest description.appendText("first item that matches ") followed by matcher.describeTo(description) for a better message when it failsLawsuit
J
11

I created this matcher in case you have many elements with same characteristics like same id, and if you want not just the first Element but instead want an specific Element. Hope this helps:

    private static Matcher<View> getElementFromMatchAtPosition(final Matcher<View> matcher, final int position) {
    return new BaseMatcher<View>() {
        int counter = 0;
        @Override
        public boolean matches(final Object item) {
            if (matcher.matches(item)) {
                if(counter == position) {
                    counter++;
                    return true;
                }
                counter++;
            }
            return false;
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("Element at hierarchy position "+position);
        }
    };
}

Example:

You have many buttons with same id given from a library you are using, you want to pick the second button.

  ViewInteraction colorButton = onView(
            allOf(
                    getElementFromMatchAtPosition(allOf(withId(R.id.color)), 2),
                    isDisplayed()));
    colorButton.perform(click());
Judicative answered 21/4, 2017 at 21:18 Comment(0)
P
2

For anyone having the same problem I just had : if you are using the Matcher @appmattus shared to perform one ViewAction, it will be fine, but with multiple ViewActions in the perform, it won't :

onView(first(allOf(matcher1(), matcher2()))
      .perform(viewAction1(), viewAction2())

viewAction1 will be executed, but before executing vewAction2, the matchers are evaluated again and isFirst will always return false. You will get an error stating that no views are matching.

So, here a version working with multiple ViewActions and te possibility to return not just the first one, but the second or third (or...) if you prefer :

class CountViewMatcher(val count: Int) : BaseMatcher<View>() {

    private var matchingCount = 0
    private var matchingViewId: Int? = null

    override fun matches(o: Any): Boolean {
        if (o is View) {
            if (matchingViewId != null) {
                // A view already matched the count
                return matchingViewId == o.id
            } else {
                matchingCount++
                if (count == matchingCount) {
                    matchingViewId = o.id
                    return true
                } else {
                    return false
                }
            }
        } else {
            // o is not a view.
            return false
        }
    }
}
Pyralid answered 27/3, 2019 at 7:35 Comment(0)
A
1

From what I understand, in your scenario all prices should be in CAD after you have switched the currency. So just grabbing the first item and verifying it should solve your problem too:

onData(anything())
        .atPosition(0)
        .onChildView(allOf(withId(R.id.product_price), withText(endsWith("CAD"))))
        .check(matches(isDisplayed()));
Armandoarmature answered 3/7, 2016 at 13:3 Comment(0)
V
0

@ThomasV correctly identified the problem that matcher is evaluating for each ViewAction. That is why we need to compare View with itself multiple times as with others. So I suggest to use hashCode of each View, with always helps to find our View and separate it from others.

fun firstMatched(matcher: Matcher<View>): BoundedMatcher<View, View> {
    return object : BoundedMatcher<View, View>(View::class.java) {
        private var matchingHashCode: Int? = null

        override fun matchesSafely(item: View): Boolean {
            if (matcher.matches(item)) {
                when (matchingHashCode) {
                    null -> {
                        matchingHashCode = item.hashCode()
                        return true
                    }
                    item.hashCode() -> {
                        return true
                    }
                    else -> {
                        return false
                    }
                }
            } else {
                return false
            }
        }

        override fun describeTo(description: Description) {
            matcher.describeTo(description.appendText("first of matched view"))
        }
    }
}
Vellicate answered 6/6 at 9:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.