QaMantra: August 2018

Page Factory Design Pattern

In Selenium 'Page factory Design Pattern' is Extensive and Enhanced Version of Page Object Model(POM) , Page Factory pattern is used to initialize web elements which are defined in Page Objects.

How Elements are identified in Page Factory Design Pattern ?

@FindBy annotation is used to Identify Elements in Page Factory ,Annotations supports all 8 Type of Selenium Locators (id, name, className, css, xpath, tagName, linkText and partialLinkText)

There is two ways to use this annotation.

1. By 'How'

@FindBy(how = How.LocatorName, using = "Value")
private WebElement ElementName; Ex-
@FindBy(how = How.ID, using = "password") private WebElement Password;

2. By Direct Locator name @FindBy(LocatorName="Value") private WebElement ElementName; Ex-
@FindBy(id="password") private WebElement Password; For Multiple Elements Handlng(List of WebElements) Ex- @FindBy(tagName = "mylist") private List<WebElement> links;

How to Initialize WebElements Defined in Page Object ?

page objects Initalzation should be done using initElements() method from PageFactory Class as below, Once we call initElements() method, all elements will get initialized. PageFactory.initElements() static method takes the driver instance of the given class and the class type, and returns a Page Object with its fields fully initialized.

LoginPage loginpage = new LoginPage(driver);
PageFactory.initElements(driver, homePage);

Note : LoginPage is a class which have page objects .

// To initialize LoginPage Objects or Elements
LoginPage loginpage = PageFactory.initElements(driver, LoginPage.class);

// Using Constructor
public LoginPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);