Playwright + Python · Lesson 18 of 37

0/37 done

Module 18: Assertions

18. Web-First Auto-Retrying Assertions with expect()

16 minlesson

Your task

Implement verify_dashboard_elements(page, expected_url):

  1. expect(page).to_have_url(expected_url)
  2. expect(page.get_by_role("heading")).to_be_visible()

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

Web-First Auto-Retrying Assertions

python
from playwright.sync_api import expect

expect(page).to_have_url("https://targetqa.com/dashboard")
expect(page).to_have_title("Dashboard - TargetQA")

welcome_msg = page.get_by_role("heading")
expect(welcome_msg).to_be_visible()
expect(welcome_msg).to_have_text("Welcome back!")

Key Takeaways & Walkthrough Summary

Web-first assertions are Playwright's most effective weapon against flakiness. The expect() function re-polls the DOM until the condition passes or timeout occurs. In this lesson, you will master to_be_visible, to_have_text, and to_have_url. Let's master assertions!

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

Common Doubts & Lesson Q&A

Why avoid standard Python assert locator.is_visible()?

locator.is_visible() evaluates immediately once without retrying. expect(locator).to_be_visible() continuously polls until true.

Ready? Solve the task in the editor

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

Python
Skip