2

I've one small question, what's the difference beetween following lines in Selenium with Page Factory?

@FindBy(id = "foobar") WebElement foobar;

And

@FindBy(how = How.ID, using = "foobar") WebElement foobar;
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
mtmx
  • 827
  • 1
  • 10
  • 31

1 Answers1

2

As per the JavaDocs of Annotation Type FindBy both the expression :

@FindBy(id = "foobar") WebElement foobar;

and

@FindBy(how = How.ID, using = "foobar") WebElement foobar;

Both of the expressions points to the same element.

Modifier How

How is defined in org.openqa.selenium.support.How, extends java.lang.Enum<How> and also implements java.lang.Comparable<How>. The Enum Constants of How are as follows :


Update

As per the counter question, if you have a look at the source code of the enum How it is defined as :

public enum How {
  CLASS_NAME {
    @Override
    public By buildBy(String value) {
      return By.className(value);
    }
  },
  CSS {
    @Override
    public By buildBy(String value) {
      return By.cssSelector(value);
    }
  },
  ID {
    @Override
    public By buildBy(String value) {
      return By.id(value);
    }
  },
  ID_OR_NAME {
    @Override
    public By buildBy(String value) {
      return new ByIdOrName(value);
    }
  },
  LINK_TEXT {
    @Override
    public By buildBy(String value) {
      return By.linkText(value);
    }
  },
  NAME {
    @Override
    public By buildBy(String value) {
      return By.name(value);
    }
  },
  PARTIAL_LINK_TEXT {
    @Override
    public By buildBy(String value) {
      return By.partialLinkText(value);
    }
  },
  TAG_NAME {
    @Override
    public By buildBy(String value) {
      return By.tagName(value);
    }
  },
  XPATH {
    @Override
    public By buildBy(String value) {
      return By.xpath(value);
    }
  },
  UNSET {
    @Override
    public By buildBy(String value) {
      return ID.buildBy(value);
    }
  };

  public abstract By buildBy(String value);
}

So for example when you implement How in conjunction with ID as per the following line :

@FindBy(how = How.ID, using = "foobar") WebElement foobar;

how = How.ID functionally returns By.id(value) and is equivalent to what exactly the following line returns :

@FindBy(id = "foobar") WebElement foobar;

Hence we can conclude that @FindBy(id = "foobar") and @FindBy(how = How.ID, using = "foobar") are just 2 different way to achieve the same result and are available as per user's choice.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352