繁体   English   中英

如何在espresso中以编程方式单击Android手机的“主页”按钮

[英]How to click Android phone “home” button programmatically in espresso

我需要在Android手机上模拟Espresso中的主页按钮单击。 我试过了

onView(withId(android.R.id.home))执行(点击())。

和onView(withContentDescription(“ Navigate up”))。perform(click());

正如某些帖子所建议的那样,但始终无法找到视图。 我是Espresso的新手,不确定如何调试它。 谢谢。

最好与withContentDescription(R.string.abc_action_bar_up_description)一起使用,而不要使用“向上导航”,但是它无论如何都不能像单击主页按钮那样,它仅使用导航栏的“后退”按钮,因此只有在您拥有导航按钮的情况下,它才有效在您的应用中。

如果要模拟屏幕按钮单击并且正在使用UI Automator库,则可以像这样模拟

fun pressHome() {
    // Might be a good idea to initialize it somewhere else
    val uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
    uiDevice.pressHome()
}

另外,如果您不想或不能使用UI Automator,则可以尝试使用KeyEvent.KEYCODE_HOME调用Intrstumentation.sendKeyDownUpSync()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM