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

Patterns › Facade, Steps and App Actions

App Actions: preconditions without the UI

App Actions means preparing test preconditions bypassing the interface: through the app's internal actions, an API, the database or a saved session. Only what the test verifies goes through the UI. Playwright offers storageState with a setup project, the request fixture and page.route. A few end-to-end UI tests are still needed, otherwise a broken login form goes unnoticed.

Patterns · App Actions

Every test logs in through the form and the run takes twenty minutes. What can be skipped?

In a suite of two hundred tests each one starts with the login form: a navigation, two fields, a button, waiting for the redirect. That is a few seconds times two hundred, plus two hundred chances to fail on a form the test has nothing to do with. A transfer test should not depend on whether the Email field works. App Actions is the approach where preconditions are prepared bypassing the interface.

How it works and why. The term came from the Cypress blog (2019): drive the application directly through its internal model that it exposes to tests, for example an object on window, instead of clicking the UI. Today it names a wider idea: prepare state by the cheapest reliable path, that is through the API, the DB or a saved session. The principle is simple: only what the test verifies goes through the UI. Everything else is a precondition, and the less it depends on the interface, the faster and more stable the test.

Playwright has three mechanisms for this:

  • storageState + a setup project. Log in once and save cookies and localStorage to a file. Other projects plug it in with use: { storageState }.
  • The request fixture. Creates data through the API before the test (an account, a user), and a fixture removes it afterwards.
  • page.route. Replaces backend responses when a specific state is needed that is hard to create (for example, a server error).

What to prepare where

UI login in every testa form, a redirect, N times per runAPI login once in setupcookies to a file, tests start logged inThe login test itselfthe form and messages: UI belongs here
Figure 1. The UI path is kept for what the test checks, the rest is prepared cheaper.

Example: before and after. Before: a login in beforeEach of every file.

test.beforeEach(async ({ page }) => {
  await page.goto('/login');
  await page.getByLabel('Email').fill('qa@example.com');
  await page.getByLabel('Password').fill(process.env.QA_PASS!);
  await page.getByRole('button', { name: 'Sign in' }).click();
  await page.waitForURL('/dashboard');
});

After: a separate setup file logs in through the API once.

// auth.setup.ts
import { test as setup, expect } from '@playwright/test';

setup('authenticate', async ({ request }) => {
  const res = await request.post('/api/login', {
    data: { email: 'qa@example.com', password: process.env.QA_PASS },
  });
  expect(res.ok()).toBeTruthy();
  await request.storageState({ path: '.auth/user.json' });
});

In the config the setup is a project, and the main project depends on it and takes the saved state:

// playwright.config.ts
export default defineConfig({
  use: { baseURL: 'http://localhost:3000' },
  projects: [
    { name: 'setup', testMatch: /.*\.setup\.ts/ },
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'], storageState: '.auth/user.json' },
      dependencies: ['setup'],
    },
  ],
});

The .auth/ folder goes to .gitignore: it holds live sessions. The setup project runs before every run, so the state does not go stale between runs.

Limits and alternatives. The main trap: request.storageState() saves cookies. If the app keeps the token in localStorage, an API login does not carry it over, and the page opens logged out. Then the token from the response is added to the state's origins (code below), or setup logs in through the UI once. Playwright does not save sessionStorage in storageState at all: for it there is only a workaround through addInitScript.

// the token lives in localStorage: cookies are not enough, origins must be added
const { token } = await res.json();
const state = await request.storageState();
state.origins = [
  {
    origin: 'http://localhost:3000',
    localStorage: [{ name: 'token', value: token }],
  },
];
fs.writeFileSync('.auth/user.json', JSON.stringify(state));

Where the UI is needed and where it is not

The subject

A login test

Goes through the form in the UI: that is the check.

A precondition

A transfer test

The login comes from a saved state, the form is not needed.

Figure 2. Both paths are legitimate: the choice depends on what the test checks.

Two more limits. Tests that change the user's data cannot run in parallel on one account: an account per worker is needed (see the lesson on worker fixtures). And a few end-to-end tests must remain that go through the key path fully via the UI: otherwise a broken login form goes unnoticed, because all other tests bypass it.

Common mistakes.

  • A UI login in every test that is not about login: slow, and the form becomes the cause of unrelated failures.
  • One account for all workers while tests change its data: races and flakes that are hard to reproduce.
  • A committed .auth/user.json: live cookies end up in the repository.
  • The token is in localStorage but only cookies are saved: the test opens logged out and fails on the first click.
  • No UI test for the login itself: the bypass everywhere hides a broken form.

Check yourself

The API login in setup passed (200) but tests open /login. The app stores the token in localStorage. Why and what to do?

Answer

request.storageState() saves cookies and localStorage stays empty, so the page does not see the token. Add the token from the response to the state's origins[].localStorage, or log in through the UI once in setup.

Should the test of the login form itself use storageState?

Answer

No. The form and its error messages are the subject there, so the test should start logged out (use: { storageState: { cookies: [], origins: [] } }) and go through the UI.

In an interview. Junior: I prepare preconditions through the API and a saved state, not through the UI: it is faster and more stable. I go through the UI only for what the test checks. Middle/Senior: I log in in a setup project and save storageState. I remember it holds cookies and localStorage but not sessionStorage, so a token in localStorage has to be added. I split user data per worker and keep a few end-to-end UI tests so a broken form is not missed.

More articles in this chapter