POM Under the Hood: Technical Deep Dive into the Page Object Model

Anjanee Herath
4 min readApr 8, 2024

--

Test automation is crucial in systematic testing to ensure web application quality and reliability. When the web application evolves, new features are added and old ones are changed daily. With every change in the UI, the mountain of maintenance work grows higher. This was a problem to address in test automation. To overcome this we use a concept called Page Object Model (POM)

Using POM leads to more readable, robust, and scalable test scripts reducing code duplication and enhancing the structure and maintainability.

The Page Object Model (POM) is a strategic design pattern used to enhance test automation by inserting an abstraction layer between test scripts and web pages. It involves organizing the application into distinct sections, each represented by a page. A dedicated class called a page object, is created for each page. These page objects act as intermediaries, facilitating the interaction with the page under test without the test scripts needed to engage directly with the web elements. By adopting POM, when changes occur in the application’s UI, only the affected page object requires updating, leaving the test scripts intact and isolated from any modifications. This setup simplifies maintenance and improves the stability of test suites.

To implement POM:

Start by identifying the pages within your web application that will be subject to testing.

For each page, craft a corresponding class that encapsulates the page’s functionalities. This includes defining web elements as variables within the class and developing methods that simulate user actions such as clicks and text entry.

Ultimately, in your test scripts, you’ll leverage these methods rather than interacting with web elements directly, streamlining the testing process and making your scripts more resilient to changes in the application’s user interface.

The Page Object Model (POM) stands out with Its core design principles. Some of these design principles are as follows.

  • Encapsulation: The Page Object Model (POM) encapsulates the internal structure of the web page from the tests. By hiding the UI structure details and exposing only the operations that can be performed on a page, POM ensures that tests are not easily broken and are protected from changes in the UI.
  • Abstraction: The high-level interfaces abstract the functionality of pages, enabling tests to interact with the pages without knowing about the specific elements. This makes the tests more readable and focused on the workflow rather than the implementation details.
  • Single Responsibility Principle (SRP): Each page object should be responsible for the interactions with just one page on the app. This principle helps the code to be organized, making it easier to update and maintain.
  • DRY (Don’t Repeat Yourself): Reusing page objects and their methods across multiple test cases helps to avoid code duplication. This principle helps create reusable components for efficient test script development.

However, when applying POM we need to follow these best practices to gain the maximum advantage.

  • Use descriptive and meaningful method names that convey the purpose of the function. This enhances the readability and maintainability of the code.
  • Instead of locating web elements at the time of page object creation, opt for lazy loading where elements are fetched when they are used. This approach reduces the initialization time of page objects and minimizes the risk of encountering stale element references.
  • Keep the test logic separate from the page objects. The page objects should only interact with the UI elements, while the test scripts should focus on defining the test logic and assertions.
  • Avoid Hardcoding Data. Instead, use external data sources or, data providers to keep your tests adaptable and reusable.
  • Handle Waits Intelligently. Explicitly wait for elements or conditions rather than using fixed periods of sleep. This ensures that your tests are more reliable and faster, as they wait only as long as necessary for conditions to be met.
  • Implement Page Factory. Many frameworks, like Selenium, offer a Page Factory model that provides an easier way to initialize page objects and manage web elements. Utilizing such features can streamline the creation and management of page objects.
  • Regularly revisit and refactor the page objects and tests. This helps in adapting to changes in the application and to improve the design and efficiency of the test code.
  • While the code should be self-explanatory, judicious use of comments can help explain the intent behind certain operations or complex logic, aiding in future maintenance.
  • Integrate POM with a testing framework (e.g., JUnit, TestNG) to leverage features like setup and teardown methods, parameterized tests, and assertions, making the tests more structured and manageable.

There are several advantages of using POM in test scripts some of them are as follows.

  • Maintainability: Changes in the UI can be managed in a single class file without having to rewrite the tests. This significantly reduces the maintenance effort.
  • Reusability: Common page elements and functionalities can be reused across multiple test cases, minimizing code duplication.
  • Readability: Tests become more readable and understandable which is crucial for collaboration and long-term project sustainability.
  • Reduced Flakiness: By abstracting away the direct interactions with the web elements, tests are less susceptible to breaking due to minor changes in the UI.

As with any methodology, the key to success lies in understanding the principles, planning the implementation thoughtfully, and adapting the approach to fit the unique needs of each project. In the fast-evolving world of web development, adopting practices like POM can be a decisive factor in achieving high-quality, reliable software delivery.

--

--

Anjanee Herath
Anjanee Herath

No responses yet