top of page

A Deeper Dive into the Page Object Model and Page Factory in Test Automation

Writer's picture: Harpreet KaurHarpreet Kaur

Automated testing can be an incredible tool in the software developer's toolbox. Selenium, in particular, is widely used for automating tests of web applications. To help make Selenium tests more readable and maintainable, we often use the Page Object Model (POM) and Page Factory design patterns. If these sound complex, don't worry; we're going to break them down with simple examples.


Exploring Page Object Model (POM)


Let's visualize a website as a city where each page is a building. The Page Object Model (POM) provides us with a blueprint of every building (page). In this blueprint, each interactive element (like text fields, buttons, links, etc.) on the page is laid out.

POM in Selenium represents each web page of the application as a separate class. The web elements are represented as instance variables in the class, and the actions or interactions are represented as methods.

For instance, consider a simple login page with fields for username, password, and a login button. Here's how you could represent it with POM:

public class LoginPage {

    // These are the elements we interact with on the Login Page
    private WebDriver driver;
    @FindBy(id="username") 
    WebElement username;
    
    @FindBy(id="password") 
    WebElement password;
    
    @FindBy(id="login") 
    WebElement loginButton;

    // Constructor to initialize the driver
    public LoginPage(WebDriver driver){
        this.driver = driver;
    }

    // Methods to interact with the elements
    public void setUserName(String strUserName){
        username.sendKeys(strUserName);     
    }
    
    public void setPassword(String strPassword){
        password.sendKeys(strPassword);
    }

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

    // Method to login by encapsulating the interactions
    public void loginToWebsite(String strUserName,String strPassword){
        this.setUserName(strUserName);
        this.setPassword(strPassword);
        this.clickLogin();        
    }
}

Getting Familiar with Page Factory

While POM gives us the blueprint of a page, Page Factory is the skilled worker who knows when and how to put the blueprint to work. Page Factory is an inbuilt POM concept in Selenium WebDriver that helps in initializing the elements of the Page Object.

One important feature of Page Factory is lazy initialization. Elements are only located when you perform operations on them, which can make your tests run faster and save memory.

Now, let's refactor our LoginPage class from above using Page Factory:


public class LoginPage_PF {

    // The elements on the page@FindBy(id="username") 
    WebElement username;
    
    @FindBy(id="password") 
    WebElement password;
    
    @FindBy(id="login") 
    WebElement loginButton;

    // Constructor to initialize the elements
      public LoginPage_PF(WebDriver driver){
       PageFactory.initElements(driver, this);
    }

    // Methods to interact with the elements
       public void setUserName(String un){
       username.sendKeys(un);
    }
    
       public void setPassword(String pw){
       password.sendKeys(pw);
    }

    public void clickLogin(){
       loginButton.click();
    }  
    
    // Method to login using the above interactions
    public void loginToWebsite(String un,String pw){
        this.setUserName(un);
        this.setPassword(pw);
        this.clickLogin();        
    }
}

Here, the @FindBy annotation helps Selenium find and initialize the web elements. The initElements method takes the driver and the current class instance and initializes the WebElement instances.

The Page Object Model and Page Factory in Action

Let's say, after logging in, we are directed to a home page. We can create a separate class for the home page and create methods for the possible interactions on that page. We then use these classes (Login Page and Home Page) in our tests.

Here's a simple test that uses the LoginPage_PF and a hypothetical HomePage_PF class:


public class TestLogin {

    WebDriver driver = new ChromeDriver();
    
    @Testpublic void successfulLogin() {
        driver.get("http://www.example.com/login");

        LoginPage_PF loginPage = new LoginPage_PF(driver);
        loginPage.loginToWebsite("username", "password");
        
        HomePage_PF homePage = new HomePage_PF(driver);
        Assert.assertTrue(homePage.isWelcomeMessageVisible());
    }
}

Conclusion

By using the Page Object Model and Page Factory in Selenium, you can create organized, robust, and maintainable automated tests. You can map your application in a way that's intuitive and reduces redundancy. So don't shy away from these design patterns; they are key to effectively automate your tests and make your test automation journey smoother. Remember, the more you practice, the more comfortable you'll get!

235 views

Recent Posts

See All

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page