[英]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.