Espresso match first element found when many are in hierarchy

天涯浪子 提交于 2020-01-09 19:19:07

问题


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?


回答1:


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



回答2:


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



回答3:


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



回答4:


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
        }
    }
}


来源:https://stackoverflow.com/questions/32387137/espresso-match-first-element-found-when-many-are-in-hierarchy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!