使用espresso验证弹出菜单中的项目

peu*_*hse 6 android android-espresso

我有一个popupmenu.Uix截图提供.现在,我想通过单击它们来验证列表中的某些项目并验证发生了什么.

但无论我做什么,我似乎都无法在弹出菜单中选择项目.菜单没有ID,我认为不可能设置菜单的ID.

我尝试过不同的东西,比如:

onView(nthChildOf(anyOf(withId(android.R.id.title)), 1)).perform(click());

onView(withText("5 sekunder")).perform(click());
Run Code Online (Sandbox Code Playgroud)

但没有任何作用.如何单击弹出菜单中的项目?在这里,您可以找到带有弹出菜单视图层的UIX文件.

编辑:

更明显的是,当您单击操作栏右角的点以展开子菜单时.在我的情况下,子菜单总是由三个项目组成.我最接近解决方案的是:

onData(anything()).atPosition(2).perform(click());
Run Code Online (Sandbox Code Playgroud)

但大多数情况下它会打开第一个项目,而不是第二个项目中的项目

No activities in stage RESUMED. Did you forget to launch the activity. (test.getActivity() or similar)?
Run Code Online (Sandbox Code Playgroud)

日志可以在这里找到.在日志中,您可以看到它实际上点击了错误的项目,它点击"点击菜单:菜单1".

在此输入图像描述

Jup*_*ter 12

Espresso为该案例提供RootMatchers.这对我来说很有效:

onView(withText("Text")).inRoot(isPopupWindow()).perform(click());

public static Matcher<Root> isPopupWindow() {
        return isPlatformPopup();
}
Run Code Online (Sandbox Code Playgroud)

isPlatformPopup()是Espresso RootMatcher.你可以在这里阅读更多内容https://google.github.io/android-testing-support-library/docs/espresso/advanced/#using-inroot-to-target-non-default-windows

或试试这个:

onView(withText("Text"))
  .inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView()))))
  .perform(click());
Run Code Online (Sandbox Code Playgroud)

  • @innoSPG很好,但它确实是这个问题的答案.如果有人问我"嘿,我如何点击弹出菜单中的某个项目?" 再说一次,我会说"使用Espresso RootMatchers".对这个问题来说,这将是一个很好的答案. (4认同)