日本語

Keep Playwright Tests Running After a Failure with expect.soft

​​Playwright​E2E Testing,TypeScript

With Playwright's expect.soft(), a test does not stop at a failed assertion. It keeps going to the last line. Every failure is recorded, and the test is reported as failed. This is useful in E2E tests that check several display items on one screen, such as a member information or my page screen, when you want to know every incorrect item in a single run.

A normal expect() ends the test at the first failure (the official documentation on soft assertions). Here, I ran both forms in a test environment to check the difference between them, how long a failure takes, and how to stop a test partway through when you want to.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.5, and @playwright/test 1.63.0. I wrote the tests in TypeScript, used only Chromium (headless), and ran with one worker. I did not run them on Firefox or WebKit, or on a CI service.

Comparing where expect and expect.soft stop

For testing, I made a member information page that just lists a name, an email address, a membership rank, and points. The membership rank shows Silver, and the points show "1,200 pt". The tests deliberately check these two values against the wrong values, Gold and "1,500 pt".

First, here is a test that checks four items with a normal expect().

ts
import { test, expect } from '@playwright/test';
import { readFileSync } from 'node:fs';

test.beforeEach(async ({ page }) => {
  await page.setContent(readFileSync('site/profile.html', 'utf8'));
});

test('会員情報(expect)', async ({ page }) => {
  await expect(page.locator('#name')).toHaveText('山田 花子');
  await expect(page.locator('#rank')).toHaveText('ゴールド');
  await expect(page.locator('#point')).toHaveText('1,500 pt');
  await expect(page.locator('#email')).toHaveText('hanako@example.com');
  console.log('最後の行まで進んだ');
});

When I ran it, there was only one error, for the membership rank. The final console.log() text was not printed either. The points were also wrong, but the test ended before reaching them. (The output below omits the long rule lines and some of the error details.)

text
$ npx playwright test tests/hard.spec.ts --reporter=list
Running 1 test using 1 worker
  ✘  1 [chromium] › tests/hard.spec.ts:8:1 › 会員情報(expect) (5.1s)
  1) [chromium] › tests/hard.spec.ts:8:1 › 会員情報(expect) ────────────────
    Error: expect(locator).toHaveText(expected) failed
    Locator:  locator('#rank')
    Expected: "ゴールド"
    Received: "シルバー"
    Timeout:  5000ms
    ...
  1 failed

Next, I rewrote the four assertion lines from expect to expect.soft (and changed the test name). From here on, I omit the import and beforeEach, which are the same as in the first example.

ts
test('会員情報(expect.soft)', async ({ page }) => {
  await expect.soft(page.locator('#name')).toHaveText('山田 花子');
  await expect.soft(page.locator('#rank')).toHaveText('ゴールド');
  await expect.soft(page.locator('#point')).toHaveText('1,500 pt');
  await expect.soft(page.locator('#email')).toHaveText('hanako@example.com');
  console.log('最後の行まで進んだ');
});

This time, the final console.log() message was printed, and both the membership rank and the points errors were reported together. The test result was a failure (✘), and the exit code was 1.

text
$ npx playwright test tests/soft.spec.ts --reporter=list
Running 1 test using 1 worker
最後の行まで進んだ
  ✘  1 [chromium] › tests/soft.spec.ts:8:1 › 会員情報(expect.soft) (10.1s)
  1) [chromium] › tests/soft.spec.ts:8:1 › 会員情報(expect.soft) ──────────
    Error: expect(locator).toHaveText(expected) failed
    Locator:  locator('#rank')
    Expected: "ゴールド"
    Received: "シルバー"
    Timeout:  5000ms
    ...
    Error: expect(locator).toHaveText(expected) failed
    Locator:  locator('#point')
    Expected: "1,500 pt"
    Received: "1,200 pt"
    Timeout:  5000ms
    ...
  1 failed

In the HTML report (--reporter=html), under "Test Steps," all four assertions were executed, and the two failed ones were marked with ✘.

