Automated tests are code, and they rot like code. A suite of 800 UI tests that each locate the login button directly will need 800 edits the day the button’s ID changes. A suite where every test builds its own customer record from scratch will break in 300 places when the customer model gains a mandatory field. Design patterns in test automation exist for one reason: to make sure that a single change in the product means a single change in the test code.

This article walks through the patterns that matter most, starting with the familiar Page Object and moving to the ones that solve problems Page Objects do not.

The Page Object Model and what it actually protects

A Page Object is a class that represents one page or significant component of the UI. It holds the locators for that page and exposes methods that describe what a user can do there: loginAs, searchFor, addToCart. Tests call those methods and never touch locators directly.

The pattern protects against two kinds of change. If a locator changes, only the page class is edited. If the steps for an action change (say login now needs an OTP screen), only the method body changes, while tests that call loginAs stay as they are. That second benefit is the bigger one and is often missed: a Page Object should expose intent, not mirror every field. A class with methods like typeIntoUsernameBox and clickSubmitButton is just locators with extra steps.

Two guidelines keep Page Objects healthy. First, keep assertions out of them, or at least limit them to checks that the page loaded correctly; the test should decide what counts as a pass. Second, when an action navigates to a new page, return the object for that new page. This makes the test read like the user journey and lets the compiler catch flows that no longer make sense.

Page Factory: a convenience, not a different pattern

In Selenium’s Java bindings, Page Factory is a helper for building Page Objects. You annotate fields with FindBy and call initElements, and the library fills the fields with proxies that find the element lazily each time it is used. So Page Factory is an implementation style of the Page Object Model, not an alternative to it.

The convenience comes with caveats. Annotations must be compile-time constants, so dynamic locators (a row containing a particular order number, for example) need ordinary methods anyway. Optional caching of found elements can produce stale element errors on pages that re-render. And the proxy behaviour can make waiting logic harder to reason about. For these reasons many teams, including people close to the Selenium project, now prefer plain locator fields resolved inside methods. Playwright and Cypress do not have a Page Factory at all; their locators are lazy by design, so a plain class holding locators gives the same benefit.

Patterns for test data: builders and factories

Test data is where many suites quietly become unmaintainable. Picture an insurance quoting system where a valid policyholder needs a name, date of birth, PAN, address with PIN code, nominee details and a KYC status. If each test creates that object by hand, every model change touches every test.

The Builder pattern fixes this. A PolicyholderBuilder starts with sensible, valid defaults for everything. A test then overrides only what it cares about: a builder with age 17 for an eligibility test, or with KYC pending for a blocked-purchase test. The test now states its intent clearly, because the one unusual field is the only one mentioned. When the model gains a new mandatory field, one default is added in the builder and nothing else changes.

The Factory pattern (or the related “object mother”) goes a step further and offers named, ready-made variants: seniorCitizen, nriCustomer, customerWithLapsedPolicy. Factories also appear in driver creation, where a DriverFactory decides whether to return a local Chrome session, a remote grid session or a mobile driver based on configuration, so tests never contain that branching themselves.

Patterns for behaviour: strategy, decorator and facade

  • Strategy swaps an algorithm without touching callers. A test framework might have several ways to log in (through the UI, through an API call that sets a session cookie, or via a pre-authenticated storage state). Tests ask for “a logged-in user” and the configured strategy decides how. Most tests should use the fast API route; a handful of tests cover the UI login itself.
  • Decorator wraps an object to add behaviour. Wrapping the driver to log every action, take screenshots on failure or highlight elements in debug mode keeps those concerns out of tests and page classes.
  • Facade hides a multi-step setup behind one call. “Create an order that has shipped” might involve three API calls and a database update; a facade method keeps that knowledge in one place.

A word on Singleton: it is commonly used for the driver or for configuration, and it causes trouble the moment tests run in parallel, because a single shared driver cannot serve multiple threads. If you want one instance per test thread, you need per-thread storage, not a global singleton.

The Screenplay pattern: when pages are the wrong unit

Page Objects organise code around screens. That works until journeys span many screens and many kinds of interface. The Screenplay pattern, popularised by Serenity BDD, organises code around actors who have abilities (browse the web, call an API), perform tasks (place an order) made of smaller interactions (click, enter text), and ask questions (what is the order status?).

The gain is composability. A task such as “register a customer” can be reused in hundreds of tests, regardless of whether it is done through UI or API underneath, and a test with two actors (a buyer and a seller, say) reads naturally. The cost is more classes and a steeper learning curve. For a team of five testers automating one web app, Page Objects plus builders are usually enough. For a large product with web, mobile and API layers and many contributors, Screenplay’s structure can pay for itself.

A quick test of maintainability

Rather than counting patterns, ask what happens under a few common changes:

Product changeHealthy suite editsUnhealthy suite edits
A button’s locator changesOne page classEvery test that clicks it
Login gains an OTP stepOne login method or strategyEvery test’s setup
Customer model gains a mandatory fieldOne builder defaultEvery test that creates a customer
Tests must run on a remote gridDriver factory and configTest classes themselves

If your answers sit in the middle column, the patterns are doing their job. If they sit in the right-hand column, adding more patterns will not help until the duplication is removed. Patterns are tools for keeping each piece of knowledge in exactly one place. Applied for their own sake, they just add layers.

Written by the RP Strativa Team. The insurance system and class names in the examples are illustrative.

Leave a Reply

Trending