Playwright + Python · Lesson 33 of 37

0/37 done

Module 33: Visual Testing

33. Pixel-Perfect Visual Comparisons & Baseline Management

15 minlesson

Your task

Implement save_component_screenshot(locator, save_path):

  1. locator.screenshot(path=save_path)

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

Visual Comparisons in Python

python
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-snapshots

Key Takeaways & Walkthrough Summary

Functional 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.

Common Doubts & Lesson Q&A

How do I handle dynamic timestamps during visual comparisons?

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.

Python
Skip