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 design pattern in Selenium that enhances test maintenance and reduces code duplication. This pattern separates the test logic from the UI representation, making the tests more readable and easier to manage.

Benefits of Using POM in Selenium Java

1. Code Reusability:

  • By creating a separate class for each page of the application, POM promotes code reusability. Each class encapsulates the UI elements and interactions specific to that page, making it easy to reuse these components across multiple tests.

2. Maintainability:

  • Changes in the UI only need to be updated in one place, reducing the maintenance overhead. For instance, if an element locator changes, only the corresponding Page Object class needs to be updated, not all the tests that use it.

3. Readability:

  • POM enhances the readability of test scripts by abstracting the page details into a separate layer. This makes the test cases easier to understand and manage.

4. Separation of Concerns:

  • It clearly separates the test code from the code that interacts with the UI, adhering to the Single Responsibility Principle. This makes the test framework more modular and scalable.

5. Improved Test Organization:

  • Organizing code into Page Objects helps in structuring the test framework in a logical manner. It provides a clean architecture for the automation codebase.

6. Ease of Parallel Testing:

  • POM can be combined with test frameworks like TestNG or JUnit to run tests in parallel, improving execution time and efficiency.

Example of POM in Selenium Java

Let’s consider a simple example of testing a login functionality for a web application.

1. Creating Page Object Class for Login Page

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

public class LoginPage {
    WebDriver driver;

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

    // Locators for the elements
    By username = By.id("username");
    By password = By.id("password");
    By loginButton = By.id("loginButton");

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

    public void setPassword(String pass) {
        driver.findElement(password).sendKeys(pass);
    }

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

				
			

2. Creating the Test Class

				
					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;

public class LoginTest {
    WebDriver driver;
    LoginPage loginPage;

    @BeforeClass
    public void setUp() {
        // Set up the Chrome driver
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
        driver.get("http://example.com/login");
        loginPage = new LoginPage(driver);
    }

    @Test
    public void testLogin() {
        // Perform login
        loginPage.setUsername("testuser");
        loginPage.setPassword("testpassword");
        loginPage.clickLoginButton();
        
        // Assert the result after login (e.g., check if login was successful)
        // This can include checking the URL, presence of certain elements, etc.
    }

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

				
			

3. Advanced Example with Page Factory

Page Factory is an extension of POM, which provides an easier way to initialize the web elements.

Updated LoginPage with Page Factory

				
					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 to initialize the driver and elements
    public LoginPage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    // Using @FindBy annotation to locate elements
    @FindBy(id = "username")
    WebElement username;

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

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

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

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

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

				
			

In the test class, the initialization remains the same as before. This approach reduces boilerplate code and makes the Page Object class more readable.

Using POM in Selenium Java provides a structured way to manage web elements and interactions, improving code reusability, maintainability, and readability. It is an essential design pattern for any robust and scalable test automation framework.

Scroll to Top