Estimated Reading Time: 10–15 minutes
In Part 1, we learned what Playwright is, why it is becoming the preferred browser automation framework, and how to prepare your development environment.
In this part, you'll create your first Playwright Java project, configure Maven, install browser binaries, and write your very first browser automation program.
Open your favorite IDE (IntelliJ IDEA is recommended) and create a new Maven Project.
Use the following sample details:
Group Id: com.rookienerd.playwright
Artifact Id: playwright-java-demo
Java Version: 17 or above
Once the project is created, you'll see a folder structure similar to:
playwright-java-demo
├── src
│ ├── main
│ └── test
├── pom.xml
└── target
The pom.xml file is the heart of a Maven project. It manages dependencies, plugins, and build configurations.
Open the pom.xml file and add the latest Playwright dependency from the official Playwright documentation.
After saving the file, Maven automatically downloads the required libraries.
Tip: Avoid hardcoding version numbers from blog posts. Always check the official documentation for the latest stable release.
Playwright requires browser binaries before running tests.
Open a terminal in the project directory and run:
mvn exec:java -e -Dexec.mainClass=com.microsoft.playwright.CLI -Dexec.args="install"
This downloads:
Chromium
Firefox
WebKit
The download happens only once unless you reinstall or upgrade Playwright.
Before writing code, let's understand the core classes you'll use.
Creates the Playwright engine.
Playwright playwright = Playwright.create();
Think of this as starting the automation session.
Represents the browser instance.
Browser browser = playwright.chromium().launch();
You can also launch:
Firefox
WebKit
using similar APIs.
A Browser Context acts like a fresh browser profile.
Each context has its own:
Cookies
Local Storage
Session Storage
Using separate contexts keeps your tests isolated and reliable.
A Page represents a browser tab.
Page page = context.newPage();
Almost every interaction in Playwright happens through the Page object.
Create a Java class named:
LaunchBrowser.java
Add the following code:
import com.microsoft.playwright.*;
public class LaunchBrowser {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch(
new BrowserType.LaunchOptions()
.setHeadless(false));
BrowserContext context = browser.newContext();
Page page = context.newPage();
page.navigate("https://example.com");
System.out.println("Page Title : " + page.title());
browser.close();
}
}
}
Playwright.create();
Starts the Playwright engine.
playwright.chromium().launch();
Launches a Chromium browser.
setHeadless(false)
Opens the browser in headed mode, allowing you to watch the automation.
For CI/CD pipelines, change it to:
setHeadless(true)
This runs the browser in the background and is generally faster.
browser.newContext();
Creates an isolated browser session.
This is one of Playwright's key strengths because tests do not share cookies or login sessions unless you choose to.
context.newPage();
Opens a new browser tab.
page.navigate("https://example.com");
Navigates to the specified URL.
Playwright automatically waits for the page to load before proceeding.
page.title();
Retrieves the page title.
Expected output:
Page Title : Example Domain
Capturing screenshots is useful for debugging and reporting.
page.screenshot(
new Page.ScreenshotOptions()
.setPath(Paths.get("homepage.png")));
After execution, the screenshot will be saved in your project directory.
Playwright makes locating elements simple.
Click a button:
page.locator("#loginButton").click();
Enter text:
page.locator("#username").fill("rookienerd");
Enter a password:
page.locator("#password").fill("Password123");
Click Login:
page.locator("button[type='submit']").click();
Unlike many older frameworks, Playwright automatically waits for elements to become ready before interacting with them.
You can validate the title as follows:
String actualTitle = page.title();
if(actualTitle.contains("Example")){
System.out.println("Test Passed");
}
else{
System.out.println("Test Failed");
}
Later in this series, we'll use proper assertions with JUnit or TestNG instead of manual checks.
Error:
Executable doesn't exist
Solution:
Install the browser binaries using the Playwright CLI.
Error:
Unsupported class version
Solution:
Install Java 17 or later and ensure JAVA_HOME is configured correctly.
If dependencies are not downloaded:
Reload the Maven project.
Run mvn clean install.
Check your internet connection and Maven repository settings.
Use the latest stable Playwright version.
Prefer Playwright locators over XPath whenever possible.
Avoid Thread.sleep() for synchronization.
Close browsers after each test to free resources.
Organize your code into reusable page objects as your project grows.
Congratulations! You have now:
Created your first Maven Playwright project.
Configured Playwright dependencies.
Installed browser binaries.
Launched Chromium.
Opened a web page.
Retrieved the page title.
Captured a screenshot.
Interacted with basic web elements.
These fundamentals form the foundation of any Playwright automation framework.
Playwright offers built-in auto-waiting, browser contexts, network interception, and modern APIs that reduce boilerplate code. Selenium remains an excellent choice with broad ecosystem support, but Playwright can simplify many common automation tasks.
Java 17 or later is recommended for new Playwright projects.
Yes. Playwright supports Chromium-based browsers, including Google Chrome and Microsoft Edge, as well as Firefox and WebKit.
Yes. Many teams combine Playwright with TestNG or JUnit for test organization, assertions, reporting, and parallel execution.
Playwright Locators Explained with Real-Time Examples
In the next article, you'll learn:
Text locators
CSS locators
XPath (when to avoid it)
Role locators
Label locators
Placeholder locators
Chaining locators
Best practices for writing stable and maintainable Playwright tests.