top of page
Writer's pictureMonika Patil

Basic Understanding Of Page Object Model In Selenium Using Page Factory


Today we are going to see one of important topic in the selenium framework which is the “Page object model with page factory”. It makes a very important role while designing a testing framework for the project. In this blog, we are going to focus on :

>> Introduction of “Page Object Model” (POM)

>> Basic Rules of POM

>> Advantages of the Page Object Model

>> Basic Information of “Page Factory”

>> Different Types of Locators in Selenium

>> Creating a Page Object Model in Java

Introduction of “Page Object Model” (POM)

Page Object Model, also known as POM, is a design pattern in Selenium that creates an object repository for storing all web elements. POM is can be used with any kind of framework like keyword-driven, Data-driven, hybrid framework etc. It’s an approach to systematically organizing the scripts in such a way that it makes it easy to maintain the code free of hassles and also helps to prevent redundant or duplicate code. Every single web page in the application must have its own corresponding page class, which is in charge of searching the WebElements in that page and then execute operations on them. For instance, if there is a change in the locator value on a specific page, then it is very easy to identify and make the quick change only in the respective page without impacting the code elsewhere.

Basic Rules of POM

>> Do not Write Assert.assertEquals Method in Page Class (POM Class)

>> Assertion is always in test Class Because assertion is part of testng class and test class is for validation.

>> Never used driver.findby in test class always used in the page class because driver is interacting with the UI.

Advantages Of Page Object Model

>> It is useful for maintaining the code (flow in the UI is separated from verification)

>> Makes code readable (Methods get more realistic names)

>> Makes the code reusable (object repository is independent of test cases)

>> The Code becomes less and optimized

Basic Information of “Page Factory”

>> The model helps to handle page object models in new way and its easy to implement. It include inbuilt concept for the Page object model.

>> Page Factory is a class provided by Selenium Webdriver to support Page Object Design patterns.

>> Allows storing of page element in cache memory using @CacheLookUp annotation. Which in return improves performance of Automation.

>> In-Page Factory, testers use @FindBy annotation to locate and declare web elements using different locators. The initElements method is used to initialize web elements. Below is an example of declaring an element using @FindBy

@FindBy(id="elementId") WebElement element;

Different Types of Locators in Selenium

>> By CSS ID: findElement (By.id(“Id”));

>> By CSS class name: findElement (By.className(“class”));

>> By name attribute: findElement (By.name(“name”));

>> By Xpath: findElement(By.xpath(“xpath”));

>> By tagName: findElement(By.tagName(“htmlTag”));

>> By link text: findElement(By.linkText(“textoflink”));

>> By partial link text: findElement(By.partialLinkText(“partialtextoflink”));

Creating a Page Object Model Using Page Factory in Java Bellow Sample Example :

In the Bellow Example First I created separate page for Login.java and HomePage.java in that class I m going to write web element using Page Factory concept @FindBy annotation and initElements method is used to initialize web elements methods. after that in HomePageTest.java class I write the validation of HomePage in test class.

Login page with Page Factory

package PageFactory;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class Login {
    //Page Factory 
    WebDriver driver;

    @FindBy(name="username")
    WebElement username;
    
    @FindBy(name="password")
    WebElement password;

    @FindBy(className="amazon")
    WebElement titleText;
    
    @FindBy(xpath="//input[@type='submit']")
    WebElement loginBtn;
    
    public Login(WebDriver driver){
        this.driver = driver;

        //This initElements method will create all WebElements
        PageFactory.initElements(driver, this);
    }

    //Set user name in textbox
    public void setUserName(String UserName){
        username.sendKeys(UserName);     
    }

    //Set password in password textbox
    public void setPassword(String Password){
        password.sendKeys(Password);
    }

    //Click on login button
    public void clickLogin(){
        loginBtn.click();
    }  

    //Get the title of Login Page
    public String getLoginTitle(){
        return titleText.getText();
    }

    /** This POM method will be used in test case to login in the application
     * @param strUserName
     * @param strPasword
     * @return
     */
    public void loginToamazon(String UserName, String Password){
        //Fill user name
        this.setUserName(UserName);
        //Fill password
        this.setPassword(Password);
        //Click Login button
        this.clickLogin();           
    }
}

Home Page with Page Factory

package PageFactory;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.support.FindBy;

import org.openqa.selenium.support.PageFactory;

public class HomePage {

    WebDriver driver;

    @FindBy(xpath="//*[@id='ap_email']")
      WebElement homePageUserName;    

    public HomePage(WebDriver driver){

        this.driver = driver;

        //This initElements method will create all WebElements

        PageFactory.initElements(driver, this);

    }   

    //Get the User name from Home Page

        public String getHomePageDashboardUserName(){

         return    homePageUserName.getText();

        }

}

HomePageTest with Page Factory concept

package test;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.Assert;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;

import PageFactory.HomePage;

import PageFactory.Login;

public class HomePageTest{

    
    String driverPath = "C:\\chromedriver.exe";

    WebDriver driver;

    Login objLogin;

    HomePage objHomePage; 

    @BeforeTest

    public void setup(){

        System.setProperty("webdriver.chrome.driver", driverPath);
        
        driver = new ChromeDriver();

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.get("https://www.amazon.com/");

    }

    /**

     * This test go to https://www.amazon.com/

     * Verify login page title as amazon

     * Login to application

     * Verify the home page using Dashboard message

     */

    @Test(priority=0)

    public void test_Home_Page(){

        //Create Login Page object

    objLogin = new Login(driver);

    //Verify login page title

    String loginPageTitle = objLogin.getLoginTitle();

    Assert.assertTrue(loginPageTitle,"amazon","Login page title not matched");

    //login to application

    objLogin.loginToamazon("abc", "123");

    // go the next page

    objHomePage = new HomePage(driver);

    //Verify home page
String homePageUserName = objHomePage.getHomePageDashboardUserName();
Assert.assertEquals(homePageUserName, "Expected Username", "Home page username not matched");

    }

}

This was just a demo for the understanding of the POM Using Page Factory. In actual project will require several updations like creating Base Class in that we have to intitalized all the webdriver and wait, creating utility classes for database connectivity, passing test data through config files, etc.

These additional components help to create a more robust and maintainable test automation framework that can handle a variety of scenarios and requirements.

Enjoy your learning journey! Keep exploring new horizons!

397 views
bottom of page