Playwright + JS · Lesson 19 of 35
Module 19: Assertions
Try it yourself
Implement formatAssertionResult:
Assertion Failed: [${field}] Expected '${expected}', but received '${actual}'..The editor has a worked example. Read it, tweak it, then press Mark as complete.
// 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");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.
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.