Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take 2 weeks Free 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. So for that, our test suite typically grows with 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, however, this would be time-consuming and challenging to maintain. Hence, it is recommended to use Page Object Model, where we can store our locators in one single place so that they don’t consume time when any changes are required.

Advantages of using the Page Object Model

  • Helps in easy maintenance of the test suite.
  • Develops scripts in a more readable format.
  • Makes scripts reusable.
  • Efficient and reliable.

Disadvantages of using the Page Object Model

  • It requires initial high efforts and time to set up the model.
  • Requires technical-sounding testers with functional programming practices.

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 web Elements. In short, we have created a function using the defined locators that would act like 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 we have used earlier with the “By” class. The difference that is to be noticed is only that we have used the @FindBy annotation to find the web elements. With each FindBy annotation, we have also used a WebElement interface reference to store the located element. As well, To initialize the web elements located using @FindBy, we have used PageFactory class with its predefined method entitlements.

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 which means the way we want to organize web locators in our Page class.

Summarizing it all!!

The POM with Selenium is classified into two approaches. The first one is the standard approach in which we used the “BY” class to store our locators. Another one is the page factory approach in which we use 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 Offshore Software Testing Company. So guys, give it a try, this is very useful in Selenium-based automation projects.

Software Development Team
Need Software Development Team?
captcha
🙌

Thank you!
We will contact soon.

Oops! Something went wrong.

Recent Blogs

Categories

NSS Note
Trusted by Global Clients