In this section, we will see how to set up Playwright automation using Java and TestNG from scratch.
Playwright is a modern browser automation framework developed by Microsoft. It enables you to automate web applications across Chromium, Firefox, and WebKit using a single API. Playwright supports JavaScript, TypeScript, Java, .NET, and Python.
One of Playwright’s key strengths is its built-in auto-waiting mechanism, which helps reduce flaky tests and eliminates the need for manual waits in most scenarios.
Prerequisites
Before starting Playwright automation with Java, make sure you are comfortable with the basics of Java and TestNG.
1. Java Basics
2. TestNG Tutorial
Pre-Requisites Setup
To get started, install the following tools:
IDE: Install an IDE, preferably IntelliJ IDEA.
Java Development Kit (JDK): Install JDK (Java 11 or above is recommended).
You can follow this guide to install JDK: Link
Once the IDE and JDK are installed, we can proceed with installing Playwright and TestNG using Maven.
Adding Playwright and TestNG to the Project
After creating a new Maven project in your IDE, open the pom.xml file located in the project root directory.
Here, we need to add dependencies for Playwright and TestNG.
Playwright Dependency
You can get the latest Playwright Java dependency from:
Playwright documentation: official link
Maven Repository: official link
Open the latest version and copy the Maven dependency.
TestNG Dependency
To add TestNG:
- Search for TestNG Maven Dependency
- Or go directly to Maven Repository: Home » org.testng » testng
- Open the latest stable version
- Copy the dependency snippet
Final pom.xml
After adding both dependencies, your pom.xml should look like this:
After saving pom.xml, restart or reimport the Maven project in your IDE so that Playwright and TestNG dependencies are downloaded and ready to use.
Note: Playwright automatically manages browser binaries.
Writing the First Script in Playwright Java
Now, let’s write our first script in Playwright-Java.
We will create the test inside the src/test/java folder.
Create a java class in the IDE:
1. Navigate to src/test/java
2. Create a new Java class
3. Write automation script in Playwright Java (example code and code explanation is given below)
Playwright Script in Java (Single Class)
import com.microsoft.playwright.*;
import org.testng.annotations.*;
public class HomePageTestSingleClass {
// Playwright core objects
Playwright playwright;
Browser browser;
BrowserContext context;
Page page;
/**
* This method runs before each test method.
* It initializes Playwright, launches the browser,
* and creates a fresh browser context and page.
*/
@BeforeMethod
public void setUp() {
playwright = Playwright.create();
// Launch Chromium browser in non-headless mode
browser = playwright.chromium().launch(
new BrowserType.LaunchOptions().setHeadless(false)
);
// Create isolated browser context and page
context = browser.newContext();
page = context.newPage();
}
/**
* Test to verify navigation to the Demo Site page
* from the QA Feast homepage.
*/
@Test
public void verifyDemoSiteNavigation() {
// Navigate to QA Feast website
page.navigate("https://www.qafeast.com/");
// Accept cookie consent banner
page.locator("#gdpr-cookie-accept").click();
// Click on Tools menu
page.locator("li[class='tut_ fr_tls']>a").click();
// Navigate to Demo Site
page.locator("//li/a[text()='Demo Site']").click();
// Locate the Textbox element to verify page load
Locator textbox = page.locator("//li/label[text()='Textbox']");
textbox.waitFor();
// Assertion to confirm Demo Site page is opened
assert textbox.isVisible() : "Demo Site page did not open";
}
/**
* This method runs after each test method.
* It closes the browser and releases Playwright resources.
*/
@AfterMethod
public void tearDown() {
browser.close();
playwright.close();
}
}
Script Explanation:
Playwright.create(): Initializes the Playwright automation engine.
Browser Launch: Launches the Chromium browser in non-headless mode so we can visually see the test execution.
BrowserContext: Creates an isolated browser session similar to an incognito window.
Page Creation: Opens a new browser tab within the created browser context.
Navigation: Navigates the browser to the QA Feast website.
User Actions: Accepts the cookie banner, clicks on the Tools menu, and navigates to the Demo Site page.
Validation: Verifies that the Demo Site page is opened by checking the visibility of the “Textbox” heading.
Cleanup: Closes the browser and releases all Playwright resources.
Executing the Playwright-Java automation script using testng.xml
- Create a testng.xml file in the project root folder.
- Paste this script inside testng.xml file
Run from IDE
TestNG will execute only the tests defined in the XML file.
- Right-click on testng.xml
- Select Run 'testng.xml'
For complete testng.xml execution tutorial: Click Here
Run Using Maven directly from Command Line
- To run TestNG tests from the command line, execute: mvn test
- Maven uses the Surefire plugin to detect testng.xml and execute the configured test suite.