安卓系统中如何从最近的应用中选择应用 Espresso仪器测试方法

问题描述 投票:0回答:1

如何进入最近的应用程序菜单,如何使用爱思聪Android仪器测试从最近的应用程序中选择特定的应用程序?

android android-espresso android-testing android-uiautomator android-instrumentation
1个回答
0
投票

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

         }

© www.soinside.com 2019 - 2024. All rights reserved.