Playwright + Python · Lesson 33 of 37
Module 33: Visual Testing
Your task
Implement save_component_screenshot(locator, save_path):
Write it in the editor, then press Check answer (Ctrl + Enter).
from playwright.sync_api import expect
# Full page screenshot comparison:
expect(page).to_have_screenshot("homepage_baseline.png")
# Element-specific screenshot comparison:
card = page.locator("#pricing-card")
expect(card).to_have_screenshot("pricing_card_baseline.png")
# Update baselines via CLI:
# pytest --update-snapshotsFunctional tests can pass even when CSS breaks and buttons overlap. Visual regression testing compares current page screenshots against approved golden baselines. In this lesson, you will master visual screenshot assertions and pixel tolerances in Python. Let's explore visual testing!
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.
Mask dynamic elements: page.locator('.timestamp').evaluate('el => el.style.visibility = "hidden"') before comparing.
Ready? Solve the task in the editor
Use Check answer to mark it complete. Hints are there if you need them.
___