Test Steps section of the Playwright HTML report. Four rows labeled Expect "soft toHaveText" are listed. The rows for #rank and #point have a red ✘, and the rows for #name and #email have a green ✓. Both ✘ rows took 5.0s.
All four expect.soft assertions run, and the two failed ones are marked with ✘

Waiting for the timeout on every failure

In the output above, the test took 5.1 seconds with expect() and 10.1 seconds with expect.soft(). An assertion such as toHaveText(), which waits for the screen to change, keeps rechecking until the timeout if the value does not match (the official documentation on auto-retrying assertions). The default is 5 seconds (the official list of timeouts). Because expect.soft() moves on after a failure, those 5 seconds add up for each failure. In the HTML report, each of the two ✘ rows also took 5.0s.

If the test exceeds the overall test timeout (30 seconds by default), it stops there. In a test that repeats a failing expect.soft() eight times, the output showed errors up to the fifth, then Test timeout of 30000ms exceeded., and the test ended after 30.0 seconds.

ts
test('8つの表示を確かめる', async ({ page }) => {
  for (let i = 1; i <= 8; i++) {
    await expect.soft(page.locator('#rank'), `${i}つ目`).toHaveText('ゴールド');
  }
});

The second argument to expect.soft() is the message shown in the error (the official documentation on custom expect messages). In the output, each error started with its custom message (the ${i}つ目 label, which means "1st," "2nd," and so on), so I could see how far the test got. The last line of the call log for the sixth error was - Test ended., which means the test ended while it was still waiting. The seventh and eighth failures were also reported as errors, and both showed Received: "" (an empty string).

text
$ npx playwright test tests/timeout.spec.ts --reporter=list
Running 1 test using 1 worker
  ✘  1 [chromium] › tests/timeout.spec.ts:8:1 › 8つの表示を確かめる (30.0s)
    Error: 1つ目
    expect(locator).toHaveText(expected) failed
    ...(2つ目から5つ目も同じエラー)
    Test timeout of 30000ms exceeded.
    Error: 6つ目
    ...
    Error: 7つ目
    expect(locator).toHaveText(expected) failed
    Locator:  locator('#rank')
    Expected: "ゴールド"
    Received: ""
    ...
  1 failed

If you only need to check a screen whose values are already displayed, a shorter assertion timeout reduces the wait on failure.

Making everything soft with expect.configure

Instead of writing .soft every time, you can create an expect with soft enabled using expect.configure() (the official documentation on expect.configure). Here, I also set the timeout to 1 second.

ts
const softExpect = expect.configure({ soft: true, timeout: 1000 });

test('会員情報(expect.configure)', async ({ page }) => {
  await softExpect(page.locator('#name')).toHaveText('山田 花子');
  await softExpect(page.locator('#rank')).toHaveText('ゴールド');
  await softExpect(page.locator('#point')).toHaveText('1,500 pt');
  await softExpect(page.locator('#email')).toHaveText('hanako@example.com');
});

As with expect.soft(), the membership rank and points errors were both reported. The timeout in the errors was 1000ms, and the test took 2.1 seconds instead of 10.1 seconds.

text
$ npx playwright test tests/configure.spec.ts --reporter=list
Running 1 test using 1 worker
  ✘  1 [chromium] › tests/configure.spec.ts:10:1 › 会員情報(expect.configure) (2.1s)
    Error: expect(locator).toHaveText(expected) failed
    Locator:  locator('#rank')
    Expected: "ゴールド"
    Received: "シルバー"
    Timeout:  1000ms
    ...
    Error: expect(locator).toHaveText(expected) failed
    Locator:  locator('#point')
    Expected: "1,500 pt"
    Received: "1,200 pt"
    Timeout:  1000ms
    ...
  1 failed

Stopping before the next action when there is a failure

In a test that checks some display and then continues with actions, such as pressing a button, going further may be pointless if the display is already wrong. In that case, you can check the errors recorded so far with test.info().errors (the official documentation on TestInfo errors).

