How to Run Playwright Tests with TestNG in Java

Run Playwright tests with TestNG in Java for browser automation and parallel test execution tutorial

Run Playwright tests with TestNG to leverage the power of modern, fast, and reliable browser automation in a structured Java testing framework. Playwright, developed by Microsoft, supports testing across multiple browsers such as Chromium, Firefox, and WebKit, all from a single API. It enables smooth automation of web applications with features like auto-waiting, tracing, and powerful debugging tools.

When combined with TestNG, Playwright becomes even more efficient. TestNG brings structure, flexibility, and scalability to test execution. It allows developers to group tests, run them in parallel, generate detailed HTML reports, and manage test dependencies seamlessly. This integration makes it easier to handle large-scale test suites and ensures faster feedback during automation runs.

In this Playwright TestNG tutorial, you’ll learn how to configure Playwright with TestNG in Java, set up your first test, and execute it effectively. You’ll also explore advanced capabilities such as parallel execution, test reporting, and cross-browser automation, helping you build a robust and maintainable test automation framework.

Prerequisites

Before you begin to run Playwright tests with TestNG, make sure your system is properly set up with the required tools and dependencies. Below are the essential prerequisites you’ll need to get started.

Required Tools and Setup

To configure Playwright with TestNG in Java, ensure the following tools are installed on your system:

  • Java Development Kit (JDK): Version 11 or higher
  • Apache Maven: For dependency management and project build
  • Eclipse IDE (or IntelliJ IDEA): To write and manage your Java test scripts
  • Playwright for Java: For browser automation
  • TestNG: For test structure, grouping, and reporting

If you haven’t installed Playwright with Java yet, follow this detailed step-by-step guide:

How to Install Playwright with Java, Maven, and Eclipse IDE

Installing Playwright Dependencies

Once your development environment is ready, you’ll need to add the Playwright and TestNG dependencies to your project. This is typically done using Maven by editing the pom.xml file.

Here’s an example snippet:

<dependencies>
    <!-- Playwright Dependency -->
    <dependency>
        <groupId>com.microsoft.playwright</groupId>
        <artifactId>playwright</artifactId>
        <version>1.55.0</version>
    </dependency>

    <!-- TestNG Dependency -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.9.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

After adding these dependencies, right-click your project in Eclipse and select Maven > Update Project.

Maven update project to download Playwright and TestNG libraries in Eclipse
Right-click the project and select Maven → Update Project to download the required Playwright and TestNG dependencies.

This action will download the required Playwright and TestNG libraries into your local Maven repository and make them available in your project.

Configuring the Java Project for Playwright and TestNG

Once the dependencies are installed, configure your test project structure as follows:

src
 ├── main
 │   └── java         → Application source files (optional)
 └── test
     └── java         → Playwright TestNG test scripts

Next, create a testng.xml file at the root of your project. This file defines how your Playwright tests will be executed.

Maven project structure with Playwright and TestNG setup in Eclipse IDE
Project structure showing Maven and TestNG configuration for running Playwright automation tests in Eclipse.

Example:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Playwright TestNG Suite">
  <test name="Playwright Tests">
    <classes>
      <class name="com.example.test.PlaywrightExampleTest"/>
    </classes>
  </test>
</suite>

With these steps complete, your Java project is now ready to run Playwright tests with TestNG.

Write and Run Your First Playwright Test in TestNG

Now that your project is configured, let’s learn how to run Playwright tests in TestNG Java by writing a simple test case. This section will help you understand how Playwright and TestNG work together to automate browser actions efficiently.

Writing a Simple Playwright Test Case

Once your project is configured, it’s time to write and execute your first Playwright test using TestNG. This will help you verify that your setup is working correctly.

Create a new Java class inside the com.example.test package (for example, PlaywrightExampleTest.java) and add the following code:

package com.example.test;


import com.microsoft.playwright.*;
import org.testng.Assert;
import org.testng.annotations.*;

public class PlaywrightExampleTest {

    Playwright playwright;
    Browser browser;
    BrowserContext context;
    Page page;

    @BeforeClass
    public void setUp() {
        // Initialize Playwright and launch browser
        playwright = Playwright.create();
        browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
        context = browser.newContext();
        page = context.newPage();
    }

