Advanced Page Object Model in Selenium

 ๐Ÿงฉ Advanced Page Object Model (POM) in Selenium

The Page Object Model (POM) is a popular design pattern in Selenium automation used to separate test code from page-specific code. This makes tests more readable, maintainable, and reusable.


In the advanced version of POM, we build on the basic structure by introducing:


Page Factory


Base classes


Utilities


Dynamic locators


Reusable components


Integration with frameworks like TestNG or JUnit


๐Ÿ—️ Structure of Advanced POM

bash

Copy

Edit

/src

 └── test

      ├── base/

      │    └── BaseTest.java

      ├── pages/

      │    ├── LoginPage.java

      │    └── DashboardPage.java

      ├── utils/

      │    └── DriverFactory.java

      ├── tests/

      │    └── LoginTest.java

      └── config/

           └── ConfigReader.java

๐Ÿ”ง Components Explained

1. BaseTest Class

Contains setup and teardown methods (e.g., launching browser, closing browser).


java

Copy

Edit

public class BaseTest {

    WebDriver driver;


    @BeforeMethod

    public void setUp() {

        driver = DriverFactory.getDriver();

        driver.get("https://example.com");

    }


    @AfterMethod

    public void tearDown() {

        driver.quit();

    }

}

2. DriverFactory

Centralized way to manage browser setup.


java

Copy

Edit

public class DriverFactory {

    private static WebDriver driver;


    public static WebDriver getDriver() {

        if (driver == null) {

            driver = new ChromeDriver(); // You can add browser switch logic here

        }

        return driver;

    }

}

3. Page Classes with Page Factory

Encapsulate web elements and actions for a specific page.


java

Copy

Edit

public class LoginPage {

    WebDriver driver;


    @FindBy(id = "username")

    WebElement usernameField;


    @FindBy(id = "password")

    WebElement passwordField;


    @FindBy(id = "loginBtn")

    WebElement loginButton;


    public LoginPage(WebDriver driver) {

        this.driver = driver;

        PageFactory.initElements(driver, this);

    }


    public void login(String user, String pass) {

        usernameField.sendKeys(user);

        passwordField.sendKeys(pass);

        loginButton.click();

    }

}

4. Test Class

Calls methods from page objects to perform tests.


java

Copy

Edit

public class LoginTest extends BaseTest {


    @Test

    public void testValidLogin() {

        LoginPage loginPage = new LoginPage(driver);

        loginPage.login("admin", "admin123");


        // Assert login success using DashboardPage (not shown here)

    }

}

๐Ÿง  Benefits of Advanced POM

Code Reusability: Shared logic stays in base classes/utilities


Maintainability: Easy to update locators or actions in one place


Readability: Test classes read like plain English


Scalability: Better suited for large-scale test automation projects


Integration Friendly: Easily integrates with TestNG, JUnit, Maven, CI/CD tools


⚠️ Best Practices

Avoid logic in your page classes—keep them clean and focused


Use dynamic locators where needed (e.g., By.xpath("//div[text()='" + value + "']"))


Keep assertions only in test classes, not in page classes


Group reusable methods (e.g., wait helpers, common actions) in a Utils class

Learn Testing Tools Training in Hyderabad

Read More

Parallel Testing with TestNG

API Automation in Karate DSL

Building a CI/CD Pipeline with Selenium and Jenkins

Performance Testing with JMeter: Advanced Tips

Visit Our IHUB Talent Training Institute in Hyderabad

Get Directions

Comments

Popular posts from this blog

How to Install and Set Up Selenium in Python (Step-by-Step)

Tosca for API Testing: A Step-by-Step Tutorial

Handling Frames and Iframes Using Playwright