When working with Playwright Java for browser automation and testing, one of the most essential steps is launching a browser instance before interacting with a web application. Playwright supports three main browser engines out of the box:
- Chromium – The open-source browser engine behind Google Chrome and Microsoft Edge.
- Firefox – Mozilla’s fast and secure browser engine.
- WebKit – The browser engine used by Safari.
The beauty of Playwright is that it provides a single API to work with all three, making cross-browser testing smooth and consistent.
In this article, we will cover:
- Installing Playwright Java dependencies
- Understanding browser engines
- Launching Chromium, Firefox, and WebKit browser engines
- Different launch modes (headed, Slow Motion, launching real browsers, etc.)
By the end, you’ll be able to confidently launch any browser in Playwright Java and know exactly which options to choose for your tests.
Installing Playwright Java dependencies
Before we can launch browsers with Playwright in Java, we first need to add Playwright as a dependency to our project.
For Maven:
<!-- https://mvnrepository.com/artifact/com.microsoft.playwright/playwright -->
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.53.0</version>
</dependency>
For Gradle:
// https://mvnrepository.com/artifact/com.microsoft.playwright/playwright
implementation("com.microsoft.playwright:playwright:1.53.0")
Understanding browser engines
A browser engine (sometimes called a “layout engine” or “rendering engine”) is a specialized software component within a web browser that interprets web content (HTML, CSS, sometimes JavaScript) and renders it visually on your screen. Essentially, the browser engine is responsible for turning the code of a website into the web page you see and interact with.
Key Examples of Browser Engines
Engine | Used By |
---|---|
Chromium | Google Chrome, Microsoft Edge |
Firefox | Mozilla Firefox |
WebKit | Safari, iOS browsers |
Key point: All three are installed automatically when you install Playwright, so you don’t need separate setups.
How Browser Engines Differ from Actual Browsers
While they are closely intertwined, a browser engine is not itself a browser. The browser is the full application that includes:
- The user interface (UI): tabs, back/forward buttons, address bar, menus, etc.
- Tools for bookmarking, downloads, settings management, etc.
- JavaScript engine: executes scripts embedded in web pages.
- Networking: handles requests, cookies, caching, and more.
- Sometimes, developer tools and privacy features.
The browser engine is just the part that draws web pages and interprets how they should look and behave based on web standards. The browser is the full package that delivers a complete browsing experience to users.
Launching Browser engines in Playwright Java
Once you’ve installed Playwright and understand browser engines, the next step is launching them before interacting with a website.
Launching Chromium
Here’s the code to launch a website in the chromium engine:
import com.microsoft.playwright.*;
public class CodekruTest {
public static void main(String[] args) {
//Initializes the Playwright library.
Playwright playwright = Playwright.create();
// 1. Launch Chromium browser
Browser browser = playwright.chromium().launch();
// 2. Create a new isolated browser context (like a fresh browser profile)
BrowserContext context = browser.newContext();
// 3. Open a new page (tab) inside the browser context
Page page = context.newPage();
// 4. Navigate to the target website
page.navigate("https://testkru.com/Elements/TextFields");
// 5. Retrieve and print the page title
System.out.println("Title: " + page.title());
// 6. Close the sessions
browser.close();
playwright.close();
}
}
Step-by-Step Explanation
1. Playwright.create()
- Initializes the Playwright library.
- Establishes a connection between your Java program and the underlying browser automation protocols.
2. playwright.chromium()
- Selects the Chromium engine from the three available ones (chromium(), firefox(), webkit()).
- This is where you decide which browser you want to automate.
3. .launch()
- Starts the browser process.
- Headless mode is enabled by default (no UI is shown), which makes tests faster.
4. browser.newContext()
- Creates a fresh, isolated browser profile (like opening Chrome with a new user profile).
- Contexts do not share cookies, cache, or local storage which is perfect for running independent tests without interference.
- Allows multiple parallel sessions in the same browser instance.
5. context.newPage()
- Opens a new tab in the current browser context.
- A Page object represents one browser tab. All your interactions like click(), fill(), or navigate() happen on a Page.
6. page.navigate()
- Loads the given URL.
- Playwright waits until the page is fully loaded before continuing to the next step (unless configured otherwise).
7. page.title()
- Retrieves the title of the current page, which is useful for assertions in test automation. In this example, we used it simply to confirm that the URL loaded correctly.
8. browser.close()
- Closes the entire browser instance.
- Important for releasing memory and avoiding too many open browser processes.
Launching Firefox
Launching Firefox is just as simple, replace playwright.chromium().launch() with playwright.firefox().launch(), and everything else remains the same.
Only the browser engine method changes from .chromium() to .firefox().
Launching Safari
Similarly we can use playwright.webkit().launch() to launch the safari browser.
Note: Here, we’re launching the browser engines, not the full browsers. Later in this post, we’ll also see how to launch the actual browsers.
Different launch modes
When launching a browser in Playwright, you can customize how it starts. Playwright’s BrowserType.LaunchOptions lets you configure everything at the time of launching.
Headed Mode (UI Mode)
By default, Playwright runs browsers in headless mode, meaning no browser window is visible. However, we can override this behavior to watch our tests execute in real time.
Browser browser = playwright.chromium().launch(
new BrowserType.LaunchOptions().setHeadless(false)
);
By adding this line, we can see the Chromium browser open and execute our steps, making it easy to follow along.
Whole code –
import com.microsoft.playwright.*;
public class CodekruTest {
public static void main(String[] args) {
//Initializes the Playwright library.
Playwright playwright = Playwright.create();
// 1. Launch Chromium browser
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
// 2. Create a new isolated browser context (like a fresh browser profile)
BrowserContext context = browser.newContext();
// 3. Open a new page (tab) inside the browser context
Page page = context.newPage();
// 4. Navigate to the target website
page.navigate("https://testkru.com/Elements/TextFields");
// 5. Retrieve and print the page title
System.out.println("Title: " + page.title());
// 6. Close the sessions
browser.close();
playwright.close();
}
}
Slow Motion (Slomo) Mode
Slows down each Playwright action by a set number of milliseconds. It Helps in debugging and understanding exactly what’s happening step-by-step.
Browser browser = playwright.chromium().launch(
new BrowserType.LaunchOptions()
.setHeadless(false)
.setSlowMo(100) // 100ms delay between actions
);
Launching Real Browsers
As mentioned earlier, Playwright launches browser engines (like Chromium, WebKit, etc.) rather than the full browsers. However, if needed, we can also launch the actual browser using the code below.
Browser browser = playwright.chromium().launch(
new BrowserType.LaunchOptions()
.setChannel("chrome")
);
Above piece of code will open a real chrome browser on our device.
Note: Playwright Java does not support .setChannel(“firefox”) the same way it does for Chrome, because Firefox support is tied to its Playwright-specific build of Gecko.
We hope that you have liked the article. If you have any doubts or concerns, please write to us in the comments or mail us at admin@codekru.com.