Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a test that clicks on views inside PopupWindow

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?

like image 945
MisaMisa Avatar asked Dec 09 '14 15:12

MisaMisa


2 Answers

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());
like image 129
Nishanth Avatar answered Oct 27 '22 00:10

Nishanth


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());
like image 39
denys Avatar answered Oct 27 '22 01:10

denys