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

A Java Playwright project needs three pieces: the Playwright Java dependency, browser binaries that match that Playwright version, and Java code that launches a browser and interacts with a page. Start with the small Maven example below, then choose whether to keep it as an executable sample or move the browser checks into a test runner. Maven and Gradle are both documented options; use the one already used by your repository.

Start with a minimal Maven project

The official Playwright Java introduction uses a Maven project with a pom.xml and an App.java entry point. This version demonstrates navigation and a basic assertion. The dependency version shown in the Java introduction at the time represented by the available documentation is 1.63.0; check the current Java introduction for the release to use when creating a new project.

Project files

playwright-java-sample/
├── pom.xml
└── src/
    └── main/
        └── java/
            └── org/
                └── example/
                    └── App.java

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>playwright-java-sample</artifactId>
  <version>1.0-SNAPSHOT</version>
  <properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>com.microsoft.playwright</groupId>
      <artifactId>playwright</artifactId>
      <version>1.63.0</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.5.0</version>
      </plugin>
    </plugins>
  </build>
</project>

The compiler properties demonstrate a Java 8 source/target baseline, consistent with the Java introduction’s stated Java 8-or-higher requirement. Confirm the currently supported Java and operating-system combinations in that live documentation before choosing a production runtime.

App.java

package org.example;

import com.microsoft.playwright.*;

public class App {
  public static void main(String[] args) {
    try (Playwright playwright = Playwright.create()) {
      Browser browser = playwright.chromium().launch();
      Page page = browser.newPage();
      page.navigate("https://playwright.dev/");
      System.out.println("Title: " + page.title());
      if (!page.title().contains("Playwright")) {
        throw new AssertionError("Unexpected page title: " + page.title());
      }
      browser.close();
    }
  }
}

With Maven installed and the project files in place, install the browser binaries for the Playwright version in the dependency, then run:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
mvn compile exec:java -D exec.mainClass="org.example.App"

The command and starter structure follow the official Java introduction. Playwright Java supports Chromium, Firefox, and WebKit through its API; a project should use the browser installation command documented for the version it depends on.

Install the matching browser binaries

Adding the Java library dependency does not by itself guarantee that the browser executable is installed. Playwright versions are paired with specific browser binaries. If you update the dependency, install browsers again as needed rather than assuming an older browser download remains compatible.

  1. Use the Playwright CLI command documented for Java in the browser guide to install the required browser or browsers.
  2. For a single-browser sample, install only the browser you intend to run; for a cross-browser suite, install the browser set the tests cover.
  3. On a CI machine, install the operating-system dependencies as well as the browser binaries, following the guide’s instructions for that environment.
  4. When changing the Playwright library version, refresh the browser installation and any version-keyed CI cache.

Playwright-managed Chromium is not automatically the same thing as branded Google Chrome or Microsoft Edge. The browser guide distinguishes the bundled browser from branded channels; use a branded channel only when that distinction matters to your test.

Choose Maven or Gradle for a test project

The runnable starter above is deliberately a Maven executable sample. For a larger test suite, integrate Playwright with the test runner already used by the repository and keep its dependency declarations and commands in one build system. Playwright’s Java test-runner documentation includes Maven and Gradle setup examples.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Choice Fits best when Project shape
Maven The project already uses Maven, or you want to follow the Java introduction’s Maven starter. Declare Playwright in pom.xml; run the sample through Maven or configure the documented Java test runner.
Gradle The existing repository uses Gradle and its test task is the natural place for browser checks. Declare Playwright and the test runner in Gradle configuration using the official test-runner examples.

Do not paste Gradle dependency syntax into a Maven project or assume that the standalone App.java command is the test command for a runner-based suite. Follow the matching Maven or Gradle example in the official runner page for exact plugin and test-task configuration.

Turn navigation into a useful browser test

A test should wait for meaningful page state rather than sleeping for an arbitrary number of milliseconds. Playwright locators wait for elements to become actionable, and its web-first assertions retry while the expected condition is not yet true. That makes a locator-based assertion more robust on dynamic pages than checking immediately or adding a fixed delay.

For example, a runner-based test can express the intended interaction in this form; use the imports and test annotations of the runner configured for the project:

Page page = browser.newPage();
page.navigate("https://playwright.dev/");

Locator docsLink = page.getByRole(AriaRole.LINK,
    new Page.GetByRoleOptions().setName("Get started"));
docsLink.click();

