Я работаю над проектом Android Kotlin. Я добавляю тесты пользовательского интерфейса, написанные с использованием платформы Espresso, в мой проект. Но в моих тестах я не могу выбрать пункт меню меню параметров в панели действий с помощью метода withId ().
Ниже описано, как я добавил меню в панель действий в действии.
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.event_list, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_logout -> {
ApplicationController.instance.clearLoginData()
finish()
}
}
return super.onOptionsItemSelected(item)
}
Это список событий. xml в папке ресурсов меню
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_logout"
android:icon="@android:drawable/ic_lock_power_off"
android:title="@string/menu_item_logout"
app:showAsAction="never"/>
</menu>
Вот как мой метод теста
@Test
fun itWipesOutLoginDataWhenLogoutMenuItemIsTapped() {
FakeEventService.SCENARIO_UNDER_TEST = 0
this.eventListActivityRule.launchActivity(null)
openActionBarOverflowOrOptionsMenu(ApplicationProvider.getApplicationContext<Context>())
onView(withId(R.id.action_logout)).perform(click())
}
Как вы можете видеть в методе теста Я выбираю пункт меню с помощью метода withId (). Но когда я запускаю тест, я получаю следующую ошибку:
androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.example.memento:id/action_logout
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:androidx.appcompat.widget.MenuPopupWindow$MenuDropDownListView{2582e16 VFED.VC.. ........ 0,0-686,168}
View Hierarchy:
+>PopupDecorView{id=-1, visibility=VISIBLE, width=686, height=168, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params={(740,98)(686x168) gr=TOP START CENTER DISPLAY_CLIP_VERTICAL sim={state=unchanged} ty=APPLICATION_SUB_PANEL fmt=TRANSLUCENT surfaceInsets=Rect(112, 112 - 112, 112) (manual)
fl=LAYOUT_NO_LIMITS ALT_FOCUSABLE_IM WATCH_OUTSIDE_TOUCH SPLIT_TOUCH HARDWARE_ACCELERATED FLAG_LAYOUT_ATTACHED_IN_DECOR
pfl=WILL_NOT_REPLACE_ON_RELAUNCH LAYOUT_CHILD_WINDOW_IN_PARENT_FRAME}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+->PopupBackgroundView{id=-1, visibility=VISIBLE, width=686, height=168, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@de56b69, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+-->MenuDropDownListView{id=-1, visibility=VISIBLE, width=686, height=168, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@4044e8f, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+--->ListMenuItemView{id=-1, visibility=VISIBLE, width=686, height=168, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.AbsListView$LayoutParams@393b6fa, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+---->AppCompatImageView{id=2131230882, res-name=group_divider, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@70144ab, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
+---->LinearLayout{id=2131230818, res-name=content, visibility=VISIBLE, width=686, height=168, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@d648c6, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+----->RelativeLayout{id=-1, visibility=VISIBLE, width=574, height=76, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@4a503dd, tag=null, root-is-layout-requested=false, has-input-connection=false, x=56.0, y=46.0, child-count=2}
Что не так с моим кодом и как я могу это исправить?