Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select a listview in espresso?

I'm testing with espresso and I have multiple AdapterViews in one page, for example, with id: R.id.list1, R.id.list2, when I use

onData(withMainValue("xx")).check(matches(isDisplayed()))

public static Matcher<Object> withMainValue(final String value) {
    return new BoundedMatcher<Object,
                            GuessYouLikeGoodItem.DataEntity>(GuessYouLikeGoodItem.DataEntity.class) {
        @Override public void describeTo(Description description) {
            description.appendText("has value " + value);
        }
        @Override public boolean matchesSafely(
                        GuessYouLikeGoodItem.DataEntity item) {
            return item.store_name.contains(value);
        }
    };
}

, 

the Espresso reports:

    android.support.test.espresso.AmbiguousViewMatcherException: 'is assignable from class: class android.widget.AdapterView' matches multiple views in the hierarchy.
    Problem views are marked with '****MATCHES****' below.

How to select the specific listview and try onData on it ?

like image 438
herbertD Avatar asked Mar 11 '23 23:03

herbertD


1 Answers

If you have multiple listview with unique id, you should be able to check is one of the list is displayed

onView(withId(R.id.list1)).check(matches(isDisplayed()));

If you want to go inside AdapterView, this will allow you to click an element inside listview

onData(anything()).inAdapterView(withId(R.id.list1)).atPosition(0).perform(click());
like image 155
jitinsharma Avatar answered Apr 06 '23 02:04

jitinsharma