Page Object Model - POM


In this tutorial, we will study about "Page Object Model", a design pattern in UI automation testing. Before starting with Page Object Model, let's first know what are design patterns.

Design pattern

A Design pattern is a generic solution to a common software design/architecture problem. Implementation of these design patterns leads to inclusion of best practices and best solution, evolved over the time by others while working with similar problems.

Page Object Model in Selenium

A Page Object Model is a design pattern that can be implemented using selenium webdriver. It essentially models the pages/screen of the application as objects called Page Objects, all the functions that can be performed in the specific page are encapsulated in the page object of that screen. In this way any change made in the UI will only affect that screens page object class thus abstracting the changes from the test classes.

Advantages of using Page Object Model

  • Increases code reusability - code to work with events of a page is written only once and used in different test cases
  • Improves code maintainability - any UI change leads to updating the code in page object classes only leaving the test classes unaffected
  • Makes code more readable and less brittle

Creating a Page Object Model in Java

Here, we'll create an automation framework implementing Page Object Model using Selenium with Java.
Suppose we have to test a dummy application with only a login page and a home page. To start with we first need to create page objects for all available pages in our application - LoginPage.java and HomePage.java. Then we will create a test class that will create instance of these page objects and invoke there methods to create tests.
Let's take the scenario where in the login page user enters valid credentials and on clicking submit button, user is redirected to home page.

Content of LoginPage.java

public class LoginPage {
	
    public LoginPage(WebDriver driver) {
        this.driver = driver;
    }

    //Using FindBy for locating elements
    @FindBy(id = "userName")
    private WebElement userName;

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

    @FindBy(id = "submitButton")
    private WebElement submit;

    /*Defining all the user actions that can be performed in the loginPage
    in the form of methods*/

    public void typeUserName(String text) {
        userName.sendKeys(text);
    }

    public void typePassword(String text) {
        password.sendKeys(text);
    }

    /*Take note of return type of this method, clicking submit will navigate
    user to Home page, so return type of this method is marked as HomePage.*/
    public HomePage clickSubmit() {
        submit.click();
        return new HomePage(driver);
    }

    public HomePage loginWithValidCredentials(String userName, String pwd) {
        typeUserName(userName);
        typePassword(pwd);
        return clickSubmit();
    }
}


Content of HomePagePage.java

public class HomePage {

    public HomePage(WebDriver driver) {
        this.driver = driver;
    }
	
    @FindBy(id = "userInfo")
    private WebElement userInfo;
	
    public void clickUserInfo(){
	userInfo.click();
    }
	
    public String showUserInfo(){
	String userData = clickUserInfo();
	return userData;
    }
}


Content of Test class - POMTest.java-

public class POMTest{
    
    //Create firefox driver's instance
    WebDriver driver = new FirefoxDriver();
	
    @Test
    public void verifyUserInfo() {

    //Creating instance of loginPage
    LoginPage loginPage = new LoginPage(driver);
	
    //Login to application
    HomePage homePage = loginPage.loginWithValidCredentials("user1","pwd1");
		
    //Fetch user info
    String userInfo = homePage.showUserInfo();
		
    //Asserting user info   
    Assert.assertTrue(userInfo.equalsIgnoreCase("XYZ"),"Incorrect userInfo");
    }
}

This was just a demo for understanding of Page Object Model, an actual project would require several updations like creating abstract classes for page objects, creating base classes for test classes, creating helper and utility classes for database connectivity, for passing test data through config files etc.