@Override
  public Result execute(Map<String, Object> arguments) throws UiObjectNotFoundException {
    super.execute(arguments);
    @SuppressWarnings("unchecked")
    List<String> optionNames = (List<String>) arguments.get("optionNames");
    ;
    UiScrollable optionsMenu =
        new UiScrollable(
            new UiSelector()
                .className("android.widget.ListView")
                .packageName("com.android.settings")
                .focused(false));

    if (!isUiObjectAvailable(optionsMenu, arguments)) {
      return Result.FAILURE;
    }

    for (String optionName : optionNames) {
      UiObject option =
          optionsMenu.getChildByText(
              new UiSelector().className(TextView.class.getName()), optionName);
      if (!isUiObjectAvailable(option, arguments) && !enabled.equals(option.isEnabled())) {
        return Result.FAILURE;
      }
    }
    return Result.OK;
  }
  // 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)));
  }
Example #3
0
  @Test
  public void testClick() throws UiMultimatorException, IOException {
    UiDevice uiDevice = getUiDevice().any();
    uiDevice.pressHome();

    UiObject messagingButton = uiDevice.newUiObject(new UiSelector().text("Messaging"));
    messagingButton.click();
  }
Example #4
0
  @Test(
      expectedExceptions = UiMultimatorException.class,
      expectedExceptionsMessageRegExp = "UiObject not found: UiSelector\\[TEXT=notFound\\]")
  public void testUiSelectorThatWillThrow() throws Exception {
    UiDevice uiDevice = getUiDevice().any();
    uiDevice.pressHome();
    uiDevice.pressMenu();

    UiObject notFound = uiDevice.newUiObject(new UiSelector().text("notFound"));
    notFound.clickAndWaitForNewWindow();
  }
  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;
  }
Example #6
0
  /** Current implementation ignores the exception and continues. */
  public void postHandler() {
    // TODO: Add custom error logging here

    String formatedOutput =
        String.format(
            "UI Exception Message: %-20s\n", UiDevice.getInstance().getCurrentPackageName());
    Log.e(LOG_TAG, formatedOutput);

    UiObject buttonOK = new UiObject(new UiSelector().text("OK").enabled(true));
    // sometimes it takes a while for the OK button to become enabled
    buttonOK.waitForExists(5000);
    try {
      buttonOK.click();
    } catch (UiObjectNotFoundException e) {
      Log.e(LOG_TAG, "Exception", e);
    }
  }
Example #7
0
  @Test
  public void testClearText() throws Exception {
    UiDevice uiDevice = getUiDevice().any();
    uiDevice.pressHome();

    UiObject messagingButton = uiDevice.newUiObject(new UiSelector().text("Messaging"));
    messagingButton.clickAndWaitForNewWindow();

    UiObject newMessageButton = uiDevice.newUiObject(new UiSelector().text("New message"));
    newMessageButton.clickAndWaitForNewWindow();

    UiObject toText =
        uiDevice.newUiObject(
            new UiSelector().className("android.widget.MultiAutoCompleteTextView"));
    toText.setText("lollipop");

    toText.clearTextField();
  }
Example #8
0
  @Test
  public void testGetText() throws Exception {
    UiDevice uiDevice = getUiDevice().withSerial("emulator-5554");
    uiDevice.pressHome();
    uiDevice.pressMenu();

    UiObject settingsOption =
        uiDevice.newUiObject(new UiSelector().text("System settings").className(TextView.class));
    settingsOption.clickAndWaitForNewWindow();

    UiObject sound = uiDevice.newUiObject(new UiSelector().text("Sound"));
    Assert.assertEquals("Sound", sound.getText());

    UiObject battery = uiDevice.newUiObject(new UiSelector().text("Battery"));
    Assert.assertEquals("Battery", battery.getText());
  }
 public WebElement findElement(By by) {
   findPlugins().beforeFindElement(browser, by);
   WebElement result = UiObject.wrap(searchContext.findElement(by), by);
   findPlugins().afterFindElement(browser, by, result);
   return result;
 }
 public List<WebElement> findElements(By by) {
   findPlugins().beforeFindElement(browser, by);
   List<WebElement> result = UiObject.wrapAll(searchContext.findElements(by), by);
   findPlugins().afterFindElements(browser, by, result);
   return result;
 }
  private void loadXPrivacyConfig(String actionCommand) {
    // There is an intent to open the import configuration screen,
    // however there is a bug on XPrivacy, when that intent is called,
    // after a file is selected its contents are not displayed and no
    // operation is performed
    Log.d(uiaDaemon_logcatTag, "Loading XPrivacy configuration file.");

    // Extract the configuration file name
    String fileName = actionCommand.split("=")[1];
    Log.d(uiaDaemon_logcatTag, "Filename = " + fileName);

    try {
      // launchApp("XPrivacy");

      String XPrivacy = "biz.bokhorst.xprivacy";
      String XPrivacyToolbar = XPrivacy + ":id/widgetToolbar";
      String XPrivacyOkButton = XPrivacy + ":id/btnOk";
      UiObject toolbar = this.device.findObject(new UiSelector().resourceId(XPrivacyToolbar));

      // Locate the toolbar
      UiObject buttonSet = toolbar.getChild(new UiSelector().index(2));
      UiObject menuButton = buttonSet.getChild(new UiSelector().index(1));
      // Click to open the menu
      menuButton.click();
      waitForGuiToStabilize();

      // Select "Operations …" item from menu
      UiObject operationsButton = this.device.findObject(new UiSelector().text("Operations …"));
      operationsButton.click();
      waitForGuiToStabilize();

      // Select "Import"
      UiObject importButton = this.device.findObject(new UiSelector().text("Import"));
      importButton.click();
      waitForGuiToStabilize();

      // Select configuration file
      UiObject fileManagerButton = this.device.findObject(new UiSelector().text("OI File Manager"));
      fileManagerButton.click();
      waitForGuiToStabilize();

      UiObject textInput = this.device.findObject(new UiSelector().text("File name"));
      textInput.setText(fileName);

      UiObject pickFileButton = this.device.findObject(new UiSelector().text("Pick file"));
      pickFileButton.click();
      waitForGuiToStabilize();

      UiObject okButton = this.device.findObject(new UiSelector().resourceId(XPrivacyOkButton));
      okButton.click();

      do {
        okButton = this.device.findObject(new UiSelector().resourceId(XPrivacyOkButton));
        waitForGuiToStabilize();
      } while (!okButton.isEnabled());

      waitForGuiToStabilize();

      okButton.click();
      this.device.waitForIdle();
      waitForGuiToStabilize();

      // Return to home
      this.device.pressHome();
      this.device.waitForIdle();
    } catch (UiObjectNotFoundException ex) {
      Log.e(uiaDaemon_logcatTag, "Failed to import XPrivacy configuration.");
    }
    // catch (UiAutomatorDaemonException ex)
    // {
    //  Log.e(uiaDaemon_logcatTag, "Failed to start XPrivacy.");
    // }

    Log.d(uiaDaemon_logcatTag, "XPrivacy configuration file loaded.");
  }