首页 > 解决方案 > 如何在 android Espresso Instrumentation Test 中从最近的应用程序中选择应用程序

问题描述

如何使用 Espresso Android Instrumentation Test 转到最近的应用程序菜单以及如何从最近的应用程序中选择特定的应用程序

标签: androidandroid-espressoandroid-testingandroid-uiautomatorandroid-instrumentation

解决方案


由于从最近的应用程序菜单中选择应用程序,它需要控制器而不是设备。我认为仅靠 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")
               }
           }

         }


推荐阅读