assertThat(page).hasURL(Pattern.compile(".*intro"));
assertThat(page.getByRole(AriaRole.HEADING,
    new Page.GetByRoleOptions().setName("Installation"))).isVisible();

This illustrates locator-based interaction and retrying assertions; the exact test annotation and assertion imports depend on the runner selected. See Writing tests for Java-specific patterns. Prefer locators that describe the user’s view—such as role and accessible name—where they identify the control reliably.

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

Keep browser and page lifetimes clear

  • Create Playwright and browser objects at an appropriate test or fixture scope, then close them reliably.
  • Use a fresh page or browser context when test isolation matters; do not let cookies or prior page state silently couple tests.
  • Make assertions about observable outcomes, such as a URL, heading, or visible result, rather than implementation details that can change without affecting behavior.
  • Use a targeted wait for a selector or assertion condition when the page has asynchronous work. Avoid fixed sleeps unless a specific timing experiment truly requires them.

Run the Java project in CI

CI requires more than compiling Java: the agent must be able to run browsers, the matching Playwright browser binaries must be available, and Linux environments may need browser system dependencies. The official CI guide describes the general sequence and provider-specific examples.

  1. Provision a CI agent and Java version supported by the current Playwright Java documentation.
  2. Check out the project and resolve its Maven or Gradle dependencies.
  3. Install Playwright browsers and required operating-system dependencies using the documented command for that environment.
  4. Run the project’s Maven or Gradle test command.
  5. If caching browser binaries, key the cache to the Playwright version so an upgrade does not restore incompatible binaries.

Start with uncached browser installation if diagnosing a CI failure; this separates cache problems from missing dependencies or an incompatible runtime. Once the build is stable, version-keyed caching can avoid repeated downloads. Exact CI YAML varies by provider, operating system, and chosen build tool, so use the corresponding example in the CI guide rather than copying a workflow for a different environment.

Common failures and fixes

Symptom Likely cause What to do
Playwright reports that an executable is missing. The required browser binaries have not been installed for the library version, or a stale cache was restored. Run the browser installation command from the Java browser guide; invalidate or version the cache by Playwright release.
Browser launches locally but not in CI. The CI agent lacks browser capability or operating-system dependencies. Follow the CI guide’s browser and dependency installation sequence for the agent’s OS.
A test fails intermittently after navigation. The code checks too early or waits for a fixed duration that does not match page behavior. Wait on a meaningful locator or use a retrying web-first assertion instead of an arbitrary sleep.
The browser is not the branded Chrome or Edge expected. The test launched Playwright’s managed Chromium build. Consult the browser guide’s channel guidance and explicitly select a branded channel if required.
The sample cannot resolve a dependency or runs with an unexpected Playwright version. The build file, local dependency cache, or test-runner configuration differs from the example. Check the version in the active Maven or Gradle configuration, refresh dependencies, and keep the chosen build tool consistent.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Performance, reliability, and cost considerations

Playwright itself is a library rather than a per-screenshot hosted service in this project pattern. The practical costs are the CI machine time, browser downloads, and the maintenance of the Java/build environment. Install only browsers the suite needs, reuse a version-correct cache in CI, and keep tests focused on user-visible behavior to reduce avoidable work and fragile retries. No independent runtime benchmark is established here; actual execution time depends on the tested site, browser, machine, network, and suite.

For reliable results, pin the dependency deliberately, install the browser binaries that correspond to it, and update both together. Check current Java, operating-system, architecture, and browser guidance in the live Playwright documentation because those compatibility details can change between releases.

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

Or skip the browser setup

If the goal is to obtain a website screenshot rather than build browser automation into a Java test suite, ScreenshotNeo provides a screenshot API and MCP server. One GET request returns an image or PDF. Its cURL example saves a WebP screenshot; see the API documentation for request options.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

ScreenshotNeo accepts cookie/consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be disabled. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers report the page verdict and billing status. Its MCP server offers take_screenshot, get_page_info, and capture_pdf for AI agents and MCP clients. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000. Every feature is available on every plan.

Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.

Official references

Frequently Asked Questions

Can the same Playwright Java project run on Windows, macOS, and Linux?

The Java introduction lists all three operating systems; verify its current supported releases and architectures before selecting a deployment environment.

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.

Does the sample require a fixed delay before every assertion?

No. Use locator actionability and retrying web-first assertions for page conditions instead of adding a blanket sleep.

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.