Appearance
Lesson 02 · Playwright for Java
Beyond the 1Z0-830 exam
Playwright (from Microsoft) is the modern challenger to Selenium: one API drives Chromium, Firefox, and WebKit, with auto-waiting built into every action. It removes most of the manual wait code that makes Selenium suites flaky.
Objectives
After this lesson you will be able to:
- Launch a browser and drive a
Pagewith Playwright for Java. - Use locators and rely on auto-waiting.
- Capture traces/screenshots/video for debugging.
- Decide between Playwright and Selenium.
The Playwright model
java
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch(); // or .firefox()/.webkit()
Page page = browser.newPage();
page.navigate("https://example.com/login");
page.locator("#username").fill("ada");
page.locator("#password").fill("secret");
page.locator("button[type=submit]").click();
assertThat(page).hasURL(Pattern.compile(".*/dashboard")); // Playwright assertions auto-retry
browser.close();
}Playwright is AutoCloseable (try-with-resources, Module 04). A BrowserContext gives each test an isolated, cookie-free session — cheap to create, unlike a fresh browser.
Auto-waiting — the key difference
Every Playwright action (click, fill, textContent) automatically waits for the element to be present, visible, stable, and enabled before acting, up to a timeout. You rarely write an explicit wait:
java
page.locator("#submit").click(); // waits for #submit to be actionable, then clicksContrast Selenium, where you typically wrap the same action in a WebDriverWait. Playwright's web-first assertions (assertThat(locator).isVisible()) also auto-retry until they pass or time out — no manual polling.
Locators are lazy
A Playwright Locator is a description of how to find an element, resolved fresh on each action — so it survives DOM re-renders. Selenium's WebElement is a handle to one resolved element that can go stale (StaleElementReferenceException) when the page updates. Locator-based code is more robust by design.
Tracing and debugging
Playwright's killer feature for CI is the trace — a recording of every action, DOM snapshot, console log, and network call, viewable after the fact:
java
context.tracing().start(new Tracing.StartOptions()
.setScreenshots(true).setSnapshots(true));
// … run the test …
context.tracing().stop(new Tracing.StopOptions().setPath(Paths.get("trace.zip")));It also captures screenshots (page.screenshot(...)) and video per context — invaluable for diagnosing a headless CI failure.
Playwright vs Selenium
| Selenium | Playwright | |
|---|---|---|
| Waiting | mostly manual (explicit waits) | auto-waiting built in |
| Element model | WebElement (can go stale) | lazy Locator (re-resolved) |
| Browsers | Chrome/Firefox/Edge/Safari via drivers | Chromium/Firefox/WebKit bundled |
| Debugging | screenshots, logs | trace viewer, video, screenshots |
| Maturity/ecosystem | huge, long-established | newer, growing fast |
| Cross-language standard | W3C WebDriver | Playwright protocol |
Neither is "wrong." Selenium wins on ecosystem maturity and being the W3C standard; Playwright wins on out-of-the-box reliability and debugging. New projects increasingly pick Playwright.
SDET note
This module's lab is framework-agnostic on purpose: the LoginPage page object talks to a FakeBrowser interface, so the same page object could sit on top of Selenium's WebDriver or Playwright's Page. Good UI test architecture isolates the tool choice behind page objects (next lesson).
Key Takeaways
- Playwright drives Chromium/Firefox/WebKit from one API;
PlaywrightisAutoCloseable. - Auto-waiting waits for actionability on every action — far fewer manual waits than Selenium.
- A
Locatoris lazy and re-resolved, avoiding Selenium's stale-element problem. - Traces, video, and screenshots make headless/CI failures debuggable.
- Choose per project: Selenium for ecosystem/standard, Playwright for built-in reliability.
Lesson Quiz
Playwright's auto-waiting means…
Why is a Playwright Locator more robust than a Selenium WebElement?
What is a Playwright trace useful for?
A BrowserContext is used to…
A fair summary of Selenium vs Playwright is…
Next: Page Object Model & Design. This module's lab is in labs/src/main/java/com/jse21/m18_ui/.