使用onView时使用Espresso单击操作栏中的菜单项(withId(...))

Rom*_*zzi 8 android optionmenu action-menu android-espresso

我正在使用Espresso进行测试,首先打开操作栏:

openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());
Run Code Online (Sandbox Code Playgroud)

一切都还可以,但是我想点击一个选项菜单:

onView(withId(R.id.action_menu)).perform(click());
Run Code Online (Sandbox Code Playgroud)

菜单布局xml有一个id为"action_menu"的选项.

我收到一个错误:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: ***.********.******.android.debug:id/action_menu
If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:android.support.v7.widget.ListPopupWindow$DropDownListView{43fd3f58 VFED.VC. .F...... 24,24-612,168}
Run Code Online (Sandbox Code Playgroud)

我通过使用以下方式实现了这一步:

onView(withText(R.string.action_report)).perform(click());
Run Code Online (Sandbox Code Playgroud)

我想知道id有什么问题.

有任何想法吗?

Cor*_*e B 2

看起来代表菜单的视图不知道 id,而只知道项目的文本。我是 Espresso 新手,因此无法提供更多详细信息,但这篇文章与您的问题相关: Espresso NoMatchingViewException when using withId matcher