Learn Page Object Model (POM) in Selenium with Java (2023)

page object model,page object model framework,page object model in selenium,what is page object model,page object model selenium webdriver,page object model framework in selenium,page object model tutorial,what is page object model (pom),page object model for beginners,page object model job interview,page object model framework selenium,page object model selenium java example,page object model selenium,page object model vs page factory
Page Object Model

What is Page Object Model (POM)?

POM is a design pattern used in Selenium Automation testing to create a framework for testing web applications. It separates the test code from the code responsible for interacting with the user interface (UI) of the web application, making it easier to maintain and update tests as changes are made to the application.

  • In POM, Each page of the web application is represented as an object, with methods and properties that represent the elements on that page. 
  • These objects are used in the test code to interact with the application instead of directly interacting with the HTML elements on the page.
  • The benefits of using POM include improved test maintenance, increased code reusability, improved test structure, and better collaboration between testers and developers.

The Architecture of the Page Object Model (POM):

The Architecture of the Page Object Model in Selenium is based on the concept of separating the Page Objects from the test code. It follows the principle of "separation of concerns," where the test code only interacts with the page objects, and the page objects handle the interactions with the web elements on the page.

The Architecture of POM typically consists of three components:

1. Page Classes

A page class is a Java class that contains the elements and actions for a specific page in the application. Each page in the application will have its own page class. The page class defines the web elements on the page and the actions that can be performed on them.

2. Test Classes

A test class is a Java class that contains the actual test code. It interacts with the page objects to perform the desired actions and verify the expected results. The test class should not contain any direct references to the web elements on the page.

3. Driver Class

The driver class is responsible for initializing the web driver and providing it to the page and test classes. The driver class should only be instantiated once in the test suite, and all page and test classes should use the same instance of the driver.

How to implement the Page Object Model (POM) using Selenium with Java:

Step 1: Create a Maven Project

Create a new Maven project in your IDE (such as Eclipse or IntelliJ IDEA). This will manage your project's dependencies and make it easier to package and distribute your code.

Step 2: Add Selenium and TestNG Dependencies

Add the Selenium and TestNG dependencies to your project's pom.xml file. 

Here's an example:


Step 3: Create Page Classes using Page Factory

To use Page Factory in Selenium, we first create a Page Object class that represents a page on the website. This class contains a set of web elements that we want to interact with on the page. We can use the @FindBy annotation to specify how to locate each web element on the page. For example:

Create a new Java class for each page of the web application you want to test. 

For example, if you have a login page, create a LoginPage class.

public class LoginPage {
    private WebDriver driver;
    public LoginPage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    @FindBy(id = "username")
    private WebElement usernameInput;
    @FindBy(id = "password")
    private WebElement passwordInput;
    @FindBy(id = "login-btn")
    private WebElement loginButton;
    public void enterUsername(String username) {
    public void enterPassword(String password) {
    public void clickLoginButton() {

In this example, we're using the @FindBy annotation to locate the web elements on the page. We're also using the PageFactory class to initialize the web elements with the driver.

Step 4: Create Test Classes

Create a new Java class for each test you want to run. Here's an example:

public class LoginTest {
    private WebDriver driver;
    public void setup() {
        driver = new ChromeDriver();
    public void teardown() {
    public void testLogin() {
        LoginPage loginPage = new LoginPage(driver);
        // Assert that the user is logged in
    private boolean isUserLoggedIn() {
        // Code to check if the user is logged in

In this example, we're using the @BeforeClass annotation to set up the driver before the tests run, and the @AfterClass annotation to quit the driver after the tests run. We're also using the @Test annotation to specify that this method is a test.

Step 5: Run the Tests

Finally, run the tests using TestNG. You can do this from the command line or from within your IDE. 

Here's an example command to run the tests from the command line:

mvn clean test

This will run all the tests in your project.

Benefits of Page Object Model with Selenium Java:

  • Separation of concerns: The test code and the code responsible for interacting with the UI are separated, making the code easier to maintain and update.
  • Code reusability: The page objects can be reused in multiple tests, reducing code duplication.
  • Improved test structure: The use of page objects and test classes makes the test structure more organized and easier to understand.
  • Easy maintenance: If the UI changes, only the page object needs to be updated, rather than modifying all the tests that use that page.
  • Reduced code complexity: The use of page objects abstracts the details of interacting with the UI, making the test code simpler and easier to read.

Overall, the Page Object Model is a widely-used design pattern in Selenium automation testing that can help simplify the test code, improve code reusability, and make test maintenance easier.

Post a Comment

Post a Comment (0)

Previous Post Next Post