【问题标题】:UiAutomator picking app to test from app drawerUiAutomator 从应用程序抽屉中挑选应用程序进行测试
【发布时间】:2015-08-15 11:06:22
【问题描述】:

我正在尝试使用 UiAutomator 为 android 应用程序(我有 apk 但没有源代码)编写 UI 自动化“黑盒”测试。 在设置阶段,我无法打开抽屉的应用程序。到目前为止,我的代码是

@Override
public void setUp() throws Exception {
    super.setUp();
    mDevice = UiDevice.getInstance(getInstrumentation());
    mDevice.pressHome();
    //Wait for the app drawer icon to show up on the screen
    mDevice.wait(Until.hasObject(By.desc("Apps")), 3000);
    //Obtain reference to the app drawer button in order to click it
    UiObject drawerIcon = mDevice.findObject(new UiSelector().description("Apps"));
    drawerIcon.clickAndWaitForNewWindow();
    //Finding and Clicking on the Sunshine app
    UiObject drawer = mDevice.findObject(new UiSelector().resourceId("com.sec.android.app.launcher:id/apps_grid"));
    UiObject appToTest = mDevice.findObject(new UiSelector().description("app-to-test-description"));
    while (!appToTest.exists()) {
        drawer.swipeLeft(3);
    }
    appToTest.clickAndWaitForNewWindow();
}

当我运行测试时,它应该会打开应用程序(然后运行我尚未编写的各种测试方法。) 相反,它打开抽屉并挂起。我想有更好的方法来识别抽屉并滚动它直到找到正确的应用程序。 这是错误日志。

运行测试
开始试运行
android.support.test.uiautomator.UiObjectNotFoundException: UiSelector[RESOURCE_ID=com.sec.android.app.launcher:id/apps_grid] 在 android.support.test.uiautomator.UiObject.getVisibleBounds(UiObject.java:891) 在 android.support.test.uiautomator.UiObject.swipeLeft(UiObject.java:315) 在 com.crisanti.roberto.uturistautomatedtest.UiAutomatorTest.setUp(UiAutomatorTest.java:29) 在 android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191) 在 android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176) 在 android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555) 在 android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)

【问题讨论】:

    标签: android testing android-uiautomator


    【解决方案1】:

    如果您真的想从菜单开始,您已经回答了自己的问题。但是请记住,在不同的设备中,应用程序抽屉可能会有所不同(三星和其他人通常在 Android 之上自己做)。

    你也可以

    Context context = InstrumentationRegistry.getInstrumentation().getContext(); //gets the context based on the instrumentation
    Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageNameOfYourApp);  //sets the intent to start your app
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);  //clear out any previous task, i.e., make sure it starts on the initial screen
    context.startActivity(intent);  //starts the app
    

    您可以使用 UiAutomatorViewer 获得的包名称(在 sdk-folder/tools/ 中)。

    如果你愿意,你可以等到应用程序真正启动时这样做(我假设你已经有一个 UiDevice 设备)。

    device.wait(Until.hasObject(By.pkg(packageNameOfYourApp)), timeOut); //ofc you need to set timeout
    

    这适用于任何设备/模拟器配置。

    【讨论】:

    • 感谢您提及此专业提示:“使用 UiAutomatorViewer(在 sdk-folder/tools/ 中)可以获得的包名称。”我们应用程序包名称的末尾有一个“.debug”,但我使用的是原始包名称(不带后缀),因此返回的意图始终为空。可能这个“.debug”后缀来自构建类型。反正让我疯了!
    【解决方案2】:

    我已经弄清楚如何让它工作。在 Lollipop 上,启动器由 "com.google.android.googlequicksearchbox:id/apps_customize_pane_content" 而不是 "com.sec.android.app.launcher:id/apps_grid" 标识。这可能是一个问题,因为测试取决于平台版本(在 Kitkat 上,启动器有另一种行为,在 Marshmellow 上也会有另一种不同的行为)。 我做的另一个修改上线了 抽屉.swipeLeft(3); 我改成 抽屉.swipeLeft(5);

    总结一下在 Lollipop 上使用 UiAutomator 启动应用程序的代码是:

    public void setUp() throws Exception {
            super.setUp();
            mDevice = UiDevice.getInstance(getInstrumentation());
            mDevice.pressHome();
            //Obtain reference to the app drawer button in order to click it
            UiObject allAppsButton = mDevice.findObject(new UiSelector().description("Apps"));
            //The operation below expects the click will result a new  window.
            allAppsButton.clickAndWaitForNewWindow();
            // Find the application in the app launcher
            UiObject appViews = mDevice.findObject(new UiSelector().resourceId("com.google.android.googlequicksearchbox:id/apps_customize_pane_content"));
            UiObject navigationDrawerApp = mDevice.findObject(new UiSelector().text("app-to-test-name"));
            while (!navigationDrawerApp.exists()){
                appViews.swipeLeft(5);
            }
            navigationDrawerApp.clickAndWaitForNewWindow();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多