Android Espresso check selected spinner text
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"))));
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.
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