我在PopupWindow中有一个ListView,我想点击列表中的第二个项目。我尝试过以下方法:
// Open the popupwindow
onView(withId(R.id.popupwindow_open)).perform(click());
现在弹出窗口出现了,我试过了:
onData(anything()).inAdapterView(withContentDescription("delete")).atPosition(1).perform(
click());
或者这个:
onView(withContentDescription("delete"))).perform(click());
但是我总是觉得这个观点还没有找到。我怎么能在Espresso中做到这一点?
答案 0 :(得分:21)
Android系统弹出窗口和警报显示在不同的窗口中。因此,您必须尝试在该特定窗口而不是主活动窗口中查找视图。
Espresso提供了一种查找弹出窗口根视图的便捷方法。试试这个。
onView(ViewMatchers.withContentDescription("delete"))
.inRoot(RootMatchers.isPlatformPopup())
.perform(ViewActions.click());
答案 1 :(得分:5)
在您的情况下,您有两个不同的窗口。因此,要指向Espresso要与之交互的窗口,您必须使用Root匹配器。尝试或使用这些解决方案:
onView(withContentDescription("delete"))
.inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView()))))
.perform(click());
或
onData(withContentDescription("delete"))
.inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView()))))
.inAdapterView(withId(R.id.adapter_view))
.perform(click());
答案 2 :(得分:0)
尝试一下:
onView(withId(android.R.id.id_you_are_looking_for)).perform(click());
在我的情况下,我使用系统对话框,因此id必须以“ android”开头,然后可以正常工作