Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

Page Object Model (POM) in Selenium Java

Page Object Model (POM) is a design pattern in Selenium that promotes maintainable, reusable, and readable automation code. In POM, each web page in the application is represented as a class, and the elements on the page are defined as variables within the class. The methods in the class perform operations and validations on those elements.

Advantages of POM

  1. Reusability: Common operations can be reused across different test cases.
  2. Maintainability: If the UI changes, only the page class needs to be updated.
  3. Readability: Test scripts are cleaner and easier to understand.
  4. Separation of Concerns: Separates test logic from the UI operations.

Structure of POM

  1. Page Classes: Classes representing web pages. These contain locators and methods for interacting with the elements.
  2. Test Classes: Classes containing the test cases, making use of methods from the Page classes.

Example of POM in Selenium Java

Let’s consider a simple web application with a login page. We’ll create a Page Object Model for the login page.

1. Setting Up Dependencies

First, ensure you have the necessary dependencies in your ‘pom.xml‘ (for Maven) or add jars to your project if not using Maven.

				
					<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.x.x</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.x.x</version>
        <scope>test</scope>
    </dependency>
</dependencies>

				
			

2. Creating the Page Class

				
					package com.example.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 LoginPage {
    WebDriver driver;

    // Locators
    @FindBy(id = "username")
    WebElement usernameField;

    @FindBy(id = "password")
    WebElement passwordField;

    @FindBy(id = "loginButton")
    WebElement loginButton;

    // Constructor
    public LoginPage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    // Actions
    public void setUsername(String username) {
        usernameField.sendKeys(username);
    }

    public void setPassword(String password) {
        passwordField.sendKeys(password);
    }

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

    public void login(String username, String password) {
        setUsername(username);
        setPassword(password);
        clickLogin();
    }
}

				
			

3. Creating the Test Class

				
					package com.example.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.example.pages.LoginPage;

public class LoginTest {
    WebDriver driver;
    LoginPage loginPage;

    @BeforeClass
    public void setUp() {
        // Set the path to the chromedriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
        driver.get("https://example.com/login");

        // Initialize the LoginPage
        loginPage = new LoginPage(driver);
    }

    @Test
    public void testLogin() {
        loginPage.login("testuser", "testpassword");
        // Add assertions to verify successful login
    }

    @AfterClass
    public void tearDown() {
        driver.quit();
    }
}

				
			

Explanation

 

  1. LoginPage Class: Represents the login page. It contains:

    • Locators for the username, password fields, and the login button using the @FindBy annotation.
    • Methods to perform actions like setting the username, setting the password, and clicking the login button.
    • A ‘login‘ method that combines these actions for easier test script usage.
  2. LoginTest Class: Contains the test case.

    • The ‘setUp‘ method initializes the WebDriver and opens the login page.
    • The ‘testLogin‘ method uses the ‘login‘ method from the ‘LoginPage‘ class to perform the login action.
    • The ‘tearDown‘ method closes the browser after the test.

The Page Object Model helps in organizing test scripts by separating page-specific operations from test logic. This makes the test scripts easier to maintain and enhances code reusability. By following this pattern, you can handle complex web applications more efficiently in your Selenium test automation projects.

Scroll to Top