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

Cypress › Framework architecture

Page Object or App Actions: which do you choose?

Page Object and App Actions solve different problems, so they are combined rather than chosen between. Page Object gathers a page's selectors and actions into a class and eases interface maintenance. App Actions prepare state directly through the API, cy.task or application functions, which makes the test faster and more stable. So state is prepared through the API, and the UI is used where it is the subject of the test.

Cypress · Patterns and data

The statistics page changed and forty tests broke. Should they have been written differently?

A test checks a chart on the statistics page, but to reach it, it logs in, creates five orders through a form and only then opens the report. When the order form changes, the report tests fail too, although the report did not change. Page Object and App Actions answer different halves of this problem. This lesson covers what each solves, how they combine and when one of them is unnecessary.

How it works and why. Page Object gathers a page's selectors and actions into a class, so a test reads as a scenario and a locator changes in one place. It solves the maintenance problem: what to do when the interface changed. App Actions control the application's state directly instead of clicking: through the API (cy.request), through cy.task into the database or through functions the app exposes on window. They solve the speed and fragility of setup problem: state need not be built through a UI that is not the subject of the test. The Cypress docs advise against using the UI for login and data setup, which is exactly the App Actions idea.

What each approach solves

Page Object

Actions the test verifies

A page's selectors and methods in one class. An interface change is fixed in one place.

App Actions

State setup

Data and session are created through the API or an app function. Fast and independent of forms.

Figure 1. They are not rivals: one owns the page, the other owns the state.

Example: from a long scenario to two layers. Before: setup goes through the UI although the test only checks the chart.

it('the chart shows 30 days', () => {
  cy.visit('/orders/new');
  cy.get('[data-cy=product]').select('Book');
  cy.get('[data-cy=qty]').type('2');
  cy.get('[data-cy=save]').click();
  // ... four more orders
  cy.visit('/statistics');
  cy.get('[data-cy=period-select]').select('30d');
  cy.get('[data-cy=stats-chart]').should('be.visible');
});

After: a command creates the orders through the API and a Page Object handles the page. This assumes POST /api/orders exists and accepts the session from cy.login.

// cypress/support/pages/statistics.page.ts
export class StatisticsPage {
  visit() {
    cy.visit('/statistics');
    return this;
  }
  selectPeriod(value: string) {
    cy.get('[data-cy=period-select]').select(value);
    return this;
  }
  chart() {
    return cy.get('[data-cy=stats-chart]');
  }
}

// the test
const stats = new StatisticsPage();
beforeEach(() => {
  cy.login('anna@example.com', 'secret');
  cy.request('POST', '/api/orders', { product: 'Book', qty: 2 });
});
it('the chart shows 30 days', () => {
  stats.visit().selectPeriod('30d').chart().should('be.visible');
});

Why this way. The test stays about one thing: the chart. A change to the order form no longer touches it, because the form takes no part in the scenario, and a change to the period selector is fixed in selectPeriod. Methods return this or a cy chain, never an element in a variable: Cypress commands are asynchronous and a saved "element" goes stale. Assertions are better left in the test: a Page Object describes what can be done, the test says what must be true.

Limits and alternatives. App Actions need a foothold in the application: an API for creating data or a function exposed only in test mode (for example when window.Cypress exists). If there is no such path, you are left with the UI or database queries through cy.task. A Page Object is not mandatory for a small suite: when a page has two selectors, a class adds a layer for nothing. There is a lighter form too: a module of functions and custom commands. In Playwright a Page Object with async methods is usually passed in through fixtures, and App Actions are done through page.evaluate() or the API.

Common mistakes.

  • Preparing state through the UI when the test checks something else: slow, and it breaks when someone else’s form changes.
  • Putting assertions and branching logic into a Page Object: the test stops showing what is verified.
  • Exposing app functions on window in production rather than for tests only: that is a backdoor for everyone.
  • A Page Object for every detail: more files than tests.

Check yourself

A test checks the filter in the orders list. Which part of the setup belongs in an App Action and which stays in the UI?

Answer

Creating the orders and logging in go to an App Action (API or command), since they are not the subject of the test. Working with the filter itself stays in the UI through a Page Object, because that is what the test checks.

Why does a Page Object return this or a cy chain instead of an element?

Answer

Cypress commands are queued and run later. An element saved in a variable can go stale, while a chain performs the lookup again every time, with retries.

In an interview. Junior: I use a Page Object so locators sit in one place and a test reads as a scenario. For state setup the API fits better than forms. Middle/Senior: A Page Object owns UI maintenance, App Actions own the speed and stability of setup. I set state through the API and go through the UI only where it is the subject of the test. I keep assertions in the test and expose window functions in test mode only.

More articles in this chapter