Page Object Model (POM) in Selenium with Java

 ๐Ÿ—️ Page Object Model (POM) in Selenium with Java

What is Page Object Model?

Page Object Model (POM) is a design pattern that helps organize and structure your Selenium test code. It creates an object repository for web UI elements and defines methods to interact with them, making your tests:


More maintainable


Easier to read


Reusable across tests


๐Ÿ”ง Step-by-Step Implementation

✅ 1. Project Setup

Required Tools:


Java (JDK 11 or later recommended)


Selenium WebDriver


Maven or Gradle (for dependency management)


TestNG or JUnit (for running tests)


Any IDE (like IntelliJ IDEA or Eclipse)


Sample pom.xml dependencies (Maven):


xml

Copy

Edit

<dependencies>

  <dependency>

    <groupId>org.seleniumhq.selenium</groupId>

    <artifactId>selenium-java</artifactId>

    <version>4.16.1</version>

  </dependency>

  <dependency>

    <groupId>org.testng</groupId>

    <artifactId>testng</artifactId>

    <version>7.9.0</version>

    <scope>test</scope>

  </dependency>

</dependencies>

✅ 2. Create Page Class (Example: LoginPage)

java

Copy

Edit

package pages;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;


public class LoginPage {

    WebDriver driver;


    // Locators

    By usernameField = By.id("username");

    By passwordField = By.id("password");

    By loginButton = By.id("loginBtn");


    // Constructor

    public LoginPage(WebDriver driver) {

        this.driver = driver;

    }


    // Page Actions

    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();

    }


    public void login(String username, String password) {

        enterUsername(username);

        enterPassword(password);

        clickLogin();

    }

}

✅ 3. Create Test Class

java

Copy

Edit

package tests;


import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.*;

import pages.LoginPage;


public class LoginTest {

    WebDriver driver;

    LoginPage loginPage;


    @BeforeMethod

    public void setUp() {

        driver = new ChromeDriver();

        driver.get("https://example.com/login");

        loginPage = new LoginPage(driver);

    }


    @Test

    public void testValidLogin() {

        loginPage.login("testuser", "password123");

        // Add assertions here

    }


    @AfterMethod

    public void tearDown() {

        driver.quit();

    }

}

๐Ÿง  Benefits of Using POM

Feature Benefit

Separation of concerns Keeps test logic separate from UI details

Easy maintenance Update locators in one place only

Reusability Use same page methods in multiple tests

Readability Clean and understandable test code


๐Ÿ› ️ Best Practices

Use PageFactory for better performance and readability (optional)


Keep locators private and expose only actions


Modularize reusable components (like headers or sidebars) into their own page objects

Learn Selenium JAVA Training in Hyderabad

Read More

Integrating Selenium with Maven and Jenkins for CI/CD

Creating a Selenium Framework from Scratch Using Java and TestNG

How to Take Screenshots in Selenium Automatically on Failure

Cross-Browser Testing with Selenium WebDriver

Visit Our Quality Thought Training in Hyderabad

Get Directions

Comments

Popular posts from this blog

Understanding Snowflake Editions: Standard, Enterprise, Business Critical

Installing Tosca: Step-by-Step Guide for Beginners

Why Data Science Course?