QA Interview Hub · Knowledge base · Quizzes · Українська

Patterns › SOLID, DRY, KISS in tests, and anti-patterns

SOLID in a test framework

SOLID in a test framework means a change in one place does not break another and swapping a part does not force rewriting the consumer. A Page Object describes the UI, a factory the data, an API client the requests, and tests get dependencies through fixtures. Typical smells: a bloated ApiClient, a page subclass changing the meaning of open(). Real variability justifies an abstraction, not a wish for purity.

Patterns · Principles

An ApiClient with two hundred methods and it is edited every Monday. Which principle is violated?

SOLID was invented for object-oriented code, but a test framework is code too, read and changed for years. Its classes grow by the same laws: a page that knows about the DB, a client that grows with every new requirement, a subclass that breaks the parent's expectations. SOLID gives names to these pains. One caveat: in tests simplicity and readability matter more than the purity of a pattern.

How it works and why. All five principles are about one thing: a change in one place must not break another, and swapping a part must not require rewriting the consumer. The table shows their embodiment in test code and the smell of a violation.

PrincipleIn a test frameworkA smell of violation
SA Page Object describes the UI, a factory the data, an API client the requestsA Page Object creates a user in the DB itself
OA new login method is a new class or a key in a dictionaryA switch on the mode in ten places
LA page subclass can stand where the base one is expectedA subclass overrides open() and changes its meaning
ISmall interfaces: PaymentsApi, UsersApiAn ApiClient with two hundred methods
DA test depends on a fixture, not on a way to log innew LoginPage(page).loginAs(...) in every test

S: one reason to change. A Page Object that also prepares data in the DB changes both when the markup changes and when the DB schema changes. Data should be prepared by a factory or a fixture:

// before: a Page Object also prepares data
class TransferPage {
  async seedUser() {
    await db.query('insert into users(email) values ($1)', [email]);
  }
}

// after: a fixture prepares data, the page only describes the UI
export const test = base.extend<{ user: User }>({
  user: async ({ request }, use) => {
    const user = await createUserViaApi(request);
    await use(user);
    await deleteUserViaApi(request, user);
  },
});

L: a subclass can be substituted. If ReportsPage extends BasePage, then wherever open() of the base page is called, the subclass must behave the same. Deep page hierarchies often violate this:

class BasePage {
  protected readonly path: string = '/';
  constructor(protected readonly page: Page) {}

1  async open() {
    await this.page.goto(this.path);
  }
}

class ReportsPage extends BasePage {
  protected readonly path = '/reports';

  // breaks the contract: after open() the page is no longer opened from scratch
2  async open() {
    await this.page.getByRole('link', { name: 'Reports' }).click();
  }
}
  1. The contract of the base open(): navigate to the address, so one can start from an empty tab.
  2. The subclass requires the user to already be on another page with a link. A call new ReportsPage(page).open() from a clean state will fail.

The fix: do not override open() with a different meaning; give navigation a separate method, or replace inheritance with composition.

D: depend on an abstraction. In Playwright fixtures implement it naturally: a test asks for “a logged-in admin page”, and how it became one (a form, an API, a saved state) is decided by the fixture.

// depends on the way to log in
test('lists transfers', async ({ page }) => {
  await new LoginPage(page).loginAs(admin);
  await page.goto('/transfers');
});

// depends on an abstraction: a fixture that provides a logged-in page
test('lists transfers', async ({ adminPage }) => {
  await adminPage.goto('/transfers');
});

O is shown in the Strategy example: a new login method is added as a class, not by editing a switch. I means splitting a bloated ApiClient into PaymentsApi, UsersApi that share one request: a test depends only on what it needs.

Limits and alternatives. Here the difference from production code applies: tests must be simple, and an abstraction is justified by real variation or the pain of change. In TypeScript structural typing makes interfaces cheap, and Playwright fixtures provide DI, so a dependency container is usually not needed.

How much abstraction is appropriate

Wrong

A principle for its own sake

An interface with one implementation and three layers for a five-line test.

Right

A principle where there is variation

An abstraction appears when a second implementation or the pain of change shows up.

Figure 1. A principle is justified by the problem it removes.

Common mistakes.

  • An interface with one implementation “just in case”: a layer that only complicates navigation.
  • A base page class that grows into a “God Page”: everything that might be needed in several places lands in it.
  • Inheritance for code reuse instead of composition: the subclass inherits unrelated methods too.
  • A factory that reaches into the DB in the constructor: it violates SRP and makes every test pay for the extra.
  • A dependency container on top of Playwright fixtures: a double DI mechanism.

Check yourself

AdminPage extends BasePage but AdminPage.open() does not call goto, it clicks a menu link. Which principle is violated and why can a test fail?

Answer

Liskov: the subclass cannot stand in for the base class. Code that calls open() from a clean state relies on goto, while a click on a link needs an already opened page with a menu.

Why does 'Dependency Inversion' in Playwright usually not need a dependency container?

Answer

Fixtures are already built-in DI: a test declares what it needs, and Playwright creates the dependencies in the right order and cleans them up after the test. An extra container would only duplicate this mechanism.

In an interview. Junior: The most important in tests for me are SRP and Dependency Inversion: each class does one thing, and tests get dependencies through fixtures. Middle/Senior: I apply the rest when there is real variation or the pain of change, not for the sake of a template. I check Liskov on page hierarchies: if a subclass changes the meaning of open(), I replace inheritance with composition. I do not add an abstraction for one implementation.

More articles in this chapter