如何在浓缩咖啡中以编程方式单击 Android phone "home" 按钮
How to click Android phone "home" button programmatically in espresso
我需要在 Android phone 上模拟 Espresso 中的主页按钮点击。
我试过了
onView(withId(android.R.id.home)).perform(click());
和 onView(withContentDescription("Navigate up")).perform(click());
正如一些帖子所建议的那样,但它总是找不到视图。
我是 Espresso 的新手,不确定如何调试它。谢谢
使用 withContentDescription(R.string.abc_action_bar_up_description)
比 "Navigate up" 更好,但它不像单击主页按钮那样,它只使用导航栏的 "back" 按钮,所以它只会如果你的应用程序中有它,就可以工作。
如果你想模拟 home 按钮点击并且你正在使用 UI Automator library, you can simulate 就像这样
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()
我需要在 Android phone 上模拟 Espresso 中的主页按钮点击。 我试过了
onView(withId(android.R.id.home)).perform(click());
和 onView(withContentDescription("Navigate up")).perform(click());
正如一些帖子所建议的那样,但它总是找不到视图。 我是 Espresso 的新手,不确定如何调试它。谢谢
使用 withContentDescription(R.string.abc_action_bar_up_description)
比 "Navigate up" 更好,但它不像单击主页按钮那样,它只使用导航栏的 "back" 按钮,所以它只会如果你的应用程序中有它,就可以工作。
如果你想模拟 home 按钮点击并且你正在使用 UI Automator library, you can simulate 就像这样
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()