Playwright + Java · Lesson 19 of 38

0/38 done

Module 19: Assertions

19. Web-First Assertions with PlaywrightAssertions.assertThat

16 minlesson

Your task

Implement verifyDashboardPage:

  1. Assert assertThat(page).hasURL(expectedUrl).
  2. Assert assertThat(page.getByRole(AriaRole.HEADING)).isVisible().

Write it in the editor, then press Check answer (Ctrl + Enter).

Web-First Auto-Retrying Assertions

java
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;

assertThat(page).hasURL("https://targetqa.com/dashboard");
assertThat(page).hasTitle("Dashboard - TargetQA");

Locator welcomeHeader = page.getByRole(AriaRole.HEADING);
assertThat(welcomeHeader).isVisible();
assertThat(welcomeHeader).hasText("Welcome back, Dhanu!");

Key Takeaways & Walkthrough Summary

Web-first assertions are Playwright's most powerful weapon against test flakiness. Unlike JUnit assertEquals which checks values only once, PlaywrightAssertions automatically re-poll the element until the expected condition is met or the timeout expires. Let's master web-first assertions!

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

Common Doubts & Lesson Q&A

Why should I avoid assertTrue(locator.isVisible()) in Java?

locator.isVisible() checks the DOM only at that exact millisecond without auto-retrying. assertThat(locator).isVisible() continuously polls the DOM until visible.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Java
Skip