Enhanced POM – Page Factory in Selenium WebDriver

December 29, 2016

Enhanced POM Design Pattern using Page Factory

Page Factory is an enhanced way of writing POM design pattern. For a better understanding of POM using Page Factory, I will recommend you to go through Page Object Model Concepts first.

In Page Factory, Web Elements are identified using an annotation called @FindBy. It supports all the Identifiers or locators (Id, Name, Classname, Tagname, CSS Selector, Link text, Partial Link Text and XPath).

POM using Page factory

Enhanced way of designing Page Object Model design pattern using Selenium WebDriver

@FindBy annotation uses either “How” and “using” or directly locators and their values are passed. See below examples for better understanding.

Example: using “How” and “Using”:

Example: Passing  Identifiers directly:

@FindBy annotation can also be used to Identify multiple WebElements on a page. Consider a scenario where we are searching for all the links on a page.

Example: Identify Multiple Elements using @FindBy annotations:

To Initialise the Web Elements, a method called initElements() is used and can be done in below ways:

Benefits of using Page Factory:

With Dynamic Webpages:

Web Elements of a website, are initialized when a Web Page is loaded. There are some Web Elements which take extra time to appear on the page. If some operation is performed on those Web Elements, and they are not available. In this scenario, Page Factory will search for those Web Elements at the time of Interaction every time. This solves the problem of StateElementExceptions.

With Static Webpages:

Some web pages which are static (where web elements do not change). For those web pages, it is not necessary to search web elements again and again if there are multiple operations to be performed.

Here, page factory provides cache management mechanism. An annotation called @CacheLookup keeps the location of Web Elements in a cache. There is no need to search that WebElement again. See the below code for implementation

Implementation of Page Factory on a Web Page:

For Demo purpose, let us consider a scenario. On Ebay site we have to search a product, choose the category and click on search button. Let us capture this using enhanced POM (Page Factory).

To execute the scenario, let us define the main class:

For any questions, queries or comments feel free to write us at support@qatechhub.com or saurabh@qatechub.com. Happy Learning 🙂

Saurabh

About the Author

Saurabh

Follow Saurabh:

Leave a Comment: