Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Playwright Java when you need a dependable execution video: launch the browser headed, create a fresh browser context with setRecordVideoDir(...), run the test, and close the context. Playwright finalizes the video at context close. For a deliberately started and stopped recording, Playwright Java 1.59 adds Page.screencast(), including optional action overlays. Selenium remains practical when your team already has WebDriver infrastructure, but the Selenium getting-started documentation does not establish a native video API.

What you are recording: authoring versus execution

There are two different recordings in a Java browser project:

  • Test-authoring recording: Playwright Codegen watches clicks, typing and assertions, then produces starter Java code. It is for creating a test, not for producing a video of that test running.
  • Execution screencast: Playwright’s browser-context video or page screencast captures the automated run. This is what you attach to a CI failure, use in a tutorial, or show in a code review.

Keeping those purposes separate prevents a common mistake: Codegen output is source code, not an execution movie.

Set up a headed Playwright Java test

Playwright browsers run headless by default. A visible demonstration therefore needs setHeadless(false). setSlowMo adds a delay between browser operations so a viewer can follow the interaction.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import com.microsoft.playwright.*;
import org.junit.jupiter.api.*;

import java.nio.file.Paths;

class CheckoutVideoTest {
  static Playwright playwright;
  static Browser browser;

  @BeforeAll
  static void startBrowser() {
    playwright = Playwright.create();
    browser = playwright.chromium().launch(
        new BrowserType.LaunchOptions()
            .setHeadless(false)
            .setSlowMo(150));
  }

  @AfterAll
  static void stopBrowser() {
    browser.close();
    playwright.close();
  }

  @Test
  void checkout() {
    BrowserContext context = browser.newContext(
        new Browser.NewContextOptions()
            .setViewportSize(640, 480)
            .setRecordVideoDir(Paths.get("videos")));
    try {
      Page page = context.newPage();
      page.navigate("https://example.com/checkout-demo");
      page.getByLabel("Email").fill("[email protected]");
      page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions()
          .setName("Continue")).click();
      Assertions.assertTrue(page.getByText("Order summary").isVisible());
    } finally {
      // The context must close to finalize the video file.
      context.close();
    }
  }
}

The imports and selectors are ordinary Playwright Java. Replace the demonstration URL and locators with your application’s stable test data. The fixed 640×480 viewport keeps the frame consistent between runs; choose a larger size when the page contains dense tables or code.

Record every test with BrowserContext video

Video recording is configured on Browser.NewContextOptions, not on an individual page. Every page opened in that context is included. Playwright’s documentation states that videos are saved when the browser context closes, so a path is not reliably available while the test is still running.

  1. Create a new context for each test.
  2. Set setRecordVideoDir(Paths.get("videos/")).
  3. Run the test actions and assertions.
  4. Close the context in a finally block.
  5. Only after closure, obtain the page’s video path if you need to rename, upload or report it.
Path videoPath;
BrowserContext context = browser.newContext(
    new Browser.NewContextOptions()
        .setRecordVideoDir(Paths.get("videos"))
        .setViewportSize(640, 480));
Page page = context.newPage();
try {
  page.navigate("https://example.com");
  // test steps
} finally {
  context.close();
}
videoPath = page.video().path();
System.out.println("Saved video: " + videoPath);

Do not share this context between tests. A fresh context isolates cookies, local storage and permissions while allowing the browser process to be reused. In parallel JUnit execution, give each test its own context and output naming strategy so two tests cannot overwrite or mislabel artifacts.

Use the explicit Page.screencast API for demonstrations

Playwright Java 1.59 introduced a dedicated screencast API. It is useful when recording should begin after setup, end before teardown, or include action annotations.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Page page = context.newPage();
page.navigate("https://example.com/login");

Page.Screencast screencast = page.screencast();
screencast.start(new Page.Screencast.StartOptions()
    .setPath(Paths.get("videos/login-demo.webm")));
screencast.showActions();

page.getByLabel("User name").fill("demo-user");
page.getByLabel("Password").fill("demo-password");
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions()
    .setName("Sign in")).click();

screencast.stop();
context.close();

showActions() overlays action titles and highlights interactions, which makes a tutorial recording easier to understand. Use context video for a simple “record every test” policy; use screencast().start() and stop() when you need precise boundaries or annotations. Keep the API version aligned with Playwright Java 1.59 or later when using this method.

Make the recording readable and repeatable

  • Choose a stable page: use a local fixture or a demo endpoint that will not change during a presentation.
  • Fix the viewport: a known width and height prevent responsive breakpoints from changing the movie.
  • Use short, deterministic data: avoid random names, current-time assertions and network-dependent delays.
  • Wait for meaningful states: assert a heading or result rather than inserting long arbitrary sleeps.
  • Show only useful browser motion: headed mode is required for a visible run; a modest setSlowMo is usually clearer than making every step artificially long.
  • Keep contexts independent: JUnit lifecycle methods can create and close shared Playwright/browser objects, while each test owns its context.

If your objective is to explain the test itself, arrange the screen so the browser and IntelliJ IDEA’s Run tool window are visible in sequence. IntelliJ IDEA recognizes Selenium and Playwright tests; its run/debug panels can show source, logs, failures and execution time. JetBrains documents this recognition from IntelliJ IDEA 2023.3 onward.

Generate Java tests from browser actions with Codegen

Start Codegen when you need starter code rather than an execution video. It opens a browser and Playwright Inspector. Perform clicks, fills and assertions in the browser, then copy the generated Java into your test class and replace fragile locators or data.

mvn exec:java 
  -e -Dexec.mainClass=com.microsoft.playwright.CLI 
  -Dexec.args="codegen https://example.com"

The exact Maven or Gradle wiring depends on your project, but the result is the same: Codegen records interactions and assertions into Java code. Run that code separately with JUnit and record its execution using a BrowserContext video or page screencast.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Selenium: a practical alternative for existing WebDriver teams

Selenium supports automation of major browsers through WebDriver, and Selenium IDE is a record-and-playback option for authoring interactions. The cited Selenium getting-started material does not document a native Java video recorder comparable to Playwright’s BrowserContext video or Page.screencast().

If your project already uses Selenium, keep the WebDriver test and capture the visible run with an external desktop or CI recorder, or migrate the recording-focused scenario to Playwright. Selenium documentation points teams toward test-runner libraries and Grid when they need structured execution and scale. IntelliJ IDEA supports Selenium project creation and JUnit or TestNG execution, so your existing test workflow can remain familiar even when video capture is handled elsewhere.

Common failures and fixes

The browser is not visible

Cause: Playwright is headless by default. Fix: launch with new BrowserType.LaunchOptions().setHeadless(false). On a headless CI machine, provide a display server or use a CI capture solution instead of expecting a desktop window.

No video file appears

Cause: the context is still open, or the test exited before cleanup. Fix: close the context in finally; retrieve page.video().path() only after closure.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Videos are unreadable or inconsistent

Cause: changing viewport sizes, random data, animations or a moving target page. Fix: set a fixed viewport, use stable fixtures, reduce unnecessary animation and add assertions for the state you intend to show.

Parallel tests overwrite artifacts

Cause: shared contexts or identical output names. Fix: create one context per test and include the JUnit test identifier in any copied or uploaded filename.

Codegen output is brittle

Cause: generated locators reflect the page as it looked during authoring. Fix: prefer accessible roles, labels and explicit test IDs; remove incidental clicks and replace demo data with deterministic fixtures.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

ScreenshotNeo is a website screenshot API and MCP server for developers. One GET request returns a PNG, JPEG, WebP or PDF, so you can capture a test URL without maintaining a headed browser process:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for all options. It accepts cookie and consent banners before capture and removes more than 60 known consent platforms, newsletter popups and chat widgets; each step can be disabled. Bot checks, CAPTCHAs, blank pages, timeouts, failed loads and cache hits cost nothing, and response headers identify the page verdict and whether it was billed. An MCP server exposes take_screenshot, get_page_info and capture_pdf to Claude, Cursor and other MCP clients.

The Free plan includes 1,000 shots per month with no card. Paid plans start at $5 for 3,000 shots; every feature is available on every plan. Create a free ScreenshotNeo account to try it.

Cost, reliability and artifact handling

  • Playwright video consumes local disk and adds encoding work; retain only the videos needed for failed tests or demonstrations.
  • Close contexts even on assertion failures so recordings finalize and resources are released.
  • For CI, upload the finalized file as a test artifact and record the test name, commit and browser version beside it.
  • Use a separate output directory per build when multiple jobs run on the same worker.
  • Do not put passwords, tokens or personal data in a recording; use dedicated test accounts and masked fixtures.

Frequently Asked Questions

Can I record a Playwright Java test without showing a browser window?

Yes. BrowserContext video works in headless mode; headed mode is only required when the recording must visibly show the browser window during execution.

When is a Playwright video file complete?

Context-based recordings are finalized when the BrowserContext closes. Retrieve or upload the video after that close operation.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Does Selenium WebDriver include a built-in Java video API?

The cited Selenium getting-started documentation does not establish one. Selenium IDE records interactions for playback, while execution video generally requires another capture approach.

Which approach should I use for a tutorial?

Use a fixed viewport, headed launch, modest slow motion and Page.screencast() with showActions() when you need annotations; use BrowserContext recording for routine test artifacts.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.