android - 如何在 android Espresso Instrumentation Test 中从最近的应用程序中选择应用程序
问题描述
如何使用 Espresso Android Instrumentation Test 转到最近的应用程序菜单以及如何从最近的应用程序中选择特定的应用程序
解决方案
由于从最近的应用程序菜单中选择应用程序,它需要控制器而不是设备。我认为仅靠 Espresso 是无法做到的。
但是您可以通过使用 android uiautomator 来实现这一点。
fun selectAppFromRecentApps(appTitle:String){
mDevice.pressHome()
mDevice.pressRecentApps()
var uiSelector = UiSelector().className("android.widget.ScrollView") //scroll view listing all recent apps
var count = mDevice.findObject(uiSelector).childCount
for (i in 0 until count step 1) {
val child = UiScrollable(uiSelector.childSelector(UiSelector().resourceId("com.android.systemui:id/task_view_bar").instance(i))).getChild(UiSelector().resourceId("com.android.systemui:id/title")) // app framelayout
val text = child.text
if (text == appTitle) {
child.click()
break
}
if(i==count-1){
throw RuntimeException("App : "+ appTitle +" not found in
recent apps")
}
}
}
推荐阅读
- android - 当我使用已弃用的 api 时,Android Studio 3.1.3 不显示任何错误
- java - 使用 slf4j + log4j 和 dropwizard
- python - python:无法打开文件“帮助”:[Errno 2]没有这样的文件或目录
- audio-processing - librosa中音频样本的单位是什么?
- function - Sass Function with list as argument
- java - 2的幂?代码有什么问题
- angular - 清晰的隐藏警报计时器
- linux - 将多个文件更改为可执行简化的脚本
- php - 如何使用 PHP 代码同时获取 IPv4 和 IPv6 地址?
- python - Singleton 不在 Cython 中工作