使用espresso验证弹出菜单中的项目 [英] Verify items in a popup menu with espresso

查看:96
本文介绍了使用espresso验证弹出菜单中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a popupmenu. Uix screenshot provided. Now I want to verify some items in the list by clicking them and verify what's happening.

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

But no matter what I do I don't seem to be able to select items in the popup menu. The menu does not have an id and I don't think it's possible to set an id of a menu.

我尝试过其他类似的事情:

I've tried different things like:

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

onView(withText("5 sekunder")).perform(click());

但是没有任何效果.如何单击弹出菜单中的项目? 此处,您可以找到带有弹出菜单.

But nothing works. How do I click on an item in a popup menu? Here you can find the UIX file with the view hierarachy of the popup menu.

要更清楚地了解这种情况,是单击操作栏右上角的点以展开子菜单.在我的情况下,子菜单始终由三部分组成.我最接近解决方案的是:

To be clearer when this happens it is when you click on the dots in the right corner of the action bar to expand the submenu. The submenu in my case always consists of three items. The most closest I've come to a solution is:

onData(anything()).atPosition(2).perform(click());

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

But most of the times it opens the first item and not the item in position two which results in

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

可以在此处找到该日志.在日志中,您可以看到它实际上单击了错误的项目,并单击了被单击的菜单:菜单1".

The log can be found here. In the log you can see that it actually clicks the wrong item, it clicks "clicked menu: Menu 1".

推荐答案

Espresso为此提供了RootMatchers.对我来说很好:

Espresso provides RootMatchers for that case. It works well for me:

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

public static Matcher<Root> isPopupWindow() {
        return isPlatformPopup();
}

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

isPlatformPopup() is an Espresso RootMatcher. You can read more here 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());

这篇关于使用espresso验证弹出菜单中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