Android Espresso check selected spinner text
Asked Answered
T

4

34

I have this code in my Espresso test

onView(withId(R.id.src))
    .perform(click());
onData(hasToString(startsWith("CCD")))
    .perform(click());
onView(withId(R.id.src))
    .check(matches(withText(containsString("CCD"))));

What I'm trying to do is to click the item in the Spinner and check if it's indeed selected in the Spinner.

But I'm getting this error:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: a string containing "CCD"' doesn't match the selected view. Expected: with text: a string containing "CCD" Got: "AppCompatSpinner{id=2131558533, res-name=src, visibility=VISIBLE, width=528, height=163, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}"

Triplex answered 15/7, 2015 at 3:10 Comment(1)
In test spresso documentation they show a solution to select a item inside the spinner from its index position I think is the right way... URL : github.com/vgrec/EspressoExamples/blob/master/app/src/…Ezequiel
C
83

Replace withText() with withSpinnerText()

onView(withId(spinnerId)).perform(click());
onData(allOf(is(instanceOf(String.class)), is(selectionText))).perform(click());
onView(withId(spinnerId)).check(matches(withSpinnerText(containsString(selectionText))));

Reference: https://code.google.com/p/android-test-kit/issues/detail?id=85

Cramer answered 21/7, 2015 at 11:16 Comment(6)
what is the selectionText here??Bohunk
That's a string - the spinner text, the same one that is visible to user.Cramer
How do I just grab the Spinner object itself and have my way with it, without using these accessor methods? Or is that forbidden due to threads, or the other standard Android excuses?Conservatory
This link unfortunately is broken. @Cramer any idea what the correct link supposed to be?Heelandtoe
Can't find the correct link anymore. In the link there was an error report and fix for it. Generally just using matcher function withSpinnerText() should solve the issue these days and details in link were just additional historical info. Long time ago espresso did not have withSpinnerText().Cramer
Thank you so much, worked like a charm. I wasn't willing to go through clumsy Espresso API docs to find the solution. You saved my ton of time. I wish these APIs were bit straight forward to work with because on a surface level these nested calls make zero sense.Laquanda
H
14

Really simple solution that worked out for me .....without using matcher for CustomSpinner

onView(withId(R.id.custom_spinner)).perform(click());
onData(anything()).atPosition(1).perform(click());
onView(withId(R.id.custom_spinner)).check(matches(withSpinnerText(containsString("yourstring"))));
Hillegass answered 22/3, 2017 at 11:11 Comment(1)
This seems more straight-forward than the accepted answer. What makes the accepted answer better than this? (especially since the accepted answer references a dead link)Rugg
E
4

For custom adapter i had yo create a custom matcher:

 onView(withId(R.id.spinner)).perform(click());
 onData(allOf(is(instanceOf(YourCustomClass.class)), withMyValue("Open"))).perform(click());


public static <T> Matcher<T> withMyValue(final String name) {
    return new BaseMatcher<T>() {
        @Override
        public boolean matches(Object item) {
            return item.toString().equals(name);
        }

        @Override
        public void describeTo(Description description) {

        }
    };
}

Then you must override toString() method on your custom class.

Elisavetpol answered 13/8, 2016 at 18:23 Comment(2)
YouCustomClass is the adapter class and item is an item from the adapter?Underbody
what is YourCustomClass? is it like class model?Immerge
G
0

This works for me

 private void selectSpinnerItem(final int resId, final String selection) {
    onView(withId(resId)).perform(click());
    onData(hasToString(selection)).perform(click());
    onView(withId(resId)).check(matches(withSpinnerText(containsString(selection))));
}
Gavrah answered 23/11, 2021 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.