android - 在 Android Espresso 中按下概览按钮

标签 android kotlin android-testing android-espresso

我想为我的 Android 应用程序创建一个测试。每当它失去焦点时,它就应该做点什么。

因此,我想通过按 2 次概览按钮(主页按钮旁边的按钮)来对此进行测试。但是我怎样才能用 Espresso 做到这一点呢?我尝试了一些键码来按下,但没有一个起作用(不幸的是,表中没有 KEY_OVERVIEW)。

那么我该如何测试呢?

谢谢,

尼克拉斯

最佳答案

这对于 espresso 是不可能的,espresso 用于在您的应用内进行 UI 测试,但不能与外部 UI 元素交互。

如果你想按下概览(或后退或任何其他系统)按钮,你可以使用 ui-automator:

UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
mDevice.pressRecentApps();
mDevice.pressRecentApps();

上面的这段代码按了两次最近使用的应用程序按钮。

ui-automator正是用于与您的应用程序外部的 UI 交互(主页或最近的应用程序按钮、权限对话框……),并且可以与 espresso 完美结合以增强您的测试。要使用它,请将其包含在您的 gradle 中:

androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'

关于android - 在 Android Espresso 中按下概览按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42819656/

相关文章:

android - GCM 发送图像而不是消息

Java文档 : How to Create a Custom Markup

java - Kotlin 检查参数的类型

android - java.lang.SecurityException : Permission Denial: getIntentSender() when using UiAutomation in a test 错误

android - 找不到 AndroidJUnit4 的委托(delegate)运行程序 androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner

android - Dagger2 转换为 android.dagger

java - 如何在另一个类中使用相同的数组

kotlin - 线程何时会挂起暂停功能?

mocking - robolectric 3.0 Mockito -- 如何在事件中模拟类

android - 更改操作栏/工具栏操作文本颜色