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

The Page Object Model (POM) is a popular design pattern in Selenium that helps in creating an object repository for web UI elements. It enhances test maintenance and reduces code duplication. Below is an in-depth explanation with examples of how to implement POM by creating Page Classes in Selenium with Java.

1. Setting Up Your Project

First, you need to set up your Selenium project. You can use tools like Maven or Gradle for dependency management.

Maven Dependency

Add the following dependencies to your ‘pom.xml‘ file:

				
					<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.4.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

				
			

2. Creating Page Classes

In POM, each web page in the application is represented by a class. These classes contain web elements and methods to interact with them.

Example: Login Page

LoginPage.java

				
					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;

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

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

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

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

    // Methods to interact with the elements
    public void setUsername(String username) {
        usernameField.sendKeys(username);
    }

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

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

    public String getLoginPageTitle() {
        return driver.getTitle();
    }
}

				
			

3. Creating Test Classes

The test classes will use the Page Classes to perform operations.

Example: LoginTest.java

LoginTest.java

				
					package com.example.tests;

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

public class LoginTest {

    WebDriver driver;
    LoginPage loginPage;

    @BeforeClass
    public void setup() {
        // Set path for the ChromeDriver
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        
        // Initialize WebDriver
        driver = new ChromeDriver();
        
        // Open the application
        driver.get("https://example.com/login");
        
        // Initialize the LoginPage object
        loginPage = new LoginPage(driver);
    }

    @Test
    public void testLogin() {
        // Set username and password
        loginPage.setUsername("testuser");
        loginPage.setPassword("testpassword");
        
        // Click the login button
        loginPage.clickLoginButton();
        
        // Validate login success
        String expectedTitle = "Dashboard";
        String actualTitle = loginPage.getLoginPageTitle();
        Assert.assertEquals(actualTitle, expectedTitle, "Login test failed");
    }

    @AfterClass
    public void tearDown() {
        // Close the browser
        driver.quit();
    }
}

				
			

4. Running the Tests

Use your IDE or build tool to run the test class. For example, with Maven, you can use the following command:

				
					mvn test

				
			

5. Enhancing POM with PageFactory

The PageFactory class in Selenium provides an easier way to initialize web elements. It helps in reducing the boilerplate code.

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

				
			

6. Additional Tips

  • BasePage Class: Create a BasePage class for common functionalities (e.g., navigation methods, common assertions).
  • Utility Classes: Create utility classes for common actions (e.g., wait utilities, browser actions).
  • Test Data Management: Use external files (e.g., Excel, JSON) to manage test data.

7. Best Practices

  • Single Responsibility Principle: Each page class should handle actions specific to that page.
  • Readability: Keep your page and test classes clean and readable.
  • Reusability: Create reusable methods to avoid code duplication.
  • Maintainability: Structure your project to facilitate easy maintenance.
Scroll to Top