Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Espresso check selected spinner text

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}"

like image 792
Jezer Crespo Avatar asked Jul 15 '15 03:07

Jezer Crespo


2 Answers

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

like image 194
Jonas Avatar answered Sep 18 '22 12:09

Jonas


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"))));
like image 14
ArpitA Avatar answered Sep 21 '22 12:09

ArpitA