Setting Up Playwright with Java – Step-by-Step Guide

Learn how to install and configure Playwright in a Java project using Maven.

This entry is part 2 of 4 in the series Playwright With Java

Before writing Playwright tests in Java, let’s set up our environment.

1️⃣ Prerequisites

  • Install Java 11+Download JDK
  • Install Maven (or Gradle)
  • Install an IDE → IntelliJ IDEA (recommended) or Eclipse

2️⃣ Create a New Maven Project

Run:

mvn archetype:generate -DgroupId=com.playwright.demo -DartifactId=playwright-java-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

3️⃣ Add Playwright Dependency

In pom.xml:

<dependencies>
    <dependency>
        <groupId>com.microsoft.playwright</groupId>
        <artifactId>playwright</artifactId>
        <version>1.46.0</version>
    </dependency>
</dependencies>

4️⃣ Install Playwright Browsers

After adding the dependency, run:

mvn exec:java -e -Dexec.mainClass=com.microsoft.playwright.CLI -Dexec.args="install"

This downloads Chromium, Firefox, and WebKit for testing.

5️⃣ Verify Installation

Create a simple class:

import com.microsoft.playwright.*;

public class LaunchBrowser {
    public static void main(String[] args) {
        try (Playwright playwright = Playwright.create()) {
            Browser browser = playwright.chromium().launch();
            Page page = browser.newPage();
            page.navigate("https://example.com");
            System.out.println(page.title());
            browser.close();
        }
    }
}

Run it → you should see the browser launch and print the page title.

👉 Setup complete! Next, we’ll dive into Playwright’s architecture and features.

Series Navigation<< Introduction to Playwright with Java – A Modern Automation ToolPlaywright Architecture & Features Every Tester Must Know >>

Leave a Reply

Your email address will not be published. Required fields are marked *