How to get the number of elements with the same identifiers that are not displayed in adapter mode

I have a case where there are several instances of an element, and I want to count them. My view count is as follows:

 public static ViewInteraction onTestPanelView(){
        return onView(allOf(withId(R.id.myId), isDisplayed()));
    }

With a match for the view, I get the following error:

com.google.android.apps.common.testing.ui.espresso.AmbiguousViewMatcherException: '(with identifier: is <2131427517> and appears on the user screen)' corresponds to several views in the hierarchy. The task views are marked with "**** MATCHES ****" below.

This is correct because I have multiple instances of elements with the same identifier (R.id.myId). I want to write a method that returns me the number of views matching my criteria. Please note: they are not in adapter mode.

+4
source share
3 answers

An old question, but maybe someone will find it useful.

From here you can find that:

onView hamcrest , , - - . , . , .

. , . , .

.

0

You can wrap the condition that you put in onView()with a match and place the counter inside the connector. Each time a match matches an element, the counter must increment.

int counter = 0;

public static Matcher<View> withIdAndDisplayed(final int id) {
    Checks.checkNotNull(id);
    return new TypeSafeMatcher<View>() {
        @Override
        public void describeTo(Description description) {
            description.appendText("with item id: " + id);
        }

        @Override
        public boolean matchesSafely(View view) {
            if ((view.getId() == id) && (view.getGlobalVisibleRect(new Rect())
                    && withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE).matches(view))){
                counter++;
                return true;
            }
            return false;
        }
    };
}

public static ViewInteraction onTestPanelView(){
    return onView(withIdAndDisplayed(R.id.myId));
}
0
source

All Articles