private UiObject navigateToAppLaunchIcon(String appLaunchIconName) throws UiObjectNotFoundException { // Simulate a short press on the HOME button. this.device.pressHome(); // We’re now in the home screen. Next, we want to simulate // a user bringing up the All Apps screen. // If you use the uiautomatorviewer tool to capture a snapshot // of the Home screen, notice that the All Apps button’s // content-description property has the value "Apps". We can // use this property to create a UiSelector to find the button. UiObject allAppsButton = this.device.findObject(new UiSelector().description("Apps")); // Simulate a click to bring up the All Apps screen. allAppsButton.clickAndWaitForNewWindow(); // In Android 6 default apps menu (Nexus 5X) there is a search box to locate the application // Use it to search for the application UiObject searchTextbox = this.device.findObject( new UiSelector() .resourceId("com.google.android.googlequicksearchbox:id/search_box_proxy")); searchTextbox.setText(appLaunchIconName); waitForGuiToStabilize(); // The application (if found), will be the first item in the application list container // Type: com.google.android.googlequicksearchbox:id/icon UiObject appListview = this.device.findObject( new UiSelector() .resourceId("com.google.android.googlequicksearchbox:id/apps_list_view")); UiObject selectedItem = appListview.getChild(new UiSelector().index(0)); d(uiaDaemon_logcatTag, selectedItem.getContentDescription()); return selectedItem; }
// region Launching app private void launchApp(String appLaunchIconText) throws UiAutomatorDaemonException { d( uiaDaemon_logcatTag, "Launching app by navigating to and clicking icon with text " + appLaunchIconText); boolean clickResult; try { UiObject app = navigateToAppLaunchIcon(appLaunchIconText); Log.v(uiaDaemon_logcatTag, "Pressing the " + appLaunchIconText + " app icon to launch it."); clickResult = app.clickAndWaitForNewWindow(); waitForGuiToStabilize(); } catch (UiObjectNotFoundException e) { Log.w( uiaDaemon_logcatTag, String.format( "Attempt to navigate to and click on the icon labeled '%s' to launch the app threw an exception: %s: %s", appLaunchIconText, e.getClass().getSimpleName(), e.getLocalizedMessage())); d(uiaDaemon_logcatTag, "Pressing 'home' button after failed app launch."); this.device.pressHome(); waitForGuiToStabilize(); return; } if (clickResult) waitForGuiToStabilize(); else Log.w( uiaDaemon_logcatTag, (String.format( "A click on the icon labeled '%s' to launch the app returned false", appLaunchIconText))); }