QA AND TEST AUTOMATION · PART 5 · UI WEB AUTOMATION · DESIGN · 54 OF 112

Page Object Model (POM): Design and Its Limits

The Page Object Model for UI tests: one class per page or component that owns locators and user actions, so tests read like behaviour and UI changes are fixed in one place. A real ShopRung ProductsPage in Playwright TypeScript (run for real: passed), the Java Selenium version, where assertions belong, components, and the limits: god objects, deep inheritance, and when helpers or the screenplay pattern fit better.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 13

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • One class per page or component
  • locators + user actions
  • assertions in tests
  • composition
  • know the limits

Notes

The problem

Without page objects, every test that searches contains the search box locator. Some use the role, one uses the id, and nobody knows which is right. When the label changes, you edit twenty three files. And the tests read as a list of clicks and fills, so a reader has to work out what behaviour is being tested. Both problems grow with every test you add.

Without page objects
test A: getByRole("searchbox",
         {name: "Search products"})
test B: getByRole("searchbox",
         {name: "Search products"})
test C: locator("#q")  (!)
...
label renamed to "Search"
  → 23 tests to edit
tests read like clicks, not
  like behaviour

The idea

The idea is simple. Create a class for each page, or each significant part of a page. It owns the locators for that page and offers methods for what a user can do there: search for a term, add a product to the cart. Tests call those methods and check results. When the UI changes, you fix one class. Tests describe what the user does; page objects know how it is done on this particular page.

Page Object Model
  • a class per page (or part) owns its locators offers user actions: searchFor(term), addToCart
  • tests call actions and assert on results
  • UI changes → fix one class
  • tests: WHAT; page objects: HOW

ShopRung ProductsPage

Here is a real ShopRung products page object. The constructor receives the page and defines locators once: the search box by role and label, and the result count by test id. Search for fills the box and presses Enter, which submits the search form. Locators in Playwright are lazy, they do not search until used, so defining them in the constructor is cheap and safe even before the page loads.

pages/ProductsPage.ts
export class ProductsPage {
  readonly search: Locator;
  readonly count: Locator;
  constructor(private page: Page) {
    this.search = page.getByRole(
      "searchbox", {name: "Search ..."});
    this.count = page.getByTestId(
      "result-count"); }
  async searchFor(term: string) {
    await this.search.fill(term);
    await this.search.press("Enter"); }

Parameterised parts

Lists need parameterised locators. Card takes a product name and returns the product card containing it, so tests can check one card. Add to cart finds that card's button and clicks it. Open goes to the products page. Notice the methods speak the user's language, product names, never CSS paths or indexes, and the scoping trick from the locators episode now lives in one place.

... continued
  card(name: string) {
    return this.page
      .getByTestId("product-card")
      .filter({ hasText: name });
  }
  async addToCart(name: string) {
    await this.card(name)
      .getByRole("button").click();
  }
  async open() {
    await this.page.goto("/"); } }

The test

Now the test reads like the behaviour. Open products, search for mug, expect the count to be two products, and expect the red build mug's card to say out of stock. We ran it against ShopRung, and it passed. If the search label changes tomorrow, only the page object changes, and every test that searches keeps working.

tests/pom.spec.ts
test("search finds the two mugs",
  async ({ page }) => {
  const products =
    new ProductsPage(page);
  await products.open();
  await products.searchFor("mug");
  await expect(products.count)
    .toHaveText("2 products");
  await expect(products.card(
    "Red Build Mug"))
    .toContainText("Out of stock"); });

Where assertions go

Where do assertions go? A common, useful rule: page objects hold locators and actions, and perhaps a check that the right page is open. Tests hold the expectations, because one page object serves many tests that expect different things: two results in one test, none in another. So expose locators or values from the page object, like count or card, and let the test assert on them.

A useful rule
  • page objects: locators and actions, maybe "am I here?" checks
  • tests: the expectations (what SHOULD happen)
  • why: one page object serves many tests with different expectations
  • expose locators or values; let the test assert

Components

