In this tutorial, we will learn to create and set up the project using Playwright Java.
Table of Contents
System requirements
The following prerequisites are required to be installed on the machine to begin with a smooth setup and installation.
- Java 8 or higher
- IntelliJ IDE or any other IDE to create a project
- Maven
- Browsers on which tests need to be run, like Chrome, Firefox, etc.
Implementation Steps
1. Create a new Maven Project
The first step in setup is to create a new Maven project. I will be using IntelliJ in this tutorial. The following steps need to be followed to create a new Maven project :
Open IntelliJ, Navigate to File >> New >> Project

2. In the New Project window, enter the following details:
- Name of the Project – Playwright_Java_Demo
- Location/path where the project needs to be saved – Documents/Playwright (my location)
- Select JDK version – I am using JDK 17
- Archetype – Search for “quickstart” and select maven-archetype-quickstart from the results
Click on the Create button to create the project.

This will create a project as shown below in the IntelliJ.

2. Updating the Dependencies
Add the Playwright dependency to the pom.xml. The latest dependency can be found from here.
<!-- https://mvnrepository.com/artifact/com.microsoft.playwright/playwright -->
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.57.0</version>
</dependency>
After adding the dependency, refresh the project. We will see that the Playwright jar files are downloaded in External Libraries.

3. Test Creation
Create a Java class as Test file under src/test/java as shown below – PlaywrightDemo

We will be automating the following test scenario using Playwright Java.
- Open the https://qaautomation.expert/ website
- Print the heading of the page
- Verify that the Home page title is equal to “QA Automation Expert”
Below is the sample code for the above scenario. The test is using Chromium browser.
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import java.util.regex.Pattern;
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
public class PlaywrightDemo {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch();
Page page = browser.newPage();
page.navigate("https://qaautomation.expert/");
System.out.println("Title :" + page.title());
assertThat(page).hasTitle(Pattern.compile("QA Automation Expert"));
}
}
}
Explanation
1. Initialize Playwright
Below method is used to initialize a new Playwright instance. It is placed inside a try-with-resources block. This ensures that resources are closed automatically after use.
try (Playwright playwright = Playwright.create())
2. Launch Web Browser
A new Chromium browser instance is launched.
Browser browser = playwright.chromium().launch();
3. Create a new page
Creates a new page/tab in the browser.
Page page = browser.newPage();
4. Open the web application
This statement navigates the newly created page to a specified URL.
page.navigate("https://qaautomation.expert/");
5. Print the title
The title of the page is retrieved and printed to the console using`page.title()`.
System.out.println("Title :" + page.title());
6. Assert the page title
Below line asserts that the page has the title matching the regular expression “QA Automation Expert”. The use of `Pattern.compile()` allows for matching with a regular expression, which can be beneficial for flexible title checking.
assertThat(page).hasTitle(Pattern.compile("QA Automation Expert"));
4. Test Execution
Click on the green arrow next to the Java program name. A dialog box will appear with the various options. These options can be seen in the image.

By default, the tests are run in headless mode, so the browser UI is not loaded.
Below is the test execution result.

When the tests are executed for the first time, Playwright downloads Chromium, Firefox, and WebKit browsers into OS-specific Cache folders.
%USERPROFILE%\AppData\Local\ms-playwrighton Windows~/Library/Caches/ms-playwrighton MacOS~/.cache/ms-playwrighton Linux
As I am running the test on a Windows OS machine, the browser binaries are downloaded into the folder.%USERPROFILE%\AppData\Local\ms-playwright

More details about Playwright with Java can be found in the official website of Playwright – https://playwright.dev/java/docs/intro.