Skip to content Skip to sidebar Skip to footer

Set Spinner Item With Espresso

I want to know how to set an item in a spinner in espresso testing. onView(withId(R.id.spinner_gender)).perform(click()); onData(allOf(is(instanceOf(String.class)))).atPosition(0).

Solution 1:

Your code snippet looks correct, so there may be an issue with another part of your test class? Are you getting an Exception or stack-trace you can update your question with? Also check the espresso documentation for a bit more explaination.

See small code example of how you can select a spinner option by text or it's position.

@RunWith(AndroidJUnit4.class)
public class BasicEspressoTest {
    @Rule
    public ActivityTestRule<MainActivity> testRule = new ActivityTestRule<>(MainActivity.class);

    @Test
    public void selectBySpinnerPosition() throws Exception {
        onView(withId(R.id.spinner)).perform(click());
        onData(allOf(is(instanceOf(String.class)))).atPosition(0).perform(click());
    }

    @Test
    public void selectBySpinnerText() throws Exception {
        onView(withId(R.id.spinner)).perform(click());
        onData(allOf(is(instanceOf(String.class)), is("spinner's text"))).perform(click());
    }
}

Post a Comment for "Set Spinner Item With Espresso"