    @Test
    public void verifyPageTitle() {
        // Navigate to the webpage
        page.navigate("https://example.com");
        
        // Fetch and verify the page title
        String actualTitle = page.title();
        System.out.println("Page Title: " + actualTitle);
        Assert.assertEquals(actualTitle, "Example Domain", "Page title verification failed!");
    }

    @AfterClass
    public void tearDown() {
        // Close browser and Playwright instance
        browser.close();
        playwright.close();
    }
}

Using TestNG Annotations

  • @BeforeClass: Runs once before all test methods to initialize Playwright and the browser.
  • @Test: Contains the actual test logic (for example, verifying a page title).
  • @AfterClass: Executes after all tests are done to close Playwright and free up resources.

Running Playwright TestNG Example Java Code

To run the test:

  • Right-click your test file (PlaywrightExampleTest.java).
  • Select Run As > TestNG Test.
Select Run As TestNG Test option in Eclipse to execute Playwright automation test
In Eclipse, right-click the test file and select Run As → TestNG Test to execute your Playwright test case.
  • You will see the browser open, navigate to the Playwright website, and print the page title in the console.

Once the test completes, the results appear in the JUnit/TestNG panel inside Eclipse IDE.

Troubleshooting Common Errors

Sometimes, you may face setup or runtime issues while running your first Playwright TestNG test. Here are the most common ones and how to fix them.

1. SLF4J Logger Warnings

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation

Cause: This happens because no SLF4J logging implementation is configured.

Fix: You can safely ignore this warning. It doesn’t impact test execution. If you want logs, add a logger like slf4j-simple or logback-classic in your Maven dependencies.

2. UnsupportedClassVersionError

java.lang.UnsupportedClassVersionError: Unsupported major.minor version

Cause: This error occurs when your project is compiled with one Java version but runs with another.

Fix: Make sure both Eclipse and your project are using Java 11 or higher.

Steps to fix:

1. Update JDK in Eclipse:

  • Go to Window → Preferences → Java → Installed JREs
  • Click Add → Standard VM → Browse and select your Java 11 or 17 JDK folder
  • Check the new JDK as the default
In Eclipse, go to Installed JREs and check the new JDK as the default to fix Playwright TestNG runtime issues.
Check the new JDK as the default in Installed JREs under Eclipse preferences for Playwright TestNG setup
  • Click Apply and Close

2. Update your Project JDK:

  • Right-click your project → Properties → Java Build Path → Libraries tab
  • Remove the old JRE System Library
  • Click Add Library → JRE System Library → Alternate JRE → Select Java 11 or 17
  • Apply and rebuild the project
Update project JDK in Eclipse to use the correct Java version for Playwright TestNG automation
In Eclipse, update your project JDK to Java 11 or 17 under Project Properties → Java Build Path to ensure Playwright TestNG runs correctly.

3. Maven Dependency Issues

If Maven dependencies (like Playwright or TestNG) are not downloaded properly, perform a Maven Project Update:

  • Right-click your project
  • Select Maven → Update Project
  • Check Force Update of Snapshots/Releases
  • Click OK

This forces Maven to re-download all dependencies and ensures your project is up to date.

Parallel Execution in Playwright with TestNG

Running tests in parallel is one of the key advantages of using TestNG with Playwright. It helps you save time by executing multiple browser sessions simultaneously. Let’s see how to enable parallel test execution using testng.xml.

Enabling Parallel Test Execution in testng.xml

To execute tests in parallel, configure the testng.xml file by adding the parallel and thread-count attributes.

Here’s an example setup:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Playwright Parallel Suite" parallel="tests" thread-count="2">
    <test name="Test 1">
        <classes>
            <class name="com.example.test.PlaywrightParallelTest"/>
        </classes>
    </test>

    <test name="Test 2">
        <classes>
            <class name="com.example.test.PlaywrightParallelTest"/>
        </classes>
    </test>
</suite>

Explanation:

  • parallel=”tests” → Runs multiple <test> tags in parallel.
  • thread-count=”2″ → Defines the number of threads to use for parallel execution.
  • Each <test> block launches a separate Playwright browser session.

Handling Multiple Browser Sessions

When running tests in parallel, each thread should create its own Playwright and Browser instances to avoid conflicts.

Here’s how you can modify your test class for parallel-safe execution:

package com.example.test;


