In this tutorial blog, we will learn to create and set up the project using Playwright Java with JUnit5. JUnit 5 controls the test lifecycle. Playwright controls the browser automation.
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.
Basic Lifecycle Mapping
| JUnit 5 Annotation | Purpose | Playwright Action |
| @BeforeAll | Run once before all tests | Create Playwright + launch Browser |
| @BeforeEach | Run before every test | Create Context + Page |
| @AfterEach | Run after every test | Close Page + Context |
| @AfterAll | Run once after all tests | Close Browser + Playwright |
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_JUnit5_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. Setup Playwright with Java and JUnit5
Add the Playwright and JUnit5 dependencies to the pom.xml. The latest dependency can be found here.
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>Playwright_Junit5_Demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Playwright_Junit5_Demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<playwright.version>1.57.0</playwright.version>
<junit.jupiter.engine.version>5.11.0-M2</junit.jupiter.engine.version>
<junit.jupiter.api.version>5.11.0-M2</junit.jupiter.api.version>
<junit.jupiter.params.version>5.11.0-M2</junit.jupiter.params.version>
<maven.compiler.plugin.version>3.13.0</maven.compiler.plugin.version>
<maven.compiler.source.version>17</maven.compiler.source.version>
<maven.compiler.target.version>17</maven.compiler.target.version>
<maven.surefire.plugin.version>3.2.5</maven.surefire.plugin.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.site.plugin.version>4.0.0-M16</maven.site.plugin.version>
<maven.surefire.report.plugin.version>3.5.4</maven.surefire.report.plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>${playwright.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.engine.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.api.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.jupiter.params.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${maven.compiler.source.version}</source>
<target>${maven.compiler.target.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>${maven.site.plugin.version}</version>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>${maven.surefire.report.plugin.version}</version>
<configuration>
<outputName>JUnit5 Report</outputName>
</configuration>
</plugin>
</plugins>
</reporting>
</project>
The reporting plugin is added to generate the JUnit5 reports.
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>${maven.surefire.report.plugin.version}</version>
<configuration>
<outputName>JUnit5 Report</outputName>
</configuration>
</plugin>
</plugins>
</reporting>
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 Test file under src/test/java.

We will be automating the following test scenario using Playwright Java.
- Verify that the Home page title of “https://opensource-demo.orangehrmlive.com/web/index.php/auth/login” is equal to “OrangeHRM”
- Verify the successful login leads to Dashboard page
- Verify the invalid username generates error message “Invalid credentials”
Below is the sample code for the above scenario. The test is using Chromium browser.
Let’s create a base test class that will be used by all our tests. It will set up Playwright and create a browser instance. The base test class will also create a new browser context and a new page for each test method.
package com.example;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import org.junit.jupiter.api.*;
// Subclasses will inherit PER_CLASS behavior.
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class BaseClass {
// Shared between all tests in the class.
Playwright playwright;
Browser browser;
@BeforeAll
void launchBrowser() {
playwright = Playwright.create();
browser = playwright.chromium().launch();
}
@AfterAll
void closeBrowser() {
playwright.close();
}
// New instance for each test method.
BrowserContext context;
Page page;
@BeforeEach
void createContextAndPage() {
context = browser.newContext();
page = context.newPage();
page.navigate("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
}
@AfterEach
void closeContext() {
context.close();
}
}
Explanation
1. Use @TestInstance(TestInstance.Lifecycle.PER_CLASS) annotation to make JUnit create one instance of a class for all test methods within that class (by default each JUnit will create a new instance of the class for each test method).
2. Browser browser
Declared to store the browser instance that will be shared across test methods within the class.
Playwright playwright;
Browser browser;
3. BrowserContext context
Created anew for each test method to simulate independent browser sessions. This ensures that the browser state doesn’t carry over between tests.
4. Page page
Represents a single tab or window in a browser used for performing actions and assertions.
BrowserContext context;
Page page;
5. @BeforeAll
This method is annotated with @BeforeAll, indicating run this method once before ALL tests in this class.
The browser is launched in Chromium and non-headless mode (setHeadless(false)), meaning an actual browser window is opened.
6. @BeforeEach createContextAndPage()
This method runs before each test method in the class, as indicated by the @BeforeEach annotation.
It creates a new BrowserContext and a Page, ensuring each test runs in a clean, isolated state.
The method also navigates to a login page (“https://opensource-demo.orangehrmlive.com/web/index.php/auth/login”), setting up the initial state for each test.
@BeforeEach
void createContextAndPage() {
context = browser.newContext();
page = context.newPage();
page.navigate("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
}
7. @AfterMethod closeContext()
Runs after each test method, annotated with @AfterMethod.
Closes the BrowserContext, effectively closing the browser tab and cleaning up resources.
@AfterEach
void closeContext() {
context.close();
}
8. @AfterAll closeBrowser()
Run this method once after ALL tests in the test class have finished. Close the Playwright, freeing the native resources.
@AfterAll
void closeBrowser() {
playwright.close();
}
Now, we can create our first test. Let’s create a test class that extends the BaseClass class.
package com.example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class PlaywrightTests extends BaseClass{
@Test
void verifyPageTitle() {
System.out.println("Page Title :" + page.title());
assertEquals("OrangeHRM", page.title());
}
@Test
void verifyLogin() {
page.locator("input[name='username']").fill("Admin");
page.locator("input[name='password']").fill("admin123");
page.locator("button[type='submit']").click();
String expectedValue = page.locator("//h6[contains(@class, \"oxd-topbar-header-breadcrumb-module\")]").textContent();
assertEquals(expectedValue,"Dashboard");
}
@Test
void verifyInvalidCredentials() {
page.locator("input[name='username']").fill("Playwright");
page.locator("input[name='password']").fill("admin123");
page.locator("button[type='submit']").click();
String expectedValue = page.locator("//p[contains(@class, \"oxd-text oxd-text--p oxd-alert-content-text\")]").textContent();
assertEquals(expectedValue,"Invalid credentials");
}
}
Explanation
1. verifyPageTitle method
This method is used to verify the heading of the application.
2. verifyLogin method
This method verifies that the application is successfully logged in using the correct credentials.
3. verifyInvalidCredentials method
This method asserts that an error message is generated when invalid username or password is entered in the login page.
4. Test Execution through IntelliJ
Go to the Test class and right-click and select Run ‘Playwright_Tests’ (in IntelliJ).

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

5. Run the tests from command line
Execute the tests by using the below command:-
mvn clean test site

6. JUnit5 Report generation
Maven Site Plugin creates a folder – site under the target directory.

Junit also produces an “index.html” report, and it resides under the target folder. The below image shows the index.html report. This is the latest theme of the report.


To know more about Playwright with JUnit5, please refer to the official website of Playwright – https://playwright.dev/java/docs/test-runners





























