Understanding Page Object Models in Selenium with Java

When working with a website, you'll often come across multiple pages—like a home page, login page, product page and checkout page. Now, imagine trying to write all your automated test scripts for these pages in one big file. It can quickly become overwhelming and hard to manage!
As your test cases grow, so does the complexity, and that’s where the Page Object Model (POM) in Selenium comes in!
What Is the Page Object Model (POM)?
The Page Object Model is a design pattern used in Selenium test automation. It helps keep your code organized by creating a separate class for each web page in your application.
Instead of cramming all your locators(web-elements) and actions into a single file, POM encourages you to create a separate class for each web page. Each class handles its own web elements and actions, making your test scripts much easier to work with.
The web elements (like input fields, buttons, and links),the actions you can perform on them (such as clicking, entering text, moving to elements, checking visibility ), the instance field(s) as well as constructor(s) for the page class are all included in the page class.This way, all the logic related to a specific page lives in one place, enabling efficient management and code re-use across tests.
How It Works in Practice
Let’s use a basic test automation website to illustrate using POM in our java selenium automation:
Suppose, we want to write a login function test for the website: www.saucedemo.com, we would have two classes created — the first is the login page, in other words, the POM page class, followed by the test page —the login test class. The POM class page will contain the web-elements of the login page as well as the methods calling the web-elements to action as described below:
POM class - LoginPage.java
public class LoginPage {
import org.openqa.selenium.Keys;
//Instance fields
WebDriver driver;
String url;
//Constructor
public LoginPage(WebDriver driver, String website) {
PageFactory.initElements(driver, this);
url = website;
driver.get(url);
}
// Page Elements
@FindBy(id = "username")
WebElement usernameField;
@FindBy(id = "password")
WebElement passwordField;
@FindBy(id = "loginButton")
WebElement loginButton;
// Page Actions
public void enterUsername(String username) {
usernameField.sendKeys(username);
}
public void enterPassword(String password) {
passwordField.sendKeys(password);
}
public void clickLogin() {
loginButton.click();
}
}
Having declared the necessary fields and actions to be tested, next is to write the test using the operators and methods in the test class as written below:
Test class - LoginTest.java:
public class LoginTest extends LoginPage{
import org.testng.annotation.AfterTest;
import org.testng.annotation.BeforeTest;
import.org.openqa.selenium.WebDriver;
import.org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotation.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
@BeforeTest
public void setUp() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
LoginPage loginpage = new LoginPage(driver, “https://www.saucedemo.com”);
}
@Test
public void testLogin() {
loginPage.enterUsername("standard_user");
loginPage.enterPassword("secret_sauce");
loginPage.clickLogin();
}
@AfterTest
public void tearDown() {
driver.quit();
}
}
The above LoginTest class, has our test — @Test – written in not more than four lines of code and this makes our test script a lot more flexible!
Conclusion
When we make use of Project Object Models in our test scripts, we keep web elements and page actions separate from our test logic resulting to readable and reusable test scripts. Whether you're just starting out or looking to improve your existing test framework, adopting POM is a smart step toward building reliable and scalable automation tests.
Using POM in your test scripts helps you separate page elements and actions from your test logic, making your code cleaner, more readable and easier to reuse. Whether you’re just getting started or refining an existing test framework, adopting POM is a smart move toward more scalable and maintainable automation.
Here’s a good place to start: Apply POM to a simple test case to see how it works in action. The more you use it, the more you improve in it and appreciate its long-term benefits.

