How can i use espresso to click an item from a list view based on item’ text?

Issue

I’m trying to click a list view item using espresso but no success.

I tried everything that’s on stackOverflow

onData(anything()).inAdapterView(withId(R.id.more_menu_list)).atPosition(0).
                    onChildView(withId(R.id.mm_item_text)).
                    check(matches(withText("Log in")))
                    .perform(click());

            onView(allOf(is(instanceOf(MenuListAdapter.class)), hasSibling(withText("Log in")))).perform(click());

            onData(allOf(is(instanceOf(MenuListAdapter.class)), hasEntry(equalTo("STR"), is("Log in"))))
                            .perform(click());

            onData(anything()).inAdapterView(withContentDescription("Log in")).atPosition(0).perform(click());

            onData(hasToString(startsWith("Promotions")))
                    .inAdapterView(withId(R.id.more_menu_list)).atPosition(0)
                    .perform(click());


            onData(hasEntry(equalTo(MenuListAdapter.class),is("Log out")))
                    .onChildView(withId(R.id.more_menu_list));
            onView(withId(R.id.mm_item_text)).check(matches(withText("Log in")));

I also tried to create a custom matcher
    public static Matcher<Object> withItemValue(final String value) {
        return new BoundedMatcher<Object, ExtraMenu>(ExtraMenu.class) {
            @Override
            protected boolean matchesSafely(ExtraMenu item) {
                return item.getText().toUpperCase().equals(String.valueOf(value));

            }

            @Override
            public void describeTo(Description description) {
                description.appendText("has value " + value);
            }
        };
    }

used as:

onData(withItemValue("Promotions")).inAdapterView(withId(R.id.more_menu_list)).perform(click());

Most of the times i get this error. There s a menu with 5 items Caused by: java.lang.RuntimeException: No data found matching: has value Promotions contained values: <[Data: 0 (class: java.lang.Integer) token: 0, Data: 1 (class: java.lang.Integer) token: 1, Data: 2 (class: java.lang.Integer) token: 2, Data: 3 (class: java.lang.Integer) token: 3, Data: 4 (class: java.lang.Integer) token: 4]>

Solution

I found a way, i’ll post it in order to help others. I had to change the list view to a recycle view and used this command.

onView(withId(R.id.list))
                .perform(RecyclerViewActions.actionOnItem(
                        hasDescendant(withText("Your string")), click()));

You might want to use Thread.sleep(2000) if you have multiple actions before this line of code. Also you need this dependency in your build.gradle:

androidTestImplementation("com.android.support.test.espresso:espresso-contrib:2.2.2") {
    exclude group: 'com.android.support', module: 'appcompat'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude group: 'com.android.support', module: 'support-v7'
    exclude group: 'com.android.support', module: 'design'
    exclude module: 'support-annotations'
    exclude module: 'recyclerview-v7'
}

Answered By – Panduru Ionut

Leave a Comment