androidandroid-espresso

Espresso won't select item in spinner when using a DialogFragment


Situation

I have an Espresso test that selects an item from a spinner. When I run this test such that the spinner is displayed on an Activity (phone mode), then it correctly selects the item using this code:

onView(withId(R.id.spinner)).perform(scrollTo()).perform(click());
onData(allOf(is(instanceOf(String.class)),
    is("Spinner item string")))
    .perform(click());

where the ID and string are replaced with the real values.

Problem

However, if the spinner is on a DialogFragment (tablet mode) then the same code just hangs and can't seem to find the item in the spinner. It can click the spinner but is no longer able to click the item. As far as I can tell there are no other differences.

Question

Has anyone run into this problem or knows what I'm missing? Is there something different I need to do when the spinner is on a Fragment? I couldn't see anything in the docs or issue tracker. Any advice or ideas appreciated. It could be a bug, but it doesn't seem like such a strange thing to do that it wouldn't have been picked up by now, so I think more likely I'm doing something wrong.

Maybe something to do with onData, but why would it not work now and how could I fix it?


Solution

  • It seems in this instance I didn't look hard enough before posting as I've found another question with the correct answer:

    onData(allOf(is(instanceOf(String.class)), 
       is("Spinner item string")))
       .inRoot(isPlatformPopup())
       .perform(click());
    

    The answer has not been accepted on the below post but this worked for me.

    RunTimeException in Android espresso when selecting spinner in dialog