Playwright + JS · Lesson 19 of 35

0/35 done

Module 19: Assertions

19. Web-First Retrying Assertions & Soft Validations

17 minlesson

Try it yourself

Implement formatAssertionResult:

  1. Accept field, expected, and actual strings.
  2. Return Assertion Failed: [${field}] Expected '${expected}', but received '${actual}'..

The editor has a worked example. Read it, tweak it, then press Mark as complete.

Web-First Assertions with Automatic Retry

javascript
// Visibility & State:
await expect(page.getByRole("button", { name: "Submit" })).toBeVisible();
await expect(page.getByRole("button", { name: "Submit" })).toBeEnabled();
await expect(page.getByLabel("Subscribe")).toBeChecked();

// Text & Values:
await expect(page.getByTestId("cart-count")).toHaveText("3");
await expect(page).toHaveURL("https://targetqa.com/dashboard");

// Soft assertions (allow test to continue after failure):
await expect.soft(page.getByTestId("subtotal")).toHaveText("$50.00");
await expect.soft(page.getByTestId("tax")).toHaveText("$5.00");

Key Takeaways & Walkthrough Summary

Flaky tests often happen when assertions run before the page has finished rendering. Playwright solves this with Web-First Assertions: assertions that automatically retry until the condition passes or timeout occurs. Let's start asserting!

Part of the Playwright + JS course (Playwright with JavaScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Why do web-first assertions require 'await'?

Because they poll the browser asynchronously until the condition is met or the timeout expires.

Finished reading?

Review the worked example in the editor, then mark the lesson complete.

JavaScript
Skip