Android:无法在使用Espresso的UI测试选项菜单中使用withId方法选择视图/菜单项

问题描述 投票:0回答:1

我正在研究一个Android Kotlin项目。我将使用Espresso框架编写的UI测试添加到我的项目中。但是在测试中,我无法使用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)
    }

这是菜单资源文件夹中的event_list.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}

我的代码有什么问题,我该如何解决?

android kotlin integration-testing android-espresso
1个回答
0
投票

[选择菜单项时,您不能使用withId,因为菜单是用它们自己的视图和ID夸大的,所以您的ID action_logout在视图层次结构(NoMatchingViewException)中将永远不存在:

menu

尝试使用其他视图匹配器,例如,使用withText

openActionBarOverflowOrOptionsMenu(ApplicationProvider.getApplicationContext<Context>())
onView(withText("your_menu_item_logout_string")).perform(click())

还请注意,当打开(弹出)菜单时,所有后续测试交互都将在菜单内执行,直到被取消为止,因此您不必担心背景视图中的匹配冲突。

© www.soinside.com 2019 - 2024. All rights reserved.