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

Playwright › Auto-waiting and web-first assertions

Soft assertions, messages and custom matchers

On failure expect.soft does not stop the test but records the error, so one run shows all the mismatches in the report while the test still ends red. Soft is used for independent result checks, while preconditions stay a regular expect to avoid a cascade of false failures. A message goes in the second argument of expect, and repeated checks move into custom matchers with expect.extend.

Playwright · Waiting

A product card with six fields: fix one and the test fails on the next. How do you see them all at once?

A test checks a product card: price, SKU, photo, button. It fails on the price. The developer fixes it, reruns, and now the SKU fails. Then the photo. Three runs to see three small mismatches that were visible from the start. The reason is that a plain expect throws and stops the test at the first failure. That is right for preconditions but wasteful for a set of independent checks. This lesson covers expect.soft, custom messages, expect.configure and expect.extend.

How it works and why. expect.soft checks exactly like a regular assertion (with the same retrying for web-first ones), but on failure it does not throw: it records the error and lets the test move on. At the end the test is marked as failed, and the report shows all recorded mismatches together. The design is logical: one field failing does not make the other fields unclear, so stopping gains nothing and a run costs time.

Three mismatches: how many runs it takes

expect

Run 1: the price fails, the rest is unchecked. Run 2: the SKU fails. Run 3: the photo fails. Three launches, three fixes one by one.

expect.soft

One run: the report holds the price, the SKU and the photo together. All mismatches are visible at once and can be fixed in one iteration.

Figure 1. Soft does not make a test softer: it stays red, the report is just complete.
await expect.soft(page.getByLabel('Name')).toHaveValue('Rostyslav');
await expect.soft(page.getByLabel('Email')).toHaveValue('r@test.com');
await expect.soft(page.getByLabel('Phone')).toHaveValue('+380501112233');
expect(test.info().errors).toHaveLength(0);
await page.getByRole('button', { name: 'Save' }).click();

The expect(test.info().errors) line is a safeguard: if the soft assertions have already failed, going on makes no sense, because the next step would work with wrong data. The regular expect stops the test at this point, and test.info().errors holds all the soft failures recorded earlier.

Example: what is soft and what is not. The original version uses regular assertions:

// before: the first failure hides the rest
await page.goto('/products/42');
await expect(page.getByTestId('price')).toHaveText('1,250 UAH');
await expect(page.getByTestId('sku')).toHaveText('SKU-42');
await expect(page.getByRole('button', { name: 'Add to cart' })).toBeEnabled();

When rewriting, split the checks into two kinds. Preconditions: without them the rest is meaningless (the page opened, the heading is there). They stay regular, because otherwise a failed navigation makes every later assertion fail and fills the report with a dozen identical errors. Independent result checks: each says something of its own, so they become soft.

// after
test('product page shows all data', async ({ page }) => {
  await page.goto('/products/42');
  await expect(page.getByRole('heading', { level: 1 })).toBeVisible();   // precondition

  await expect.soft(page.getByTestId('price')).toHaveText('1,250 UAH');
  await expect.soft(page.getByTestId('sku')).toHaveText('SKU-42');
  await expect.soft(page.getByRole('img', { name: 'Product photo' })).toBeVisible();
  await expect.soft(page.getByRole('button', { name: 'Add to cart' })).toBeEnabled();
});

A custom message. The second argument of expect (and expect.soft) is a string: expect(locator, 'the pay button must be enabled').toBeEnabled(). It appears in the report, and the failure reads without opening the code. This helps most where the assertion itself says little: toBe(true), a check in a loop, expect.poll, where all you see is "the value did not become that".

expect.configure. Returns a configured expect instance with its own timeout or mode. It is cleaner than adding timeout to every call:

const slowExpect = expect.configure({ timeout: 15_000 });
await slowExpect(page.getByTestId('report')).toBeVisible();   // up to 15 seconds

const softExpect = expect.configure({ soft: true });
await softExpect(page.getByTestId('price')).toHaveText('1,250 UAH');

expect.extend: custom matchers. If one check repeats in many tests, move it into a matcher: it returns pass and message (a function), optionally name, expected, actual. A simplified example for a Ukrainian IBAN (29 characters, no checksum validation):

import { expect as baseExpect } from '@playwright/test';

export const expect = baseExpect.extend({
  toHaveValidIban(received: string) {
    const pass = /^UA\d{2}[A-Z0-9]{25}$/.test(received);
    return {
      pass,
      name: 'toHaveValidIban',
      message: () => 'expected a Ukrainian IBAN, received "' + received + '"',
    };
  },
});

// in a test (import our expect instead of the standard one)
expect(await page.getByTestId('iban').innerText()).toHaveValidIban();

Limits and alternatives. Soft does not solve everything. It does not suit places where the next step depends on the previous one: a click after a failed check runs in a wrong state, so between groups of soft assertions you put a "gate" with test.info().errors. The number of soft failures is not limited, and a very long report can hide the main point: split groups of checks into several tests. A custom matcher that works with a ready value does not retry: if you need waiting, build it on web-first assertions or combine it with expect.poll. And for a one-off check a plain function is enough, a matcher is overkill. In the Java world similar behaviour comes from assertAll in JUnit 5 and SoftAssertions in AssertJ.

Common mistakes.

  • Soft on preconditions: a cascade of false failures, and the real cause is hard to find.
  • Soft without checking test.info().errors before an action: the test goes on with a corrupted state, and side effects have already happened.
  • A custom message on every assertion: noise people stop noticing. It belongs where the assertion itself is unclear.
  • A matcher for a one-off check: a plain function reads better.
  • Importing the standard expect from @playwright/test instead of the extended one: it has no custom matcher, and TypeScript will say so.

Check yourself

A test opens the profile page with expect.soft(page).toHaveURL(/profile/), then runs six soft field checks. Navigation failed. What will the report show and what should change?

Answer

The report will contain the URL failure plus six field failures, because the page is not there: the cascade drowns the real cause. A check without which the rest is meaningless is made a regular expect: the test stops right there, and soft stays for independent result checks.

A test with three soft assertions reached its last line, yet the report shows it red. Is that a runner bug?

Answer

No: soft assertions do not throw at once, they record the error, and at the end the test is marked failed. All recorded failures sit in test.info().errors and show in the report. A red status after a soft failure is expected: soft changes the moment of stopping, not the result.

In an interview. Junior: expect.soft does not stop the test at the first failure, so the report shows all mismatches in one run; the test still ends red. Middle/Senior: I use soft for independent result checks and keep preconditions regular, so there is no cascade of false failures. Before an action that depends on the checks I look at test.info().errors. I add messages to non-obvious assertions, take a configured timeout through expect.configure, and move repeated checks into matchers via expect.extend.

More articles in this chapter