Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS MATH

Playwright Soft Assertions


Share

Playwright Soft Assertions

A soft assertion records a failure but lets the test keep running.

This chapter also shows how to give an assertion your own message and timeout.


Soft Assertions

A normal assertion, like the ones in the Assertions chapter, stops the test when it fails.

With expect.soft(), the test is marked as failed, but it continues.

So you see all the problems in one run.

This test checks three things on the practice shop page, which shows 5 products and an empty cart:

Example

tests/soft.spec.js

// @ts-check
import { test, expect } from '@playwright/test';

test('soft assertions', async ({ page }) => {
  await page.goto('https://www.w3schools.com/playwright/practice/shop.html');
  await expect.soft(page.getByRole('status')).toHaveText('6 products');
  await expect.soft(page.getByTestId('cart-total')).toHaveText('$0.00');
  await expect.soft(page.getByTestId('cart-count')).toHaveText('1');
  await page.getByLabel('Search products').fill('Webcam');
  await expect(page.getByTestId('product')).toHaveCount(1);
});

Two of the soft assertions are wrong: the shop has 5 products, not 6, and the cart count is 0, not 1.

The test still runs to the end, and reports both failures:

Output:

Running 1 test using 1 worker

[1/1] [chromium] › tests/soft.spec.js:4:1 › soft assertions
  1) [chromium] › tests/soft.spec.js:4:1 › soft assertions ─────────────────────────────────────────

    Error: expect(locator).toHaveText(expected) failed

    Locator:  getByRole('status')
    Expected: "6 products"
    Received: "5 products"
    Timeout:  5000ms

    Call log:
      - Expect "soft toHaveText" getByRole('status') with timeout 5000ms
      - waiting for getByRole('status')
        14 × locator resolved to <p id="status" role="status">5 products</p>
           - unexpected value "5 products"


      4 | test('soft assertions', async ({ page }) => {
      5 |   await page.goto('https://www.w3schools.com/playwright/practice/shop.html');
    > 6 |   await expect.soft(page.getByRole('status')).toHaveText('6 products');
        |                                               ^
      7 |   await expect.soft(page.getByTestId('cart-total')).toHaveText('$0.00');
      8 |   await expect.soft(page.getByTestId('cart-count')).toHaveText('1');
      9 |   await page.getByLabel('Search products').fill('Webcam');
        at /home/user/my-project/tests/soft.spec.js:6:47

    Error: expect(locator).toHaveText(expected) failed

    Locator:  getByTestId('cart-count')
    Expected: "1"
    Received: "0"
    Timeout:  5000ms

    Call log:
      - Expect "soft toHaveText" getByTestId('cart-count') with timeout 5000ms
      - waiting for getByTestId('cart-count')
        14 × locator resolved to <span data-testid="cart-count">0</span>
           - unexpected value "0"


       6 |   await expect.soft(page.getByRole('status')).toHaveText('6 products');
       7 |   await expect.soft(page.getByTestId('cart-total')).toHaveText('$0.00');
    >  8 |   await expect.soft(page.getByTestId('cart-count')).toHaveText('1');
         |                                                     ^
       9 |   await page.getByLabel('Search products').fill('Webcam');
      10 |   await expect(page.getByTestId('product')).toHaveCount(1);
      11 | });
        at /home/user/my-project/tests/soft.spec.js:8:53

  1 failed
    [chromium] › tests/soft.spec.js:4:1 › soft assertions ──────────────────────────────────────────

Use soft assertions for many small checks on one page, like the labels of a form.



Custom Message and Timeout

Pass a message as the second argument of expect().

Playwright shows it first when the assertion fails, so the report says what went wrong in your own words.

Every web-first assertion also takes a timeout option, in milliseconds.

The todo list starts empty, so after one new todo the counter says "1 item left".

This test expects "2 items left", so it fails:

Example

tests/message.spec.js

// @ts-check
import { test, expect } from '@playwright/test';

test('custom message and timeout', async ({ page }) => {
  await page.goto('https://www.w3schools.com/playwright/practice/todo.html');
  await page.getByPlaceholder('What needs to be done?').fill('Buy milk');
  await page.getByPlaceholder('What needs to be done?').press('Enter');

  await expect(
    page.getByTestId('todo-count'),
    'The counter should include the new todo'
  ).toHaveText('2 items left', { timeout: 2000 });
});

Output:

Running 1 test using 1 worker

[1/1] [chromium] › tests/message.spec.js:4:1 › custom message and timeout
  1) [chromium] › tests/message.spec.js:4:1 › custom message and timeout ───────────────────────────

    Error: The counter should include the new todo

    expect(locator).toHaveText(expected) failed

    Locator:  getByTestId('todo-count')
    Expected: "2 items left"
    Received: "1 item left"
    Timeout:  2000ms

    Call log:
      - The counter should include the new todo getByTestId('todo-count') with timeout 2000ms
      - waiting for getByTestId('todo-count')
        20 × locator resolved to <p id="count" data-testid="todo-count">1 item left</p>
           - unexpected value "1 item left"


      10 |     page.getByTestId('todo-count'),
      11 |     'The counter should include the new todo'
    > 12 |   ).toHaveText('2 items left', { timeout: 2000 });
         |     ^
      13 | });
      14 |
        at /home/user/my-project/tests/message.spec.js:12:5

  1 failed
    [chromium] › tests/message.spec.js:4:1 › custom message and timeout ────────────────────────────

The error starts with the custom message.

The assertion gave up after 2 seconds instead of the default 5 seconds.

To change the timeout for all assertions, see the Waits and Timeouts chapter.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.