Page Factory In Selenium Webdriver: Inbuilt Page Object Model Of Selenium

Hello Folks,

We have seen below topics as of now:

  1. Introduction of Page Object Model
  2. Plain Page Object Model

In this post we will see inbuilt page object model in selenium webdriver.

Selenium webdriver has inbuilt Page Object Model called Page Factory. It provides @FindBy annotation to create object repository.

What is @FindBy annotation?

  • Annotations in java are used to provide additional information.
  • @FindBy annotation is used to locate web element using any locators available in selenium webdriver (id, xpath, css etc).
  • It is used to mark a field on a Page Object to indicate an alternative mechanism for locating the element or a list of elements.
  • It is used in conjunction with PageFactory this allows users to quickly and easily create PageObjects.

Syntax: You can use @FindBy annotation in two ways:

@FindBy(LocatorStrategy=”Locatorvalue”) WebElement elementName;

Where LocatorStrategy may be any one from Id, name, className, tagName, css, xpath, linkText, partialLinkText.

Example:

@FindBy(id=”someID”) @FindBy(name=”someName”) @FindBy(className=”someClassName”) @FindBy(tagName=”sometagName”) @FindBy(xpath=”someXpath”) @FindBy(css=”someCSS”) @FindBy(linkText=”someLinktext”)

@FindBy(partialLinkText=”somePartialLinktext”)