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

Playwright › Fixtures and test structure

test.step and the describe.configure modes

test.step wraps part of a test in a named block, so the report and trace show a tree of business steps instead of a flat list of actions. The test.describe.configure modes control distribution: by default tests of a file run in order, parallel spreads them across workers, and serial binds the group: after a failure the rest is skipped and a retry restarts the group. Serial suits only inseparable scenarios.

Playwright · Fixtures

The report shows fifty clicks and requests. At which step did the order break?

An order checkout test failed, and you open the report. It is a flat list: click, fill, click, wait, another click. To tell whether payment or the status check broke, you have to read lines of code. A second pain of the same kind: the tests of a file are independent but run one after another in a single worker, so the run is long, or the opposite, they were made dependent and one failure cancels the rest. This lesson covers two tools: test.step makes the report readable, and test.describe.configure controls how tests are distributed across workers.

How test.step works and why. A step wraps part of a test in a named block. In the HTML report and trace, instead of a flat list of actions you get a tree of business steps: "Place the order", "Pay", "Check the status". Steps can be nested, a step returns the value from its callback, and if an assertion fails inside, the step is marked as failed and the error is attached to it. This does not change the test logic: a step only adds structure to the report. The scenario reads like a specification.

The same failed test in a report

Typical

Without steps

A flat list of dozens of actions. To tell which business action broke, you read the test code line by line.

Better

With test.step

A tree: "Place the order", "Pay" (with a nested "Enter the card"), "Check the status". The red step shows the stage at once.

Figure 1. Steps add no checks: they only group actions so that the report reads like a scenario.
test('purchase', async ({ page }) => {
  const orderId = await test.step('Place the order', async () => {
    await page.getByRole('button', { name: 'Buy' }).click();
    return page.getByTestId('order-id').innerText();
  });

  await test.step('Pay', async () => {
    await test.step('Enter the card', async () => {
      await page.getByLabel('Card number').fill('4242424242424242');
    });
    await page.getByRole('button', { name: 'Pay' }).click();
  });

  await test.step('Check the status', async () => {
    await expect(page.getByText('Order ' + orderId)).toBeVisible();
  });
});

Steps are convenient to wrap in Page Object methods, so that every business action shows up in the report as one line. The { box: true } option "boxes" the step: the error points to the step call in the test, not to the line inside the helper. This reads better when the helper is called from many tests.

import { expect, test, type Page } from '@playwright/test';

export class CheckoutPage {
  constructor(private readonly page: Page) {}

  async pay(card: string) {
    await test.step(
      'Pay by card',
      async () => {
        await this.page.getByLabel('Card number').fill(card);
        await this.page.getByRole('button', { name: 'Pay' }).click();
        await expect(this.page.getByText('Payment accepted')).toBeVisible();
      },
      { box: true }, // a failure points at checkout.pay(...) in the test, not at this line
    );
  }
}

The limit of reason: a step per click makes the report longer than the test itself. A step should match an action the business would name: "Pay", not "Click the button".

describe.configure modes. By default different files run in parallel across workers, while tests inside a file run in order in one worker. A failed test does not cancel the others: the worker is restarted and the following tests go on in the new one. The mode can be changed at file or block level.

ModeBehaviorWhen
defaulttests of a file sequentially in one worker; a failure does not cancel the othersthe usual case, files are independent
paralleltests of the block are distributed across workerstests share no data and order does not matter
serialsequentially; after a failure the rest is skipped, and on a retry the whole group restarts together with beforeAlla long dependent scenario, an exception

In parallel mode the tests of a group may land in different workers, so each test runs its own beforeAll and afterAll, and sharing variables between tests will not work. Serial mode does the opposite and binds the group into one dependent sequence:

Serial: a failure and a retry

test 1: passed test 2: failed test 3: skipped retry:the whole groupfrom test 1 default: a failure does not cancel other testsserial: after a failure the rest is skipped
Figure 2. The price of serial: skipped tests and a repeat of the whole chain on a retry.
// the whole file: tests run in parallel in different workers
test.describe.configure({ mode: 'parallel' });

test.describe('Registration wizard', () => {
  // one long scenario: steps depend on each other
  test.describe.configure({ mode: 'serial', retries: 1, timeout: 90_000 });

  let page: Page;
  test.beforeAll(async ({ browser }) => {
    page = await browser.newPage(); // one page for the whole chain
  });
  test.afterAll(async () => {
    await page.close();
  });

  test('step 1: personal data', async () => {
    await page.goto('/signup');
  });
  test('step 2: documents', async () => {
    // skipped if step 1 failed; on a retry the group starts from step 1
  });
});

Through configure you can also set retries and timeout for a group. The global fullyParallel: true in the config equals the parallel mode for all files. Serial mode is not a replacement for workers: 1: it only binds the tests of the group, while other files still run in parallel.

// playwright.config.ts
export default defineConfig({
  fullyParallel: true, // = mode 'parallel' for all files
  workers: process.env.CI ? 4 : undefined,
});

Example: before → after. A registration wizard was made serial with two steps, and the first step sometimes fails on a slow environment. Consequence: the second test is skipped, the report shows one red and one grey, and a retry reruns both. The fix follows the rule "a test starts from the needed state": the second test does not depend on the first, and the preparation (a draft application) is created by a fixture through the API. Then a failure of the first step does not mask the second, each test can be run alone with --grep, and parallel mode lets them run at the same time.

Limits and alternatives. Serial stays an exception for truly indivisible scenarios: a test cannot be run alone, a failure of the first step skips the rest, and a retry repeats the chain entirely. For saving time on login serial is not needed: it breaks isolation, and the saving is delivered by storageState. Steps are not assertions: a step with actions and no checks will be "green". Cypress has no built-in steps with a tree in the report, usually cy.log is used, and a sequence of it blocks in a file also ties tests by order there, so the same risks of dependent tests remain.

Common mistakes.

  • A step per click: the report becomes noisy and grouping gives no value.
  • serial "to save time" on login: it breaks isolation, and the saving is delivered by storageState.
  • parallel for tests that change a shared DB record: flakiness appears.
  • Expecting beforeAll in a serial group to run once: on a retry it starts again.
  • A step with actions and no assertions: it is "green" although nothing was checked.

Check yourself

In a serial group of three tests the second failed. Which tests run on the retry if retries: 1?

Answer

The group repeats entirely: from the first test and with a new run of beforeAll. The third test was skipped on the first attempt, and on the retry it runs if the second passes. That is why expensive steps in a serial group get repeated, and the tool suits only truly indivisible scenarios.

The pay() helper is wrapped in test.step without box, and an assertion inside failed. Where does the error point in the report, and what changes with box: true?

Answer

Without box the error points to the assertion line inside the helper. With box: true it points to the checkout.pay(...) call in the test, the place where the scenario turned to the step. This is handy for helpers called from many tests.

In an interview. Junior: test.step divides a test into named steps, and the report shows at which stage it failed. Middle/Senior: I make steps at the level of business actions and wrap Page Object methods in them with box: true. I rarely use serial mode: on a retry it restarts the whole group, and a single test cannot be run alone. Instead I make tests independent, prepare data through the API or a fixture, and enable parallel for files where tests do not share state.

More articles in this chapter