Playwright Soft Assertions

A teacher marking an exam paper circles every wrong answer and still reads to the last question. A strict teacher who stopped at the first mistake would hide all the others. Soft assertions bring this habit to testing. They record each failed check, keep the test running, and report every problem together at the end.

Hard Assertions Stop Early

A normal expect throws an error the moment a check fails. The test halts, and every later line stays unexecuted. A page with five wrong values reveals only the first one, so the team fixes one bug, reruns the test, and meets the second bug.

Soft Assertions Keep Going

The expect.soft method notes the failure, marks the test as failed, and lets the next line run.

await expect.soft(page.getByTestId('name')).toHaveText('Asha Rao');
await expect.soft(page.getByTestId('email')).toHaveText('asha@example.com');
await expect.soft(page.getByTestId('city')).toHaveText('Pune');

Flow Comparison

HARD ASSERTIONS                     SOFT ASSERTIONS

Check 1  pass                       Check 1  pass
Check 2  FAIL  -> test stops        Check 2  FAIL  -> noted
Check 3  never runs                 Check 3  FAIL  -> noted
Check 4  never runs                 Check 4  pass

Report: 1 problem                   Report: 2 problems

The Test Still Fails

A soft failure does not disappear. Playwright marks the test as failed after the last line runs, and the report lists every recorded problem with its location. Soft assertions change when the test stops, and they never change the final verdict.

Best Uses for Soft Assertions

  • Profile pages with many independent fields.
  • Invoices, receipts, and summary screens.
  • Table rows where each cell holds a separate fact.
  • Dashboards with several counters.

Mixing Soft and Hard Checks

Some checks protect the rest of the test. A failed login makes every later step meaningless. Use a hard assertion for such gate checks and soft assertions for the details that follow.

await expect(page).toHaveURL(/dashboard/);              // hard gate

await expect.soft(page.getByTestId('balance')).toHaveText('$1,200');
await expect.soft(page.getByTestId('plan')).toHaveText('Premium');

Stopping After Soft Failures

A test can inspect its own error list and stop on demand.

expect(test.info().errors).toHaveLength(0);

This line works as a checkpoint. Any earlier soft failure stops the test at that spot.

A Real-World Scenario

An online store shows an order summary with six values: item name, quantity, price, tax, shipping, and total. A tester writes one test with six soft assertions. A recent update breaks both tax and shipping. The report lists both defects in a single run, and the developer fixes them together.

Cautions

Soft assertions suit independent facts. Steps that depend on each other need hard assertions, because a soft failure early in a chain can trigger confusing errors later. A test with dozens of soft checks becomes hard to read, so split large pages into several focused tests.

Quick Practice Task

Write a test against any page with four visible texts. Check each text with expect.soft, and type one wrong expected value on purpose. Run the test and read how the report points to the single failing line.

Common Questions

Do soft assertions slow a test?

No. The runner simply keeps executing lines after a failed check. Total time stays similar, and the report gains more information.

Can a test use soft assertions for every check?

A test can, though gate checks such as a successful login deserve hard assertions. A soft failure at the gate would send the later steps into confusing errors.

How does the report show soft failures?

The report lists each failed check with its file, line number, expected value, and actual value, all inside one test result.

Key Takeaways

  • A soft assertion records a failure and continues the test.
  • The final result stays failed when any soft check fails.
  • Hard assertions guard steps that later lines depend on.
  • One run exposes many defects, which shortens the fix cycle.