I have a ListView inside a PopupWindow, and I want to click on the second item on the list. I've tried the following:
// Open the popupwindow
onView(withId(R.id.popupwindow_open)).perform(click());
And now that the popup window appears, I tried:
onData(anything()).inAdapterView(withContentDescription("delete")).atPosition(1).perform(
click());
or this:
onView(withContentDescription("delete"))).perform(click());
But I always get that the view isn't found. How can I do this in Espresso?
The Android System Popups and Alerts are displayed in a different window. So, you have to try to find the view in that particular window rather than the main activity window.
Espresso provides a convenient method to find the root view for popup windows. Try this.
onView(ViewMatchers.withContentDescription("delete"))
.inRoot(RootMatchers.isPlatformPopup())
.perform(ViewActions.click());
In your case you have two different windows. So, to point to Espresso which window you want to interact with, you have to use Root matcher. Try out or play with these solutions a bit:
onView(withContentDescription("delete"))
.inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView()))))
.perform(click());
or
onData(withContentDescription("delete"))
.inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView()))))
.inAdapterView(withId(R.id.adapter_view))
.perform(click());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With