import com.microsoft.playwright.*;
import org.testng.annotations.*;

public class PlaywrightParallelTest {
    private Playwright playwright;
    private Browser browser;
    private Page page;

    @BeforeMethod
    public void setup() {
        playwright = Playwright.create();
        browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
        page = browser.newPage();
    }

    @Test
    public void openPlaywrightSite() {
        page.navigate("https://playwright.dev/");
        System.out.println("Title in " + Thread.currentThread() + " → " + page.title());
    }

    @AfterMethod
    public void teardown() {
        browser.close();
        playwright.close();
    }
}

Key points:

  • Use @BeforeMethod and @AfterMethod instead of @BeforeClass and @AfterClass.
  • This ensures each test method runs in its own browser context.
  • Thread isolation prevents session conflicts between tests.

Example: Parallel Execution Playwright TestNG Setup

After configuring the test class and testng.xml, run the suite by:

  • Right-clicking the testng.xml file.
  • Selecting Run As → TestNG Suite.

You will see two browser instances open simultaneously, each executing its test in a separate thread. Once all tests finish, TestNG will display the results for all parallel sessions in the report.

Playwright TestNG Reporting

Reporting plays a key role in automation testing as it helps track test execution results and identify failed test cases quickly. TestNG provides built-in HTML reports that are automatically generated after every test run, making it easy to review your Playwright test outcomes without any extra setup.

Generating Default TestNG HTML Reports

When you run Playwright tests with TestNG, a set of default reports is created in the test-output folder of your project. These reports include detailed information about passed, failed, and skipped test cases along with execution time and error details.

After executing your TestNG suite, navigate to:

test-output/
TestNG HTML report generated in the test-output folder after running Playwright tests in Eclipse
After executing Playwright tests with TestNG, view the detailed HTML report located in the test-output folder.

Inside this folder, you’ll find several report files. The most important one is:

test-output/index.html

Open this file in any browser to view your test results.

TestNG HTML report displaying Playwright test execution results with passed and failed test cases
The TestNG HTML report provides a clear summary of Playwright test execution results, including passed, failed, and skipped tests.

What You’ll See in the Report

The TestNG HTML report includes:

  • Summary dashboard: Displays total tests executed, passed, failed, and skipped.
  • Execution details: Shows each test class, method name, and execution time.
  • Stack traces: Provides failure logs and exception details for failed tests.
  • Execution order: Helps track which test ran first and how long it took.

Benefits of Using TestNG Reports for Playwright

  • No extra configuration required: Reports are automatically generated after every run.
  • Lightweight and fast: Perfect for small to medium test suites.
  • Readable HTML format: Can be easily shared with team members.
  • Supports parallel tests: Displays results from concurrent Playwright sessions clearly.

Example Folder Structure After Test Execution

project/
│
├── src/
├── test-output/
│   ├── index.html
│   ├── emailable-report.html
│   ├── testng-results.xml
│   └── ...
└── pom.xml

You can open both index.html and emailable-report.html to analyze the summary and share results via email if needed.

Using TestNG Listeners in Playwright

TestNG listeners are powerful components that let you monitor and customize test execution behavior. In Playwright automation, they are especially useful for logging, capturing screenshots on failure, and generating better test reports.

What Are TestNG Listeners and Why Use Them

Listeners in TestNG act like event handlers. They listen to specific test events such as when a test starts, passes, fails, or gets skipped.

By using listeners, you can automatically trigger custom actions, for example, taking a screenshot when a test fails or logging detailed information for debugging.

Benefits of using listeners in Playwright:

  • Capture screenshots automatically when tests fail
  • Add custom logs or messages to your report
  • Monitor and control test execution flow
  • Reduce repetitive code by centralizing common actions

TestNG provides several listener interfaces, such as:

  • ITestListener: for tracking test lifecycle events
  • ISuiteListener: for handling events before or after a suite run

Implementing Listeners for Logging and Screenshots on Failure

Here’s an example of implementing a simple listener that captures screenshots whenever a Playwright test fails.

Step 1: Create a Listener Class

Create a new class file named TestListener.java inside the listeners package, and paste the following code into it.

package listeners;

import com.microsoft.playwright.*;
import org.testng.ITestListener;
import org.testng.ITestResult;
import java.nio.file.Paths;

