๐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:
Maintainability: If the UI of the web page changes, only the page object class needs to be updated, not the test scripts.
Readability: The page object classes act as an abstraction layer, making the test scripts more readable.
Reusability: The page object classes can be reused across multiple test scripts.
How it works:
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();
}
}
Initialize the page object in your test script:
LoginPage loginPage = new LoginPage(driver);
Call the methods of the page object:
loginPage.enterUsername("john");
loginPage.enterPassword("pass123");
loginPage.clickLogin();
If the UI of the login page changes, only the LoginPage class needs to be updated, not the test script.
Last updated
Was this helpful?