Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

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

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

And

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

1 Answer

0 votes
by (62.9k points)

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 :

 

CLASS_NAME

CSS

ID

ID_OR_NAME

LINK_TEXT

NAME

PARTIAL_LINK_TEXT

TAG_NAME

UNSET

XPATH

Also, 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 ways to achieve the same result and are available as per user's choice.

Browse Categories

...