The Wayback Machine - https://web.archive.org/web/20240727123545/https://www.geeksforgeeks.org/page-object-model-pom/
Open In App

Page Object Model (POM)

Last Updated : 16 Jul, 2019
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

What is POM?
POM is a design pattern which is commonly used in Selenium for Automating the Test Cases. This design pattern can be used with any kind of framework like keyword-driven, Data-driven, hybrid framework, etc.
The Page object is an object-oriented class which acts as an interface for the page of your Application under test. Page class contains web elements and methods to interact with web elements. While Automating the test cases, we create the object of these Page Classes and interact with web elements by calling the methods of these classes.

Page Object Model Design Pattern

Uses of Page Object Model (POM):
This Design Pattern is used in Selenium where web pages are represented by a corresponding class and web elements are represented by the variables of the class and all interactions are possible through the methods or say functions of the class.

Advantages of POM model:

  • Reusability: We can reuse the page class if required in different test cases which means we don’t need to write code for identifying the web elements and methods to interact with them for every test case.
  • Maintainability: As we can see from the above picture test case and page class are different from each other which means we can easily update the code if any new web element is added or existing one updated.
  • Readability: As we can see in the above picture page code is separated form test code which helps to improve code readability.

Implementation of Page Object Model:
Here I’m taking the example of Gmail application to show the implementation of POM.
The Project Hierarchy of my Maven Project is shown below:

Project Hierarchy

TestBase Class:
In this class we create the object of WebDriver class, maximize the browser, launching Url etc.




package test_cases;
  
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
  
public class TestBase {
  
    public static WebDriver driver = null;
  
    @BeforeSuite
    public void initialize() throws IOException {
  
        System.setProperty("webdriver.chrome.driver",
        System.getProperty("user.dir") + 
            "\\src\\test\\java\\drivers\\chromedriver.exe");
    
        driver = new ChromeDriver();
    
        // To maximize browser
        driver.manage().window().maximize();
    
        // Implicit wait
        driver.manage().timeouts().implicitlyWait(
             10, TimeUnit.SECONDS);
    
        // To open Gmail site
        driver.get("https:// www.gmail.com");
    }
  
    @AfterSuite
    // Test cleanup
    public void TeardownTest() {
        TestBase.driver.quit();
    }
}


GmailLoginPage Class:
In this class, we identify the web elements of Gmail Login Page and methods to interact with these web elements.




package pages;
  
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
  
public class GmailLoginPage {
  
    WebDriver driver;
  
    public GmailLoginPage(WebDriver driver) {
        this.driver = driver;
    }
  
    // Using FindBy for locating elements
    @FindBy(how = How.XPATH, using = 
            "// input[@type='email']]")
    WebElement emailTextBox;
  
    @FindBy(how = How.XPATH, using = 
            "// input[@type='password']")
      
    WebElement passwordTextBox;
    @FindBy(how = How.XPATH, using =
            "// div[@role = 'button' and @id  =
            'identifierNext']")
    WebElement nextButton;
  
    // Defining all the user actions (Methods)
    //that can be performed in the Facebook home page
  
    // This method is to set Email in the email text box
    public void setEmail(String strEmail) {
            emailTextBox.sendKeys(strEmail);
        }
        // This method is to set Password in the password text box
    public void setPassword(String strPassword) {
            passwordTextBox.sendKeys(strPassword);
        }
        // This method is to click on Next Button
    public void clickOnNextButton() {
        nextButton.click();
    }
}


Similarly, we write the code for Gmail Home Page.

GmailLoginTest:
In this class we test the login page of Gmail.




package test_cases;
  
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;
import pages.GmailHomePage;
import pages.GmailLoginPage;
  
public class GmailLoginTest extends TestBase {
  
    @Test
    public void init() throws Exception {
  
        // driver.get("https:// www.gmail.com");
        GmailLoginPage loginpage = 
           PageFactory.initElements(driver,
           GmailLoginPage.class);
    
        loginpage.setEmail("abc@gmail.com");
        loginpage.clickOnNextButton();
        loginpage.setPassword("23456@qwe");
        loginpage.clickOnNextButton();
    }
}




Similar Reads

How to display search result of another page on same page using ajax in JSP?
In this example, we are creating a search bar to display the result on the same page in using ajax in JSP. AJAX stands for Asynchronous JavaScript and XML. Where Ajax is mainly used for Displaying another web page content on the same web page without refreshment of the page. You can read more about ajax technology here. Approach: We use the JQuery
2 min read
How can a page be forced to load another page in JavaScript ?
In JavaScript, you can force a page to load another page by using the window.location object. There are a few methods to achieve this. To force a page to load another page in JavaScript, we have multiple approaches: Below are the approaches used to force a page to load another page in JavaScript: Table of Content Using window.location.replaceUsing
2 min read
How to redirect a page to another page in HTML ?
Redirecting a page in HTML involves sending users to a different web page automatically or upon interaction. This can be achieved using the anchor tag's href attribute or the meta tag with the http-equiv attribute set to refresh and the content attribute specifying the time delay and destination URL. Understanding these methods helps improve naviga
3 min read
How to show Page Loading div until the page has finished loading?
There are a lot of ways in which we can show a loading div but we have figured out the most optimal solution for you and that too in pure vanilla JavaScript. We will use the document.readyState property. When the value of this property changes, a readystatechange event fires on the document object. The document.readyState property can return these
2 min read
Mongoose Document Model.prototype.model() API
The Mongoose Document API Model.prototype.model() method of the Mongoose API is used on the Document model. It allows to get the model instance. We can call this method on any model object and by providing another model name to the method parameter we will get the new instance of that model. Let us understand the model() method using an example. Sy
3 min read
Introduction to Model View View Model (MVVM)
Description of Model is as follows: MODEL: ( Reusable Code – DATA ) Business Objects that encapsulate data and behavior of application domain, Simply hold the data. VIEW: ( Platform Specific Code - USER INTERFACE ) What the user sees, The Formatted data. VIEWMODEL: ( Reusable Code – LOGIC ) Link between Model and View OR It Retrieves data from Mode
3 min read
Difference between id and cid properties on a model object in Backbone.js
Backbone.js is a compact library used to organize JavaScript code. An MVC/MV* framework is another term for it. If MVC is unfamiliar to you, it is merely a technique for designing user interfaces. The creation of a program's user interface is made considerably easier by JavaScript functions. BackboneJS provides a variety of building elements to aid
3 min read
EmberJS Object Model
An object model refers to the way objects are structured and organized within the framework. Ember.js is an open-source JavaScript framework for building web applications, and it follows the Model-View-Controller (MVC) architectural pattern. In Ember.js, the item model rotates around models, which address the fundamental information of your applica
2 min read
Browser Object Model
Browser Object Model (BOM) is a programming interface JavaScript tool for working with web browsers. This enables access & manipulation of the browser window, frames, and other browser-related objects by facilitating the JavaScript code. The main object is "window," which helps to interact with the browser. By default, you can call window funct
3 min read
What is the DOM (Document Object Model) ?
The DOM, or Document Object Model, is a programming interface for web documents. It represents the structure of a document as a tree of objects, where each object corresponds to a part of the document, such as elements, attributes, and text. The DOM provides a way for programs to manipulate the structure, style, and content of web documents dynamic
2 min read
Article Tags :
three90RightbarBannerImg