JavaScript Required

We're sorry, but we doesn't work properly without JavaScript enabled.

Looking for an Expert Development Team? Take two weeks Trial! Try Now

Page Object Model in Selenium Test Automation

Overview

pom selenium

As the name suggests, the Page object Model is a design pattern but not a framework. There’s a myth in the testing community that Page Object Model is a framework integrated with Selenium. However, it is a design pattern that provides a way to create an object repository of all web UI elements that would be used in creating an automation framework for our web application.

In-Page Object Model, we will be working on Pages, and the “Page” refers to Java classes, which would be storing web page corresponding locators and methods to perform operations on those web pages.

When we are working on a small test automation project, using Page Object Model doesn’t matter, however, when we are working on test automation for a large growing project, it is highly recommended to use Page Object Model. For large projects, our test suite typically grows with the time. Hence it would become difficult to maintain those test scripts if we are not following the Page Object Model.

Example: We have 20 scripts in our test suite for 20 different test cases, but they do have common web elements interaction. Any change in these elements will force us to change the locators in all 20 scripts, which would be time consuming and challenging to maintain. Hence, it is recommended to use Page Object Model, where we can store our locators at one single place so that they don't consume time when any changes are required.

Advantages of using the Page Object Model

Disadvantages of using the Page Object Model

Sample code of Object Repository

import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.interactions.Actions; public class ObjectRepo { WebDriver driver; By searchButton = By.id("ac-gn-link-search"); By searchInput = By.id("ac-gn-searchform-input"); public ObjectRepo(WebDriver driver) { this.driver = driver; } public void airpodsPage(String appleInput) throws InterruptedException { driver.findElement(searchButton).click(); Thread.sleep(2000); driver.findElement(searchInput).sendKeys(appleInput); Actions action = new Actions(driver); action.sendKeys(Keys.ENTER).build().perform(); } }

Code Walkthrough

The above code refers to the object repository where we have stored our web UI locators for our different test cases. For this, we have used Selenium predefined class “By” to contain webElements. Along with this, we have created a function using the defined locators that would act as a bunch of operations performing on a web application.

Sample Test Cases using the above Object Repository

import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import junit.framework.Assert; public class AppleTest { WebDriver driver; ObjectRepo obj; @BeforeMethod public void setup() { System.setProperty("webdriver.chrome.driver", "C:\\Users\\Lenovo-I7\\Downloads\\chromedriver.exe"); driver = new ChromeDriver(); driver.get("https://www.apple.com/"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); } @Test(priority = 1) public void airpod() throws InterruptedException { obj = new ObjectRepo(driver); obj.airpodsPage("airpods"); String airpodTitle = driver.getTitle(); System.out.println(airpodTitle); Assert.assertEquals(airpodTitle, "airpods - Apple"); } @Test(priority = 2) public void ipad() throws InterruptedException { obj = new ObjectRepo(driver); obj.airpodsPage("ipad"); String ipadTitle = driver.getTitle(); System.out.println(ipadTitle); Assert.assertEquals(ipadTitle, "ipad - Apple"); } @AfterMethod public void tearDown() { driver.quit(); } }

Code Walkthrough

In the above code, we have created two test cases. We have used BeforeMethod annotation to set up the browser configuration and AfterMethod annotation to quit the browser after the execution of each test case. In both the test cases, there are few common web operations; hence, using Page Object Model in such instances becomes necessary else it would lead to code redundancy. In both the test cases, we have called out the function that we have created in an Object Repository class. If any of the locators are to be changed in the future, we can make a quick change in Object Repository class in no time.

Page Object Model with Page Factory

Page Factory Class is another way of implementing the Page Object Model. Even here, our focus is to maintain separate classes for our locators and test cases. In this, we make use of @FindBy annotation to find our web elements. To initialize web elements, Page Factory class provides a method called initElements used along with parameters i.e., WebDriver reference and class name storing the web locators.

@FindBy annotation can accept any Selenium locator as a property.

Sample Code for Object Repository using Page Factory Class

package MyPackage; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; public class ObjectRepo { WebDriver driver; @FindBy(id = "ac-gn-link-search") WebElement searchButton; @FindBy(id = "ac-gn-searchform-input") WebElement searchInput; public ObjectRepo(WebDriver driver) { this.driver = driver; PageFactory.initElements(driver, this); } public void airpodsPage(String appleInput) throws InterruptedException { searchButton.click(); Thread.sleep(2000); searchInput.sendKeys(appleInput); Actions action = new Actions(driver); action.sendKeys(Keys.ENTER).build().perform(); } }

Code Walkthrough

The above code refers to the Object Repository of our test cases. The code is more or less similar to the one which we have used earlier with “By” class. The difference that is to be noticed is only that we have used @FindBy annotation to find the web elements. With each FindBy annotation, we have also used a WebElement interface reference to store the located element. To initialize the web elements located using @FindBy, we have used PageFactory class with its predefined method initElements.

Sample Test Cases using the above Object Repository

import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import junit.framework.Assert; public class AppleTest { WebDriver driver; ObjectRepo obj; @BeforeMethod public void setup() { System.setProperty("webdriver.chrome.driver", "C:\\Users\\Lenovo-I7\\Downloads\\chromedriver.exe"); driver = new ChromeDriver(); driver.get("https://www.apple.com/"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); } @Test(priority = 1) public void airpod() throws InterruptedException { obj = new ObjectRepo(driver); obj.airpodsPage("airpods"); String airpodTitle = driver.getTitle(); System.out.println(airpodTitle); Assert.assertEquals(airpodTitle, "airpods - Apple"); } @Test(priority = 2) public void ipad() throws InterruptedException { obj = new ObjectRepo(driver); obj.airpodsPage("ipad"); String ipadTitle = driver.getTitle(); System.out.println(ipadTitle); Assert.assertEquals(ipadTitle, "ipad - Apple"); } @AfterMethod public void tearDown() { driver.quit(); } }

Code Walkthrough

In the above code, we have created two test cases similar to the one created in the standard approach of the Page Object Model. As mentioned above, the difference between both methods comes only in Page Object Repository that means the way we want to organize web locators in our Page class.

Summarizing it all!!

Page Object Model with Selenium is categorized into two approaches, and one is the standard approach in which we have used the “By” class to store our locators. The second one is the Page Factory approach in which we have used the @FindBy annotation to find and store locators. The purpose of both methods is the same that is to create separate class files for web UI locators and test cases. Page Object Model plays a vital role in Test Automation of large growing projects in any Software Testing Company. So guys, give it a try, this is very useful in Selenium-based automation projects.

Good Luck & Happy Testing :)

Learn How to do Unit Test JavaScript in the Microsoft Dynamics 365 Using XrmPage-Mock Framework

NSS Note

Some of our clients

team