如何为 Espresso 中的 Android 工具栏后退按钮操作编写代码

M.A*_*ali 5 android android-espresso android-toolbar

在我的 Android 应用程序中,我有工具栏,需要为 Espresso 中的工具栏后退按钮单击执行操作。我已经尝试了以下但它不起作用

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

需要执行它的后退按钮点击动作。

Ped*_*ran 6

ContentDescription没有为我工作,所以我不得不使用:

        onView(allOf(
            instanceOf(AppCompatImageButton::class.java), withParent(withId(R.id.toolbar))
        ))
            .perform(click())
Run Code Online (Sandbox Code Playgroud)

因为我无法在层次结构树中明确地识别它:

+-------->AppCompatImageButton{id=-1, visibility=VISIBLE, width=147, height=147, has-focus=false, has-focusable=false, 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=androidx.appcompat.widget.Toolbar$LayoutParams@acd01cf, tag=null, root-is-layout-requested=false, has-input-connection=false, x=21.0, y=0.0}
Run Code Online (Sandbox Code Playgroud)


Sri*_*sam 2

如果您的应用程序以英语运行,请使用以下命令:

onView(withContentDescription("Navigate up")).perform(click());
Run Code Online (Sandbox Code Playgroud)

要使其在任何语言上运行,请使用以下命令:

onView(withContentDescription(R.string.abc_action_bar_up_description)).perform(click());
Run Code Online (Sandbox Code Playgroud)

请注意,它R.string.abc_action_bar_up_description来自 AppCompat 支持库。

id另请注意, “返回箭头”按钮没有其他独特之处,因为 Espresso 将其视为如下:

+------> AppCompatImageButton {id=-1, desc=Navigate up, visibility=VISIBLE,
    width=84, height=68, has-focus=false, has-focusable=false, 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.support.v7.widget.Toolbar$LayoutParams@1af06e3d,
    tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
Run Code Online (Sandbox Code Playgroud)