Friday, 23 November 2012

Get type of control element by WebElement

I reckon that is useful class:
it gets WebElement and returns the type of html control corresponding to it. Implemented using W3C, some tags are absent, yet you're free to extend

public class ControlElement {
  private final Type type;
  
  public enum Type {
    BUTTON, RADIO, TEXT_AREA, SELECT, OPTION, TEXT, LINK, CHECKBOX
  }

  private static final String BUTTON = "button";
  private static final String RADIO = "radio";
  private static final String TEXT_AREA = "textarea";
  private static final String CHECKBOX = "checkbox";
  private static final String SELECT = "select";
  private static final String OPTION = "option";
  private static final String TEXT = "text";
  private static final String LINK = "a";
  
  private static final String NULL = "null";
  private static final String INPUT = "input";
  private static final String PASSWORD = "input";
  private static final String SUBMIT = "submit";
  private static final String HIDDEN = "hidden";
  private static final String RESET = "reset";
  private static final String SELECT_ONE = "select-one";

  private final Map<Entry<String, String>, Type> map =
      new HashMap<Entry<String, String>, Type>();

  public ControlElement(WebElement element) {
    initMaping();
    String tagName = element.getTagName().toLowerCase();
    String typeAttribute = element.getAttribute("type");
    Entry<String, String> key = new AbstractMap.SimpleEntry<String, String>(tagName, typeAttribute);

    if (map.containsKey(key)) {
      type = map.get(key);
    } else {
      type = null;
    }
  }

  private void initMaping() {
    // link
    map.put(new AbstractMap.SimpleEntry<String, String>(LINK, NULL), Type.LINK);
    // button
    map.put(new AbstractMap.SimpleEntry<String, String>(BUTTON, NULL), Type.BUTTON);
    map.put(new AbstractMap.SimpleEntry<String, String>(BUTTON, SUBMIT), Type.BUTTON);
    map.put(new AbstractMap.SimpleEntry<String, String>(BUTTON, RESET), Type.BUTTON);
    map.put(new AbstractMap.SimpleEntry<String, String>(INPUT, SUBMIT), Type.BUTTON);
    map.put(new AbstractMap.SimpleEntry<String, String>(INPUT, BUTTON), Type.BUTTON);
    map.put(new AbstractMap.SimpleEntry<String, String>(INPUT, RESET), Type.BUTTON);
    // text input
    map.put(new AbstractMap.SimpleEntry<String, String>(INPUT, TEXT), Type.TEXT);
    map.put(new AbstractMap.SimpleEntry<String, String>(INPUT, PASSWORD), Type.TEXT);
    map.put(new AbstractMap.SimpleEntry<String, String>(INPUT, HIDDEN), Type.TEXT);
    // text area
    map.put(new AbstractMap.SimpleEntry<String, String>(TEXT_AREA, NULL), Type.TEXT_AREA);
    // checkbox
    map.put(new AbstractMap.SimpleEntry<String, String>(INPUT, CHECKBOX), Type.CHECKBOX);
    // radio
    map.put(new AbstractMap.SimpleEntry<String, String>(INPUT, RADIO), Type.RADIO);
    // select
    map.put(new AbstractMap.SimpleEntry<String, String>(SELECT, NULL), Type.SELECT);
    map.put(new AbstractMap.SimpleEntry<String, String>(SELECT, SELECT_ONE), Type.SELECT);
    // option
    map.put(new AbstractMap.SimpleEntry<String, String>(OPTION, NULL), Type.OPTION);
  }

  /**
   * @return the type of html control element
   */
  public Type getType() {
    return type;
  }
}

No comments:

Post a Comment