예제 #1
0
  /**
   * Create and return a UiSelector based on the strategy, text, and how many you want returned.
   *
   * @param strategy The {@link Strategy} used to search for the element.
   * @param text Any text used in the search (i.e. match, regex, etc.)
   * @param many Boolean that is either only one element (false), or many (true)
   * @return UiSelector
   * @throws InvalidStrategyException
   * @throws AndroidCommandException
   */
  private UiSelector getSelector(final Strategy strategy, final String text, final Boolean many)
      throws InvalidStrategyException, AndroidCommandException {
    UiSelector sel = new UiSelector();

    switch (strategy) {
      case CLASS_NAME:
      case TAG_NAME:
        String androidClass = AndroidElementClassMap.match(text);
        if (androidClass.contentEquals("android.widget.Button")) {
          androidClass += "|android.widget.ImageButton";
          androidClass = androidClass.replaceAll("([^\\p{Alnum}|])", "\\\\$1");
          sel = sel.classNameMatches("^" + androidClass + "$");
        } else {
          sel = sel.className(androidClass);
        }
        break;
      case NAME:
        sel = sel.description(text);
        break;
      case XPATH:
        break;
      case LINK_TEXT:
      case PARTIAL_LINK_TEXT:
      case ID:
      case CSS_SELECTOR:
      default:
        throw new InvalidStrategyException(
            "Strategy " + strategy.getStrategyName() + " is not valid.");
    }

    if (!many) {
      sel = sel.instance(0);
    }
    return sel;
  }
예제 #2
0
  /*
   * @param command The {@link AndroidCommand} used for this handler.
   *
   * @return {@link AndroidCommandResult}
   *
   * @throws JSONException
   *
   * @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.
   * bootstrap.AndroidCommand)
   */
  @Override
  public AndroidCommandResult execute(final AndroidCommand command) throws JSONException {
    final Hashtable<String, Object> params = command.params();

    // only makes sense on a device
    final Strategy strategy = Strategy.fromString((String) params.get("strategy"));
    final String text = (String) params.get("selector");
    final String contextId = (String) params.get("context");

    Logger.debug(
        "Finding " + text + " using " + strategy.toString() + " with the contextId: " + contextId);

    final Boolean multiple = (Boolean) params.get("multiple");
    final boolean isXpath = strategy.equals("xpath");

    if (isXpath) {
      final JSONArray xpathPath = (JSONArray) params.get("path");
      final String xpathAttr = (String) params.get("attr");
      final String xpathConstraint = (String) params.get("constraint");
      final Boolean xpathSubstr = (Boolean) params.get("substr");

      try {
        if (multiple) {
          final UiSelector sel =
              getSelectorForXpath(xpathPath, xpathAttr, xpathConstraint, xpathSubstr);
          return getSuccessResult(fetchElements(sel, contextId));
        } else {
          final UiSelector sel =
              getSelectorForXpath(xpathPath, xpathAttr, xpathConstraint, xpathSubstr);
          return getSuccessResult(fetchElement(sel, contextId));
        }
      } catch (final AndroidCommandException e) {
        return getErrorResult(e.getMessage());
      } catch (final ElementNotFoundException e) {
        return getErrorResult(e.getMessage());
      } catch (final ElementNotInHashException e) {
        return getErrorResult(e.getMessage());
      } catch (final UiObjectNotFoundException e) {
        return getErrorResult(e.getMessage());
      }
    } else {
      try {
        final UiSelector sel = getSelector(strategy, text, multiple);
        if (multiple) {
          return getSuccessResult(fetchElements(sel, contextId));
        } else {
          return getSuccessResult(fetchElement(sel, contextId));
        }
      } catch (final InvalidStrategyException e) {
        return getErrorResult(e.getMessage());
      } catch (final ElementNotFoundException e) {
        return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
      } catch (final AndroidCommandException e) {
        return getErrorResult(e.getMessage());
      } catch (final ElementNotInHashException e) {
        return getErrorResult(e.getMessage());
      } catch (final UiObjectNotFoundException e) {
        return getErrorResult(e.getMessage());
      }
    }
  }