The Page Object design pattern
dimanche 19 janvier 2025

Licence Creative Commons

The « Page Object » design pattern is a popular approach used to organize automated test code and help prevent maintainability issues.




The methods in a Page Object class represent the actions that can be performed on a page. A Page Object should provide mechanisms to interact with, observe, and locate web elements on the page.

An example implementation of the Page Object design pattern:

LoginTest.java

public class LoginTest extends BaseTest {

    @Test
    public void testLogin() {

        LoginPage loginPage = new LoginPage(webDriver);
        HomePage homePage = new HomePage(webDriver));

        loginPage.setUsername("administrator");
        loginPage.setPassword("password");
        loginPage.submit();

        if (homePage.isDisplayed()) {
            System.out.println("Login successful, home page displayed.");
        } else { 
            System.out.println("Login failed, home page not displayed.");
        }
    }
}

BaseTest.java

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.After;
import org.junit.Before;

public class BaseTest {

    protected WebDriver webDriver;

    @Before
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        webDriver = new ChromeDriver();
        webDriver.get("http://yourapp.com");
    }

    @After
    public void tearDown() {
        if (webDriver != null) {
            webDriver.quit();
        }
    }
}

LoginPage.java

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

public class LoginPage {

    private WebDriver webDriver;

    private By usernameField = By.id("username");
    private By passwordField = By.id("password");
    private By submitButton = By.id("loginButton");

    public LoginPage(WebDriver webDriver) {
        this.webDriver = webDriver;
    }

    public void setUsername(String username) {
        WebElement usernameElement = webDriver.findElement(usernameField);
        usernameElement.clear();
        usernameElement.sendKeys(username);
    }
    
    public void setPassword(String password) {
        WebElement passwordElement = webDriver.findElement(passwordField);
        passwordElement.clear();
        passwordElement.sendKeys(password);
    }

    public void submit() {
        WebElement submitButtonElement = webDriver.findElement(submitButton);
        submitButtonElement.click();
    }
}

HomePage.java

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

public class HomePage {

    private WebDriver webDriver;
    
    private By home= By.id("home");

    public HomePage(WebDriver webDriver) {
        this.webDriver = webDriver;
    }

    public boolean isDisplayed() {
        WebElement homePageWebElement = webDriver.findElement(home);
        return homePageWebElement.isDisplayed();
    }
}

The main benefits of this model are:

  • Reduces code duplication
  • Facilitates reusability of Page Object classes across different tests
  • Simplifies maintenance when the UI changes
  • Improves code readability and clarity


Note: A 'Page Object' class does not necessarily represent an entire page; it can also represent a significant element within a page, such as the header of a web application.



References: