Enhanced POM – Page Factory in Selenium WebDriver

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”:

@FindBy (how = How.ID, using = "twotabsearchtextbox")
public WebElement searchBox;

Example: Passing  Identifiers directly:

@FindBy (id = "twotabsearchtextbox")
public WebElement searchBox;

@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:

@FindBy (tagName="a")
private List<WebElement> allLinks;

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

PageFactory.initElements(Driver, this);

//Driver is the instance of the browser
//this stands for current page

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

@FindBy(id="gh-cat")
@CacheLookup
private WebElement selectBox;

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).

package designPatterns;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;

public class EbayPOM3Style {
	
	@FindBy(id="gh-ac")
	private WebElement searchBox;
	
	@FindBy(id="gh-cat")
	private WebElement selectBox;
	
	@FindBy(id="gh-btn")
	private WebElement searchButton;
	
	private Select dropdown;
	
	public EbayPOM3Style(WebDriver Driver){
		
		PageFactory.initElements(Driver, this);
		
		dropdown = new Select(selectBox);
	}
	
	public void searchProduct(String product, String category){
		
		searchBox.sendKeys(product);
		dropdown.selectByVisibleText(category);
		searchButton.click();
	}
}

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

package designPatterns;

import commonLibs.CommonDriver;

public class DemoEbayPOM3Style {

	public static void main(String[] args) {
		CommonDriver Driver = new CommonDriver();
		
		Driver.setPageLoadTimeout(90l);
		Driver.setElementDetectionTimeout(30l);
		Driver.openBrowser("chrome", "http://ebay.in");
		
		EbayPOM3Style homePage = new EbayPOM3Style(Driver.getDriver());
		
		homePage.searchProduct("Apple Watch", "Watches");
	}

}

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

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: