Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

What is Page Object Model (POM) in Selenium Java?

Posted on July 20, 2025July 20, 2025 By Admin No Comments on What is Page Object Model (POM) in Selenium Java?

The Page Object Model (POM) is a design pattern used in Selenium test automation projects. It encourages the creation of an object repository for web UI elements, where each page of the application under test is represented as a separate Java class in code. This design leads to more readable, maintainable, and reusable automation code.

Key Concepts of POM

  • Separation of Concerns: POM separates test scripts from the details of web page structure and element locators by encapsulating them inside classes.
  • Encapsulation: Each page class contains:
    • Variables storing web element locators (fields).
    • Methods that define interactions or actions on those elements (like clicking buttons or entering text).
  • Reusability: Code for web element interactions can be reused across different test cases.
  • Maintainability: If a page changes, only the corresponding page class needs to be updated, minimizing the impact on test code.

Folder and File Structure of POM in Selenium Java for Login Form Test

SeleniumPOMProject/
│
├── src/
│   └── main/
│       └── java/
│           └── pages/
│               └── LoginPage.java
│
├── src/
│   └── test/
│       └── java/
│           └── tests/
│               └── LoginTest.java
│
├── pom.xml (for Maven dependencies)

Setup with Maven (pom.xml)

If you’re using Maven (recommended), include this in your pom.xml:

<dependencies>
    <!-- Selenium Java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.15.0</version>
    </dependency>

    <!-- WebDriver Manager for automatic driver setup -->
    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>5.8.0</version>
    </dependency>

    <!-- TestNG (for test framework, optional) -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.9.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Files to Test the Login Form

Filename: LoginPage.java, Location: src/main/java/pages/LoginPage.java

package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class LoginPage {
    WebDriver driver;

    // Element locators
    By usernameField = By.id("username");
    By passwordField = By.id("password");
    By loginButton = By.id("login");

    // Constructor to initialize WebDriver
    public LoginPage(WebDriver driver) {
        this.driver = driver;
    }

    // Actions (methods)
    public void enterUsername(String username) {
        driver.findElement(usernameField).sendKeys(username);
    }

    public void enterPassword(String password) {
        driver.findElement(passwordField).sendKeys(password);
    }

    public void clickLogin() {
        driver.findElement(loginButton).click();
    }

    // Composite method (optional)
    public void loginToApplication(String username, String password) {
        enterUsername(username);
        enterPassword(password);
        clickLogin();
    }
}

Filename: LoginTest.java, Location: src/test/java/tests/LoginTest.java

package tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
import pages.LoginPage;

public class LoginTest {
    public static void main(String[] args) {

        // Setup ChromeDriver using WebDriverManager
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();

        // Maximize and go to login page
        driver.manage().window().maximize();
        driver.get("https://example.com/login"); // Replace with actual site

        // Create LoginPage object
        LoginPage loginPage = new LoginPage(driver);

        // Perform login actions
        loginPage.enterUsername("myUsername");
        loginPage.enterPassword("myPassword");
        loginPage.clickLogin();

        // OR use combined method:
        // loginPage.loginToApplication("myUsername", "myPassword");

        // Perform validations or interactions after login...

        // Close browser
        driver.quit();
    }
}

Advantages of this Structure

  • LoginPage.java: Encapsulates all locators and methods related to the login page.
  • LoginTest.java: Contains only test logic—easy to read and maintain.
  • You can easily scale this by adding more page classes (HomePage.java, DashboardPage.java, etc.).

How to Run?

  • Use a Java IDE (like IntelliJ or Eclipse), open the Maven project.
  • Right-click on LoginTest.java and select Run.
  • Make sure you update the selector IDs and URL with your actual application values.

Benefits of Using POM

  • Reduces code duplication: Element locators and page actions are defined in one place only.
  • Improves test readability: Test scripts look like business scenarios, not technical steps.
  • Ease maintenance: Changes in UI affect only the relevant page class, not every test.
  • Supports scaling: Works well for large projects and teams working on complex application flows.

Best Practices

  • Name page classes clearly (e.g., LoginPage, DashboardPage).
  • Keep page classes focused—only methods and elements specific to a page.
  • Use meaningful method names to describe actions (e.g., clickLoginButton(), enterUsername()).
  • Combine with frameworks (like TestNG or JUnit) for test organization and execution.

Summary

The Page Object Model in Selenium Java is a design approach where each application page is represented by a Java class organizing element locators and actions. It provides a clean, maintainable, and scalable solution to automated web testing.

Selenium Tags:Selenium-Java

Post navigation

Previous Post: How to Handle Popup in Selenium Java?
Next Post: Difference between findElement() and findElements() in Selenium Java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • CSS
  • HTML
  • Interview Experience
  • Java
  • JavaScript
  • Lodash
  • PHP
  • Programs
  • Python
  • Selenium
  • Software Testing
  • Web Technologies
  • Web Templates

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme