Quick Contact

    Page Object Model

    Page Object Model is a Design Pattern which has become popular in Selenium Test Automation. It is widely used design pattern in Selenium for enhancing test maintenance and reducing code duplication. Page object model (POM) can be used in any kind of framework such as modular, data-driven, keyword driven, hybrid framework etc. A page object is an object-oriented class that serves as an interface to a page of your Application Under Test(AUT). The tests then use the methods of this page object class whenever they need to interact with the User Interface (UI) of that page. The benefit is that if the UI changes for the page, the tests themselves don’t need to change, only the code within the
    page object needs to change. Subsequently, all changes to support that new UI is located in one place.

    Page Factory

    The PageFactory Class in Selenium is an extension to the Page Object design pattern. It is used to initialize the elements of the Page Object or instantiate the Page Objects itself.
    Annotations for elements can also be created (and recommended) as the describing properties may not always be descriptive enough to tell one object from the other.

    It is used to initialize elements of a Page class without having to use ‘FindElement’ or ‘FindElements’. Annotations can be used to supply descriptive names of target objects to improve code readability. There is however a few differences between C# and Java implementation – Java provide greater flexibility with PageFactory.

    Difference between Page Object Model (POM) and Page Factory

    Page Object is a class that represents a web page and hold the functionality and members.

    Page Factory is a way to initialize the web elements you want to interact with within the page object when you create an instance of it.

    Advantages of Page Object Model Framework
    • Code reusability: We could achieve code reusability by writing the code once and use it in different tests.
    • Code maintainability: There is a clean separation between test code and page specific code such as locators and layout which becomes very easy to maintain code. Code changes only on Page Object Classes when a UI change occurs. It enhances test maintenance and reduces code duplication.
    • Object Repository: Each page will be defined as a java class. All the fields in the page will be defined in an interface as members. The class will then implement the interface.
    • Readability: Improves readability due to clean separation between test code and page specific code
    Set up Page Object Model (POM) in Selenium

    Let’s assume below program our base test case and implement the Page Object Model (POM) in it. The steps include

    • Create a ‘New Package‘ file and name it as ‘pageObjects’, by right click on the Project and select New > Package. We will be creating different packages for Page Objects, Utilities, Test Data, Test Cases and Modular actions. It is always recommended to use this structure, as it is easy to understand, easy to use and easy to maintain.
    • Create a ‘New Class‘ file and refer the name to the actual page from the test object, by right click on the above created Package and select New > Class. In our case it is Home Page and LogIn Page.
    • Now create a Static Method for each Element (Object) in the Home Page. Each method will have an Argument (driver) and a Return value (element).
    • import org.openqa.selenium.By;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.WebElement;
      public class Home_Page {
      private static WebElement element = null;
      public static WebElement lnk_MyAccount(WebDriver driver){
      element = driver.findElement(By.id("account"));
      
      return element;
      
      }
      
      public static WebElement lnk_LogOut(WebDriver driver){
      
      element = driver.findElement(By.id("account_logout"));
      
      return element;
      
      }
       
      }
      

      Driver is being passed as an Argument so that Selenium is able to locate the element on the browser (driver).

      Element is returned, so that an Action can be performed on it.

      Method is declared as Public Static, so that it can be called in any other method without instantiate the class.

      Follow the same rule for creating LogIn Page class.

      import org.openqa.selenium.*;
      
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.WebElement;
      
      public class LogIn_Page {
      
      private static WebElement element = null;
      
      public static WebElement txtbx_UserName(WebDriver driver){
      
      element = driver.findElement(By.id("log"));
      
      return element;
      }
      
      
      public static WebElement txtbx_Password(WebDriver driver){
      
      element = driver.findElement(By.id("pwd"));
      
      return element;
       
      }
      
      public static WebElement btn_LogIn(WebDriver driver){
      
      element = driver.findElement(By.id("login"));
      
      return element;
      
      }
      
      }
      
      
    • Create a ‘New Class‘ and name it as POM_TC by right click on the ‘automationFramework‘ Package and select New > Class. We will be creating all our test cases under this package.

      Now convert your old First Test Case in to the new Page Object Model test case.

      import java.util.concurrent.TimeUnit;
      
      import org.openqa.selenium.WebDriver;
      
      import org.openqa.selenium.firefox.FirefoxDriver;
      
      // Import package pageObject.*
      
      import pageObjects.Home_Page;
      
      import pageObjects.LogIn_Page;
      
      public class PageObjectModel {
      
      private static WebDriver driver = null;
      
      public static void main(String[] args) {
      
      driver = new FirefoxDriver();
      
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      
      driver.get("http://www.store.demoqa.com");
      
      // Use page Object library now
      
      Home_Page.lnk_MyAccount(driver).click();
      
      LogIn_Page.txtbx_UserName(driver).sendKeys("testuser_1");
      
      LogIn_Page.txtbx_Password(driver).sendKeys("Test@123");
      
      LogIn_Page.btn_LogIn(driver).click();
      
      System.out.println(" Login Successfully, now it is the time to Log Off buddy.")
      
      Home_Page.lnk_LogOut(driver).click();
      
      driver.quit();
      
      }
      
      }
      
    • You will notice that once you type Home_Page in your test script and the moment you press dot, all the methods in the Home Page will display. We can expose methods in order to reduce duplicated

      code. We are able to call these method multiple times. This will ensure a better maintainable test code, because we only have to make adjustments and improvements in one particular place.

      Your Project explorer window will look like this now.

    Summary
    • Page Object Model is an Object Repository design pattern in Selenium WebDriver.
    • Under this model, for each web page in the application, there should be corresponding page class.
    • This Page class will find the WebElements of that web page and also contains Page methods which perform operations on those WebElements.
    • Code becomes less and optimized because of the reusable page methods in the POM classes.
    • Selenium Page Factory Pattern is like an extension to Page Object Model , but Page Factory is much enhanced model.
    • Page Factory class can be used to make using Page Objects simpler and easier

    Copyright 1999- Ducat Creative, All rights reserved.