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

Cypress › Framework architecture

Page Object vs App Actions

Page Object and App Actions are not alternatives: the first controls how a test interacts with the interface, the second the state it starts from. The choice depends on what the test checks: steps under test go through a Page Object, and the starting state is prepared through App Actions. One test stays end to end through the form, so a bug in it does not pass unnoticed.

Cypress · Patterns and data

A test adds a task through a form although it only checks a counter. What is the right design?

Page Object and App Actions are often presented as "either-or", and arguments follow in code review. In fact they are two different knobs: one controls how a test interacts with the interface, the other how it prepares state. The lesson compares them by cost, risk and limits rather than repeating what they are; for the plain definitions see the lesson "Page Object or App Actions: which do you choose?".

How it works and why. A Page Object works at the interface level: a class wraps selectors and actions, and the test calls methods. It costs a layer: one more file to maintain and the temptation to hide logic in it. App Actions work beneath the interface: the test calls the API, cy.task or an application function. They are cheap in run time but more tightly coupled to the implementation: a change in the API or an internal function breaks the setup. So the choice becomes "what does this test check": if the interface, use a Page Object for its steps and create everything else with App Actions.

Two layers under a test

Testthe scenario and assertionsPage ObjectUI steps under testApp Actionsstate: API, cy.task, window
Figure 1. Assertions stay in the test, UI steps in the Page Object, setup beneath.

Example: an App Action through window. Assumptions: a React app with a store, and the team agreed to expose actions in test mode only. In the application code:

// src/main.tsx
if ((window as any).Cypress) {
  (window as any).appActions = {
    addTask: (title: string) => store.dispatch(addTask(title)),
  };
}

In the test: the counter is checked at once, with no form. A Page Object is needed only where the UI is the subject of the check.

it('the counter shows 2', () => {
  cy.visit('/tasks');
  cy.window().its('appActions').invoke('addTask', 'Buy milk');
  cy.window().its('appActions').invoke('addTask', 'Call bank');
  cy.get('[data-cy=counter]').should('have.text', '2');
});

// and a scenario where the UI is the very subject
class TasksPage {
  row(title: string) { return cy.contains('[data-cy=task]', title); }
  complete(title: string) {
    this.row(title).find('[data-cy=done]').click();
    return this;
  }
}
it('the checkbox strikes the task out', () => {
  cy.visit('/tasks');
  cy.window().its('appActions').invoke('addTask', 'Buy milk');
  new TasksPage().complete('Buy milk').row('Buy milk').should('have.class', 'done');
});

Why this way. The first test does not depend on how the add form looks. The second uses the App Action for the start only and does the real check through the Page Object. its() retries until the application has registered the actions. The drawback is clear: the test knows an internal function, so renaming it breaks both tests. That is why such actions are kept in a narrow, named object rather than exposing the whole store.

SituationChoice
UI behaviour is checkedPage Object
Initial state is needed and the UI is not the subjectApp Actions: API or cy.task
No API and no hook in the applicationUI, but inside a custom command
A small suite, one pageNo layer: direct cy calls

Limits and alternatives. Actions exposed on window work only in browser e2e: they must not exist in production, and a window.Cypress check in the app means it knows about tests. Alternatives: a dedicated test API, cy.task with direct database access, and for isolated components Cypress component tests. In Playwright, Page Object pages are usually passed in through fixtures, and App Actions are done through page.evaluate() or the API.

Common mistakes.

  • Saying "Page Object is obsolete": it is fine for UI steps, just not for state setup.
  • Exposing the whole store on window without tying it to test mode: an open backdoor.
  • Putting assertions in a Page Object: one method both acts and checks, and the test does not show the expectation.
  • Using App Actions for something that must be tested through the UI: the test misses a broken form.

Check yourself

The add-task form has a bug: the button sends nothing. The test with the addTask App Action passes. Why and what do you do?

Answer

The App Action bypasses the form, so the bug is invisible. You need a separate test that goes through the UI (a Page Object), while App Actions stay for tests where the form is not the subject.

Why are the actions on window registered under a window.Cypress condition?

Answer

So they are not available to ordinary users: a test hook must not be reachable in production. It is not a security measure, only hygiene, so the actions are kept narrow and safe.

In an interview. Junior: A Page Object gathers a page’s selectors and actions in one class. App Actions prepare state directly, without the UI. I combine both: the steps the test verifies go through a Page Object. Middle/Senior: They are different knobs: a Page Object controls UI interaction, App Actions control state. I choose by what the test checks, keep window actions narrow and test-mode only, and leave one test end to end through the form so a bug there does not pass unnoticed.

More articles in this chapter