UiAutomator挑选应用程序以从应用程序抽屉中进行测试

Cry*_*s85 2 testing android android-uiautomator

我正在尝试使用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();
}
Run Code Online (Sandbox Code Playgroud)

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

运行测试
试运行开始
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),位于com.crisanti.roberto.uturistautomatedtest.UiAutomatorTest.setUp(UiAutomatorTest.java:29)的android.support.test.uiautomator.UiObject.swipeLeft(UiObject.java:315)处,android.test.AndroidTestRunner.runTest(AndroidTestRunner .java:191)于android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)于android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:的android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555) 1853年)

Inê*_*nês 5

如果您真的想从菜单中启动它,那么您已经回答了自己的问题。但是请记住,在不同的设备中,应用程序抽屉可能会有所不同(三星和其他应用程序通常在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
Run Code Online (Sandbox Code Playgroud)

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

如果您愿意,可以等到实际启动该应用程序后再启动(我假设您已经有一个UiDevice设备)。

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

这将适用于任何设备/仿真器配置。