Friday, 5 August 2016

Page Factory for Selenium POM Frameworks

What is Page Factory (PF) ?

Page Factory is an inbuilt optimized page object model concept for Selenium. Page factory Class is derived from Page Object Pattern. It can be used in any kind of framework like Data driven, modular & keyword.

Advantages of PF

1. It finds WebElement every time it comes. Hence, no StaleElementExceptions should come.
2. Also we should not see NoSuchElementExceptions.
3. Helps in separating Page Object Repository & Test Methods.

How to Use PF ?

- @FindBy annotation is used to find WebElement.
- initElements method is used to initialise web elements.
-@FindBy can accept tagName, partialLinkText, name, linkText, id, css, className, xpath as attributes.

Example for using Page Factory :

Page Factory class would look like this :

Also you need to initiate this PF class in the constructor of your page object class, as follows :

PF actually keeps all the elements in cache which helps in easy & fast access to web elements hence it increases the speed of automation scripts as well.