Real pages share parts. ShopRung's header, with the search box, cart count and log in link, appears on every page, so it becomes its own component object, and both the products page and the cart page have a header. Actions that navigate can return the next page object: header open cart returns a cart page, giving a fluent flow. Prefer this composition over long base page inheritance chains.

Composition
  • Header: search, cart count, log in / out ProductsPage has a Header
  • CartPage has a Header
  • actions that navigate can return the next page: header.openCart() → CartPage
  • prefer composition over deep inheritance (BasePage chains)

In Java

In Java with Selenium the pattern is identical. The class holds the driver and its By locators as private fields, and public methods for actions, often returning this or the next page object for fluent chains, plus methods that return values for tests to assert on, like the count text. Avoid the old page factory annotations for new code; plain By fields with explicit waits are clearer.

ProductsPage.java
public class ProductsPage {
  private final WebDriver d;
  private final By search =
    By.id("q");
  private final By count = By
    .cssSelector("[data-testid=
      result-count]");
  public ProductsPage searchFor(
      String term) { ...; return this; }
  public String countText() {
    return d.findElement(count)...

The limits

The pattern has limits. Page objects can grow into god objects with two thousand lines. Deep base page inheritance hides behaviour. Wrapping every trivial click adds code without value. Hidden assertions inside actions surprise readers. And flows that span many pages fit pages poorly. Alternatives exist: small helper functions, Playwright fixtures, which is the next episode, and the screenplay pattern, which models actors and tasks instead of pages.

POM problems
  • god objects: 2,000-line pages
  • deep BasePage inheritance
  • every click wrapped, even trivial ones
  • assertions hidden inside actions
  • pages for flows that span many pages
  • alternatives: small helpers, fixtures, screenplay pattern

Common mistakes

Page object mistakes. Locators still duplicated in tests anyway. Methods named after clicks, like click button three, instead of behaviour, like apply coupon. A static shared driver inside pages, which breaks parallel runs. Waits sprinkled in tests instead of inside page methods. One page object per test, which defeats the purpose. And returning raw elements everywhere, so tests depend on the DOM again.

Avoid these
  • locators duplicated anyway
  • methods named after clicks (clickButton3) not behaviour
  • static driver inside pages
  • waits sprinkled in tests
  • one page object per test
  • returning raw WebElements everywhere

Say it like this

In the interview: the Page Object Model is a design pattern where each page or component is a class that owns its locators and offers user level actions. Tests call those actions and make the assertions, so tests read like behaviour and UI changes are fixed in one place. I keep assertions in tests, compose shared components like headers, return the next page from navigation, and avoid god objects and deep inheritance.

In the interview
  • "A design pattern where each page or component is a class
  • that owns its locators and offers user-level actions.
  • Tests call those and assert, so they read like behaviour and UI changes are fixed once. I
  • keep assertions in tests, compose components, and avoid
  • god objects and deep
  • inheritance."

Interview questions

Q1What is the Page Object Model?

A design pattern where each page or component is represented by a class that holds its locators and exposes user-level actions, separating test intent from UI details.

Q2What are the benefits of POM?

Less duplication, one place to fix UI changes, readable tests that describe behaviour, and reusable actions across tests.

Q3Should page objects contain assertions?

Generally no, apart from simple "page loaded" checks; tests own expectations so one page object can serve different scenarios.

Q4What is the difference between Page Object Model and Page Factory?

Page Factory is a Selenium helper that initialises @FindBy-annotated fields; POM is the design pattern. Plain By fields with explicit waits are usually preferred now.

Q5How do you handle components shared across pages?

Model them as component objects (for example Header) and compose them into page objects instead of inheriting.

Q6What is a fluent page object?

One whose action methods return this or the next page object, so steps chain: home.searchFor("mug").openCart().

Q7What are the downsides of POM?

God objects, deep inheritance, trivial wrappers, hidden assertions and poor fit for flows spanning many pages.

Q8What is the Screenplay pattern?

An alternative design where actors with abilities perform tasks and ask questions, focusing on user goals rather than pages.