Udemy REST Assured free

Page Object Model with Selenium PageFactory

A page object is an object-oriented class that serves as an interface to a page of your AUT (Application Under Test). 

Page Object Model is a design pattern to create Object Repository for web UI elements. Under this model, for each web page in the application, there should be corresponding page class. This Page class will find the WebElements of that web page and also contains Page methods which perform operations on those WebElements.

This class can be reused in all the scripts using that element. Suppose you are using same element in 10 different class and In future, if there is a change in the web element, The chief problem with script maintenance is that if 10 different scripts are using the same page element, with any change in that element. You need to change all 10 scripts. This is time consuming and error prone.

But if you are using PageFactory, you don't need to modify at 10 places. You only need to make the change in just 1 class file and not 10 different classes.

Benefits of POM

  • Project Manageable and maintainable and Reusability. Any change in UI can easily be implemented, updated and maintained into the Page Objects and Classes. Methods get more realistic names which can be easily mapped with the operation happening in UI.
  • Page Object Repository is Independent of Automation Tests. so we can use the same object repository for a different purpose with different tools. For example, we can integrate POM with TestNG/JUnit for functional Testing and at the same time with JBehave/Cucumber for acceptance testing.

Ways to implement Page Object Model

There are 2 different ways to implement POM:

1. Regular Java Classes: Which is not better way, Selenium offer us an easy solution for this, Which is PageFactory.
2. Selenium PageFactory

Simple Page Object w/o PageFactory

public class Search 
{
	WebDriver driver;
	
	By searchText = By.id("search_query_top");
	By searchBtn = By.name("submit_search");
	
	public Search(WebDriver driver)
	{
		this.driver = driver;
	}
	
	public void searchText(String text)
	{
		driver.findElement(searchText).sendKeys(text);
		driver.findElement(searchBtn).click();
	}
}

How to implement Selenium PageFactory

PageFactory has in build Page Object Model concept. It is optimized and easy to implement. Using PageFactory elements are initialized. We use @FindBy annotation to achieve this. Different attributes which FindBy accepts:

  • TagName
  • partialLinkText
  • Name
  • LinkText
  • Id
  • CSS
  • ClassName
  • XPath

You also need to use the constructor of class to initialize elements. Below is sample snippet of above example with PageFactory:

public class Search 
{
	WebDriver driver;
		
	@FindBy(id="search_query_top")
	public WebElement searchText;
	
	@FindBy(name="submit_search")
	public WebElement searchBtn;
	
	public Search(WebDriver driver)
	{
	    PageFactory.initElements(driver, this);
	}
	
	public void searchText(String text)
	{
		searchText.sendKeys(text);
		searchBtn.click();
	}
}

Using Class AjaxElementLocatorFactory

AjaxElementLocatorFactory is a lazy load concept in Page Factory pattern to identify WebElements only when they are used in any operation i.e. a timeOut for a WebElement can be assigned to the Object page class with the help of AjaxElementLocatorFactory.

public Search(WebDriver driver)
{
	AjaxElementLocatorFactory factory = new AjaxElementLocatorFactory(driver, 30);
	PageFactory.initElements(factory, this);
}

The above code will wait for maximum of 30 seconds until the elements specified by annotations is loaded. If the element is not found in the given time interval, it will throw NoSuchElementException' exception.

Video Tutorial: Page Object Model using Selenium PageFactory