Page Object Model and Page Factory in Selenium Using Java
Last Updated :
18 Jul, 2025
Page Object Model (POM) and Page Factory are design patterns that enhance the maintainability, reusability, and readability of test automation code.
What is Page Object Model in Selenium?
Page Object Model (POM) is a design pattern in Selenium that helps organize and manage the web elements and actions associated with a particular web page or component. The primary goal of POM is to enhance maintainability, reusability, and readability of your test automation code by promoting a clear separation between the test scripts and the underlying page details.
Advantages of POM
Here are the Advantages of the Page Object Model.
- Code Reusability: Encapsulate web elements and actions in page classes for reuse across test cases, minimizing duplication.
- Code Maintainability: Changes to UI elements are confined to their respective page classes, simplifying updates and reducing errors.
- Separation of Concerns: Keep test scripts focused on logic while page classes manage UI interactions, enhancing clarity.
- Readability: Use descriptive method names in page classes to make test scripts intuitive for testers and developers.
- Enhanced Test Structure: Organize scripts by page to mirror the application’s structure, improving navigation.
- Improved Collaboration: Clear separation fosters better communication between testers and developers.
Implementing Page Object Model (POM) in Selenium
To implement POM, follow these steps:
- Create a separate class for each web page.
- Define web elements and actions within these classes.
- Use these page classes in your test scripts.
1. Set Up Your Eclipse Project
Create a new Maven project in Eclipse and configure the pom.xml file with Selenium and TestNG dependencies for effective test automation.
XML
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.25.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
2. Create a Package for Page Objects
To create a new package in Eclipse, right-click src/main/java in Package Explorer, select New > Package, name it pages, and click Finish.
3. Create a Page Class
Right-click on the pages package, select New > Class, and name it LoginPage and define web elements and methods to interact with them.
LoginPage.java (POM Implementation):
Java
package pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class LoginPage {
private WebDriver driver;
private final String url = "https://www.saucedemo.com/";
// Locators
private By usernameField = By.id("user-name");
private By passwordField = By.id("password");
private By loginButton = By.id("login-button");
private By errorMessage = By.cssSelector("h3[data-test='error']");
// Constructor
public LoginPage(WebDriver driver) {
this.driver = driver;
}
// Navigate to login page
public void navigateTo() {
if (!driver.getCurrentUrl().equals(url)) {
driver.get(url);
}
}
// Perform login action
public void login(String username, String password) {
driver.findElement(usernameField).sendKeys(username);
driver.findElement(passwordField).sendKeys(password);
driver.findElement(loginButton).click();
}
// Get error message (for invalid login)
public String getErrorMessage() {
return driver.findElement(errorMessage).getText();
}
}
4. Create a Package for Tests
Right-click on src/test/java, select New > Package, name it tests, and click Finish.
5. Create a Test Class
Right-click on the tests package, select New > Class, and name it LoginPageTest and write test methods using the LoginPage class.
LoginPageTest.java (POM Test Class):
Java
package tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import pages.LoginPage;
public class LoginPageTest {
private WebDriver driver;
private LoginPage loginPage;
@BeforeMethod
public void setUp() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\path of chromedriver\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
loginPage = new LoginPage(driver);
}
@Test
public void testValidLogin() {
loginPage.navigateTo();
loginPage.login("standard_user", "secret_sauce");
// Verify successful login by checking URL
String expectedUrl = "https://www.saucedemo.com/inventory.html";
Assert.assertEquals(driver.getCurrentUrl(), expectedUrl, "Login failed: URL mismatch");
}
@Test
public void testInvalidLogin() {
loginPage.navigateTo();
loginPage.login("invalid_user", "wrong_password");
// Verify error message
String expectedError = "Epic sadface: Username and password do not match any user in this service";
Assert.assertEquals(loginPage.getErrorMessage(), expectedError, "Error message mismatch");
}
@AfterMethod
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
6. Run Your Tests
Right-click on LoginPageTest.java, select Run As > TestNG Test. Verify results in the Eclipse console or TestNG reports to ensure tests executed successfully.
Page Object Model OutputWhat is Page Factory in Selenium?
Page Factory is a design pattern in Selenium used to create Page Object Model (POM) implementations more efficiently. It is part of the Selenium library and offers a way to initialize web elements in a Page Object class with less boilerplate code.
Advantages of Page Factory
- Automatic Element Initialization: Simplifies and reduces the need for repetitive
findElement
calls. - Cleaner Code: Enhances readability and maintainability by separating locators from test logic.
- Improved Organization: Keeps locators organized within page classes.
- Enhanced Maintainability: Centralizes updates to locators, reducing changes in test scripts.
- Efficient Resource Usage: Promotes lazy loading of elements, improving performance.
Implementing Page Factory in Selenium
We use the same Project for the implementation of the page factory using selenium. Implement a LoginPageFactory class within the package.
Java
package pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class LoginPageFactory {
private WebDriver driver;
private final String url = "https://www.saucedemo.com/";
// Web elements using @FindBy
@FindBy(id = "user-name")
private WebElement usernameField;
@FindBy(id = "password")
private WebElement passwordField;
@FindBy(id = "login-button")
private WebElement loginButton;
@FindBy(css = "h3[data-test='error']")
private WebElement errorMessage;
// Constructor
public LoginPageFactory(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
// Navigate to login page
public void navigateTo() {
if (!driver.getCurrentUrl().equals(url)) {
driver.get(url);
}
}
// Perform login action
public void login(String username, String password) {
usernameField.sendKeys(username);
passwordField.sendKeys(password);
loginButton.click();
}
// Get error message (for invalid login)
public String getErrorMessage() {
return errorMessage.getText();
}
}
Create a class named LoginPageFactoryTest.
Java
package pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class LoginPageFactoryTest {
private WebDriver driver;
private LoginPageFactory loginPage;
@BeforeMethod
public void setUp() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\change the path of the chromedriver\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
loginPage = new LoginPageFactory(driver);
}
@Test
public void testValidLogin() {
loginPage.navigateTo();
loginPage.login("standard_user", "secret_sauce");
// Verify successful login by checking URL
String expectedUrl = "https://www.saucedemo.com/inventory.html";
Assert.assertEquals(driver.getCurrentUrl(), expectedUrl, "Login failed: URL mismatch");
}
@Test
public void testInvalidLogin() {
loginPage.navigateTo();
loginPage.login("invalid_user", "wrong_password");
// Verify error message
String expectedError = "Epic sadface: Username and password do not match any user in this service";
Assert.assertEquals(loginPage.getErrorMessage(), expectedError, "Error message mismatch");
}
@AfterMethod
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
Select Run As > TestNG Test and verify results in the Eclipse console or TestNG reports.
Output:
Output of Page Factory Using SeleniumPage Object Model Vs. Page Factory in Selenium
Aspect | Page Object Model (POM) | Page Factory |
---|
Initialization | Uses traditional methods to initialize web elements. | Utilizes PageFactory.initElements for initializing web elements. |
---|
Element Locators | Web elements are typically located using methods like findElement. | Web elements are defined using @FindBy annotations. |
---|
Code Readability | Can be less concise due to manual element initialization. | More concise and cleaner due to annotation-based element definitions. |
---|
Performance | Web elements are loaded when the page class is instantiated. | Lazy initialization; elements are loaded only when they are used. |
---|
Ease of Maintenance | Requires manual updates to element locators and initialization. | Easier to maintain with annotations and centralized initialization. |
---|
Syntax | Standard Java syntax for defining and interacting with web elements. | Uses annotations (@FindBy) to define element locators. |
---|
Error Handling | Errors in element locating are more immediately visible. | May defer errors until elements are actually interacted with. |
---|
Test Script Structure | Typically requires more boilerplate code for element definitions. | Reduces boilerplate with annotation-based element initialization. |
---|
Use Case | Suitable for projects where explicit control over element initialization is needed. | Ideal for projects requiring cleaner and more concise code with annotation support. |
---|
Conclusion
In summary, Page Object Model (POM) and Page Factory are powerful design patterns in Selenium that enhance test automation efficiency. POM organizes web elements into page classes, improving maintainability and readability, while Page Factory simplifies their initialization with annotations. Adopting these patterns leads to modular, reusable, and scalable test scripts, making your test automation projects more effective and easier to manage.
Similar Reads
Get contents of entire page using Selenium In this article, we will discuss ways to get the contents of the entire page using Selenium. There can broadly be two methods for the same. Let's discuss them in detail. Method 1: For extracting the visible text from the entire page, we can use the find_element_by_* methods which help us find or loc
2 min read
How to Save a Web Page with Selenium using Java? Selenium is widely known for automating browser interactions, but it can also be used to save web pages directly. This capability is particularly useful when you need to archive web content, save dynamic pages that change frequently, or scrape and store HTML for later analysis. In this tutorial, weâ
3 min read
How to Run Opera Driver in Selenium Using Java? Selenium is a well-known software used for software testing purposes. Selenium consists of three parts. One is Selenium IDE, one is Selenium Webdriver & the last one is Selenium Grid. Among these Selenium Webdriver is the most important one. Using Webdriver online website testing can be done. Th
3 min read
How to scrape multiple pages using Selenium in Python? As we know, selenium is a web-based automation tool that helps us to automate browsers. Selenium is an Open-Source testing tool which means we can easily download it from the internet and use it. With the help of Selenium, we can also scrap the data from the webpages. Here, In this article, we are g
4 min read
Selenium Scrolling a Web Page using Java An open-source framework that is used for automating web applications is known as Selenium. Selenium handles various operations, like opening of website, clicking buttons, scrolling the webpage, etc. In this article, we have defined the numerous ways to scroll a webpage using Selenium in Java. Table
7 min read
Selenium Scrolling a Web Page using Java An open-source framework that is used for automating web applications is known as Selenium. Selenium handles various operations, like opening of website, clicking buttons, scrolling the webpage, etc. In this article, we have defined the numerous ways to scroll a webpage using Selenium in Java. Table
7 min read