MisaMisa
MisaMisa

Reputation: 283

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?

Upvotes: 16

Views: 7231

Answers (3)

wojt2119
wojt2119

Reputation: 1

Try this:

onView(withId(android.R.id.id_you_are_looking_for)).perform(click());

In my case, I use system dialogs, so id has to be preceded with "android", then it works fine

Upvotes: 0

Nishanth
Nishanth

Reputation: 1871

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());

Upvotes: 42

denys
denys

Reputation: 6910

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());

Upvotes: 5

Related Questions