public class TestListener implements ITestListener {

    @Override
    public void onTestFailure(ITestResult result) {
        Object testClass = result.getInstance();
        try {
            Page page = (Page) result.getTestContext().getAttribute("page");
            if (page != null) {
                String testName = result.getName();
                page.screenshot(new Page.ScreenshotOptions()
                    .setPath(Paths.get("screenshots/" + testName + ".png")));
                System.out.println("Screenshot captured for failed test: " + testName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Explanation:

  • onTestFailure() triggers automatically when a test fails.
  • It retrieves the page object from the TestNG context and takes a screenshot.
  • Screenshots are saved under the screenshots/ folder with the test name.

Step 2: Add Test Class

Create a test class file named PlaywrightListenerTest.java inside the com.example.test package, and paste the code given below into it.

package com.example.test;

import com.microsoft.playwright.*;
import org.testng.annotations.*;
import org.testng.ITestContext;

public class PlaywrightListenerTest {
	private Playwright playwright;
	private Browser browser;
	private Page page;

	@BeforeMethod
	public void setup(ITestContext context) {
		playwright = Playwright.create();
		browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
		page = browser.newPage();
		context.setAttribute("page", page); // Store page in TestNG context
	}

	@Test
	public void testFailingScenario() {
		page.navigate("https://playwright.dev/");
		assert page.title().contains("Selenium"); // This will fail intentionally
	}

	@AfterMethod
	public void teardown() {
		browser.close();
		playwright.close();
	}
}

Explanation:

  • The page object is stored in the TestNG context so that the listener can access it if a test fails.
  • When the test fails, the listener automatically takes a screenshot and stores it in the screenshots folder.

Step 3: Register the Listener in testng.xml

Update your existing testng.xml file with the configuration given below.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Playwright Listener Suite">
    <listeners>
        <listener class-name="listeners.TestListener"/>
    </listeners>
    <test name="Listener Test">
        <classes>
            <class name="com.example.test.PlaywrightListenerTest"/>
        </classes>
    </test>
</suite>

Step 4: Run the TestNG Listeners Test in Playwright

Once you run(Right-click on textng.xml > Run As TestNG Suite) the suite:

  • If the test passes, it runs normally.
  • If the test fails (in our example, we have deliberately failed a test), a screenshot is automatically saved in the screenshots folder. (Refresh the project folder if you don’t see the screenshots folder.)
Captured Playwright test failure screenshots saved in the screenshots folder during TestNG execution
Playwright automatically saves screenshots of failed TestNG tests inside the screenshots folder for easy debugging.
  • The console output will confirm the screenshot capture with a message like:
Screenshot captured for failed test: testFailingScenario

This approach helps automate debugging and keeps your Playwright TestNG framework robust and easy to maintain.

Cross-Browser Testing with Playwright and TestNG

Supported Browsers in Playwright

Playwright supports all major browsers, including:

  • Chromium (Google Chrome and Microsoft Edge)
  • Firefox
  • WebKit (Safari)

This makes Playwright a perfect choice for ensuring your web application behaves consistently across different browser engines.

Writing Cross-Browser Test Configuration in TestNG

TestNG allows you to parameterize your test cases and easily switch between browsers. By combining Playwright with TestNG parameters, you can run the same test suite on multiple browsers without duplicating code.

You can define browser names in the testng.xml file and use the @Parameters annotation to read them in your test class.

Here’s how you can do it:

<!-- testng.xml -->
<suite name="Playwright Cross Browser Suite">
	<test name="Chromium Test">
		<parameter name="browser" value="chromium" />
		<classes>
			<class name="com.example.test.CrossBrowserTest" />
		</classes>
	</test>

	<test name="Firefox Test">
		<parameter name="browser" value="firefox" />
		<classes>
			<class name="com.example.test.CrossBrowserTest" />
		</classes>
	</test>

	<test name="WebKit Test">
		<parameter name="browser" value="webkit" />
		<classes>
			<class name="com.example.test.CrossBrowserTest" />
		</classes>
	</test>
</suite>

Example: Run Tests on Multiple Browsers Using TestNG Parameters

Below is an example of a Playwright test in Java that runs on different browsers using TestNG parameters:

package com.example.test;

import com.microsoft.playwright.*;
import org.testng.annotations.*;

public class CrossBrowserTest {
	Playwright playwright;
	Browser browser;
	BrowserContext context;
	Page page;

	@Parameters("browser")
	@BeforeMethod
	public void setup(String browserName) {
		playwright = Playwright.create();

		switch (browserName.toLowerCase()) {
		case "chromium":
			browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
			break;
		case "firefox":
			browser = playwright.firefox().launch(new BrowserType.LaunchOptions().setHeadless(false));
			break;
		case "webkit":
			browser = playwright.webkit().launch(new BrowserType.LaunchOptions().setHeadless(false));
			break;
		default:
			throw new IllegalArgumentException("Invalid browser name: " + browserName);
		}

		context = browser.newContext();
		page = context.newPage();
	}

	@Test
	public void verifyHomePageTitle() {
		page.navigate("https://playwright.dev/");
		System.out.println("Title on " + browser.browserType().name() + ": " + page.title());
	}

	@AfterMethod
	public void tearDown() {
		context.close();
		browser.close();
		playwright.close();
	}
}

In this setup:

  • Each test run picks up a browser parameter from the testng.xml file.
  • The Playwright instance launches the respective browser type.
  • The same test logic executes across Chromium, Firefox, and WebKit.

This approach ensures efficient cross-browser validation while keeping your test code clean and maintainable.

Playwright vs Selenium with TestNG

Comparison: Selenium vs Playwright TestNG Integration

While both Selenium and Playwright can be integrated with TestNG for browser automation, there are key differences in their performance, architecture, and ease of use. The table below highlights the main points of comparison:

FeatureSelenium with TestNGPlaywright with TestNG
Setup & ConfigurationRequires WebDriver setup for each browserNo WebDriver needed, built-in browser support
Supported BrowsersChrome, Edge, Firefox, Safari (via drivers)Chromium, Firefox, WebKit (built-in)
Execution SpeedModerate, depends on WebDriver communicationFaster, uses direct browser communication
Auto-Wait MechanismManual waits or explicit waits neededBuilt-in auto-wait and smart element handling
Parallel Test ExecutionSupported through TestNG threadsBuilt-in and efficient parallelism
Network InterceptionRequires third-party librariesNative support for network mocking and interception
Handling Frames & PopupsRequires additional handling logicSimplified frame and popup management
Trace Viewer / DebuggingLimited debugging optionsSimplified frame and pop-up management
API Testing SupportNot supported directlyBuilt-in API testing capabilities
Community & EcosystemMature and widely adoptedRapidly growing modern ecosystem

When to Choose Playwright Over Selenium

You should consider Playwright with TestNG if:

  • You need faster test execution with built-in browser binaries.
  • You prefer auto-waiting and reliable element handling without manual synchronization.
  • You want to perform cross-browser testing across Chromium, Firefox, and WebKit from one setup.
  • You aim for modern web app testing, including handling single-page applications (SPAs) and dynamic UI elements.
  • You require advanced debugging tools such as a trace viewer and network logging.

Performance and Maintenance Differences

Playwright offers superior performance due to its direct browser communication layer. It doesn’t rely on WebDriver, which reduces latency and improves execution time.

From a maintenance perspective, Playwright scripts are cleaner and less flaky because of:

  • Smart waiting mechanisms
  • Unified APIs for all browsers
  • Simplified configuration (no driver management)

In contrast, Selenium remains a strong choice for legacy projects or teams already deeply invested in its ecosystem. However, for new automation frameworks with TestNG, Playwright provides a faster, more modern, and more maintainable solution.

Conclusion

In this tutorial, you learned how to run Playwright tests with TestNG in Java, from setting up the environment to executing and reporting test results. By combining Playwright’s modern automation capabilities with TestNG’s structured test management, you can build a robust and scalable testing framework for your projects.

As you progress, explore advanced topics like parallel test execution, cross-browser testing, and detailed TestNG HTML reports to enhance your automation suite. These features will help you achieve faster, more reliable, and maintainable test runs.

If you’re interested in expanding your knowledge further, check out these related guides:

By mastering Playwright with TestNG, you can bring more power, speed, and stability to your Java automation testing journey.

Stay Updated with New Articles
Get the latest tutorials and insights delivered to your inbox.

Leave a Reply

Your email address will not be published. Required fields are marked *