Exemplo n.º 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;
  }