我想为我的 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/