Assertions and Waits

URL and Title Assertions

URL and Title Assertions: definition, detailed explanation, practical usage, examples, mistakes, interview notes, and practice for Playwright automation.

Definition and Brief Explanation

Definition: URL and Title Assertions is used to wait for and verify a specific browser, page, network, or UI condition.

Explanation: URL and Title Assertions makes tests reliable because it checks the state that matters instead of waiting for a guessed amount of time. In Playwright, expect assertions retry for a short period, which helps with dynamic pages.

Why It Matters

  • It proves the application reached the expected state.
  • It replaces fixed sleeps with meaningful waits.
  • It gives clearer failure messages than manual checks.
  • It helps separate real product bugs from timing issues.

How It Works

  1. Trigger the behavior that should change state.
  2. Choose the exact condition that proves success.
  3. Use retrying expect assertions where possible.
  4. Keep custom timeout changes local and justified.

Syntax and Examples

Example 1: URL assertion

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

Explanation: Confirms navigation reached dashboard.

Example 2: Title assertion

await expect(page).toHaveTitle(/WebNotes/);

Explanation: Checks browser tab title.

Common Mistakes

  • Waiting for a generic condition when a specific assertion is available.
  • Increasing global timeout instead of fixing the wait target.
  • Asserting text or count too broadly.
  • Using non-retrying checks on dynamic UI.

Interview Notes

  1. What does URL and Title Assertions verify?
  2. Why is a retrying assertion useful?
  3. How do you debug a timeout?
  4. What is a better alternative to waitForTimeout?

Practice Task

Create a small Playwright example for URL and Title Assertions. Add one positive assertion, one note about what can go wrong, and one improvement that would make the test more maintainable.