Playwright + Java · Lesson 34 of 38
Module 34: Visual Testing
Your task
Implement captureComponentScreenshot:
Write it in the editor, then press Check answer (Ctrl + Enter).
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
// Full-page visual comparison:
assertThat(page).hasScreenshot("homepage-baseline.png");
// Element-specific visual comparison (e.g. checkout card):
Locator paymentCard = page.locator("#payment-card");
assertThat(paymentCard).hasScreenshot("payment-card-baseline.png");Mask dynamic timestamps and random avatar images before comparing:
page.locator(".timestamp-badge").evaluate("node => node.style.visibility = 'hidden'");Functional tests can pass even if 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. Let's explore visual testing!
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.
Font rendering and subpixel antialiasing differ between macOS, Windows, and Linux. Run visual tests inside identical containerized environments or set appropriate pixel thresholds.
Ready? Solve the task in the editor
Use Check answer to mark it complete. Hints are there if you need them.
___