📄Page Object Model

Page Object Model is a design pattern in Selenium that defines page classes for each web page. It has the following benefits:

  1. Maintainability: If the UI of the web page changes, only the page object class needs to be updated, not the test scripts.

  2. Readability: The page object classes act as an abstraction layer, making the test scripts more readable.

  3. Reusability: The page object classes can be reused across multiple test scripts.

How it works:

  1. Create a page class for each web page.

This class will contain:

  • WebElements as fields

  • Methods to represent actions on that page

For example: LoginPage class for the login page will have:

public class LoginPage {
    
private WebElement usernameField;

private WebElement passwordField;

private WebElement loginButton;

public void enterUsername(String username) {
     usernameField.sendKeys(username);
}

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

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

}
  1. Initialize the page object in your test script:

LoginPage loginPage = new LoginPage(driver);
  1. Call the methods of the page object:

loginPage.enterUsername("john");
loginPage.enterPassword("pass123");
loginPage.clickLogin();
  1. If the UI of the login page changes, only the LoginPage class needs to be updated, not the test script.

Last updated