Introduction to Page Object Model (POM)

Page Object Model (POM) is one of the most common and popular design patterns used in automation testing. In the Page Object Model, each page in the web application is maintained as a separate class. All the actions that can be performed on that web page are defined as methods inside the class.

For example, if we are writing a script for the login page, a class will be created with the name LoginPage. Usually, in a login page, we have a username input, a password input, a login button, a forgot password button, and a sign-up link.

In the Page Object Model, inside the LoginPage class, a method is created for each user action. For example, to enter a username, a method will be created inside the LoginPage class.

Example Code for LoginPage.java

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LoginPage {
    WebDriver driver;
    public LoginPage(WebDriver driver) {
        this.driver = driver;
    }
    public void enterUsername(String usernameText) {
        driver.findElement(By.id("username")).sendKeys(usernameText);
    }
}


This enterUsername method enters the given text into the username field on the login page. This method is then called in the loginTest class. In the test class, we can pass different values to the method parameter to perform the action.

Example Code for LoginTests.java

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class LoginTests {
    WebDriver driver;
    LoginPage loginPage;
    
    @BeforeMethod
    public void setup() {
        driver = new ChromeDriver();
        driver.get("https://example.com/login");
        loginPage = new LoginPage(driver);
    }
    @Test
    public void validUsernameTest() {
        loginPage.enterUsername("john_doe");
    }
    @Test
    public void invalidUsernameTest() {
        loginPage.enterUsername("invalid_user@123");
    }
    @Test
    public void emptyUsernameTest() {
        loginPage.enterUsername("");
    }
    @AfterMethod
    public void tearDown() {
        driver.quit();
    }
}


Now you can see how the Page Object Model is useful.

The enterUsername method in the LoginPage class is used for both positive and negative test cases in the LoginTests class. We can pass a valid or invalid username through the method parameter. If the locator gets changed in the future, we only need to update it in one place, inside the LoginPage class.

Here are some of the major advantages of following the Page Object Model.

     It avoids code duplication (one method for both positive and negative cases).
     It enables easy maintenance (even if the locator changes, we need to update only one place).
     It allows easy grouping in tests.
     It improves readability.
     It makes debugging faster.
 

Related Tutorials

Related Questions






Read more