ts
test('表示を確かめてから編集する', async ({ page }) => {
  await expect.soft(page.locator('#rank')).toHaveText('ゴールド');
  await expect.soft(page.locator('#point')).toHaveText('1,500 pt');

  console.log('ここまでの失敗の数:', test.info().errors.length);
  // If the display fails, stop here.
  expect(test.info().errors.length).toBe(0);

  await page.getByRole('button', { name: '編集する' }).click();
  await expect(page.locator('#msg')).toHaveText('編集画面を開きました');
});

When I ran it, the failure count was 2. The normal expect() with toBe(0) failed, and the test ended there. The button was not clicked.

text
$ npx playwright test tests/stop.spec.ts --reporter=list
Running 1 test using 1 worker
ここまでの失敗の数: 2
  ✘  1 [chromium] › tests/stop.spec.ts:8:1 › 表示を確かめてから編集する (10.1s)
    ...(会員ランクとポイントの2つのエラー)
    Error: expect(received).toBe(expected) // Object.is equality
    Expected: 0
    Received: 2
    > 14 |   expect(test.info().errors.length).toBe(0);
    ...
  1 failed

The official example writes expect(test.info().errors).toHaveLength(0) on the array itself. Here, I compared only the number of errors with toBe(0).

Point

expect.soft() only keeps going past assertion failures. If an action such as a click fails, the test ends there, as usual.

I tested this with a expect.soft() followed by a click on a Save button that does not exist on the page. The click failed with a 2-second timeout, and the following expect.soft() and console.log() did not run. The errors were the expect.soft() failure for the membership rank and a TimeoutError from the click.

ts
test('ボタンの名前を間違えた', async ({ page }) => {
  await expect.soft(page.locator('#rank')).toHaveText('ゴールド');
  await page.getByRole('button', { name: '保存する' }).click({ timeout: 2000 });
  await expect.soft(page.locator('#point')).toHaveText('1,500 pt');
  console.log('最後の行まで進んだ');
});
text
$ npx playwright test tests/action.spec.ts --reporter=list
Running 1 test using 1 worker
  ✘  1 [chromium] › tests/action.spec.ts:8:1 › ボタンの名前を間違えた (7.1s)
    Error: expect(locator).toHaveText(expected) failed
    Locator:  locator('#rank')
    ...
    TimeoutError: locator.click: Timeout 2000ms exceeded.
    Call log:
      - waiting for getByRole('button', { name: '保存する' })
    ...
  1 failed

Inside test.step, only that step fails

When the checks are split up with test.step(), a failing expect.soft() marks only the step it is in as failed, and the test moves on to the next step. In the following test, the membership rank step fails, and the points step passes with the correct value (1,200 pt).

ts
test('ステップに分けて確かめる', async ({ page }) => {
  await test.step('会員ランク', async () => {
    await expect.soft(page.locator('#rank')).toHaveText('ゴールド', { timeout: 1000 });
  });
  await test.step('ポイント', async () => {
    await expect.soft(page.locator('#point')).toHaveText('1,200 pt');
  });
  console.log('最後の行まで進んだ');
});

In the list reporter, the final console.log() message was printed. The error heading included the test name followed by the name of the failed step, membership rank.

text
$ npx playwright test tests/step.spec.ts --reporter=list
Running 1 test using 1 worker
最後の行まで進んだ
  ✘  1 [chromium] › tests/step.spec.ts:8:1 › ステップに分けて確かめる (1.1s)
  1) [chromium] › tests/step.spec.ts:8:1 › ステップに分けて確かめる › 会員ランク ──────
    Error: expect(locator).toHaveText(expected) failed
    ...
  1 failed

In the HTML report, the membership rank step had a ✘, and the points step had a ✓.

Test Steps section of the Playwright HTML report. The membership rank step has a red ✘ and 1.0s, and the points step has a green ✓ and 4ms.
Only the step where expect.soft failed is marked with ✘, and the next step still runs

​Posted by 86