We can use java programming language to automate actions on a web browser. It can perform tasks like opening a webpage, clicking buttons, filling forms, and verifying content. Playwright provides APIs to interact with different browsers (Chromium, Firefox, WebKit) programmatically.
Before writing our first script in Playwright Java, We should aware of following points:
- Playwright is a Node.js library for automating web browsers.
- It allows you to interact with web pages: click buttons, fill forms, navigate pages, and run tests.
- It supports multiple browsers: Chromium (Chrome), Firefox, WebKit (Safari).
- It’s mainly used for UI testing and web automation.
Steps to Create a First Playwright Script in Java
Before writing code, we should have clear that playwright has four main steps:
Step 1: Set Up the Environment
- Install Java (JDK 11 or above) and a build tool like Maven or Gradle.
- Add Playwright as a dependency in your project.
- Playwright automatically downloads the necessary browser binaries.
Step 2: Launch a Browser
- You start by creating a browser instance.
- Playwright supports different browser types like Chromium, Firefox, Webkit, etc.
- We can choose to launch in headless mode (no GUI, faster) or headed mode (browser GUI visible).
- A page represents a single browser tab.
- Just open a page and navigate it to a URL you want to test.
- This allows your script to interact with the web application.
- You can locate elements on the page (buttons, input fields, links) using selectors like ID, class, text, or XPath.
- It performs actions such as click, type text, select dropdowns, take screenshots, etc.
- After the test, the script closes the page and the browser instance.
- This ensures resources are released properly.
<dependencies> <dependency> <groupId>com.microsoft.playwright</groupId> <artifactId>playwright</artifactId> <version>1.45.0</version> <!-- Use the latest available version --> </dependency> </dependencies>
Step 2: Basic Playwright Script in Java
Below is the first playwright script written in java and this script is launching a website: https://www.example.com, fetching the title of website and displaying on console.
import com.microsoft.playwright.*; public class FirstPlaywrightTest { public static void main(String[] args) { // 1. Launch Playwright try (Playwright playwright = Playwright.create()) { // 2. Launch a browser (Chromium in this case) Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false)); // 3. Open a new browser context (like a clean browser profile) BrowserContext context = browser.newContext(); // 4. Create a new page/tab in the browser Page page = context.newPage(); // 5. Navigate to the desired URL page.navigate("https://example.com"); // 6. Fetch the page title String title = page.title(); System.out.println("Page title is: " + title); // 7. Close the browser browser.close(); } } }
Line | Code | Explanation |
---|---|---|
1 | import com.microsoft.playwright.*; | Imports all Playwright Java classes. |
5 | Playwright.create() | Initializes Playwright engine. Must be closed after use. |
8 | playwright.chromium().launch(...) | Launches Chromium browser (similar to Chrome). You can also use .firefox() or .webkit() . |
11 | browser.newContext() | Creates an isolated browser context (like incognito). Useful for running multiple tests. |
14 | context.newPage() | Opens a new tab (page) in the browser. |
17 | page.navigate(...) | Loads the given URL in the browser. |
20 | page.title() | Returns the page’s title. |
23 | browser.close() | Gracefully shuts down the browser. |
Right Click > Run As > Java Application
Suggested Posts:
1. Handle Alerts in Playwright
2. BrowserContext in Playwright
3. Handle Dropdowns in Playwright
4. Handle IFrames in Playwright
5. Video Recording in Playwright