Showing posts with label Automation Frameworks. Show all posts
Showing posts with label Automation Frameworks. Show all posts

Playwright with TestNG Integration




















How to Integrate Playwright with TestNG in Java for UI Automation (Complete Guide with Assertions & Example)

Modern web applications demand reliable, scalable, and maintainable automation testing. If you're working with Java and looking for a powerful end-to-end testing solution, combining Playwright with TestNG is one of the most effective approaches available today.

In this detailed, SEO-optimized guide, you’ll learn:

  • What Playwright and TestNG are

  • Why integrating them is beneficial

  • How to configure Maven dependencies

  • How to structure a Playwright + TestNG framework

  • How to write assertions using TestNG

  • How to execute parallel tests

  • Best practices for scalable automation

  • A complete working example using the OrangeHRM demo application

We’ll use the demo website:

👉 https://opensource-demo.orangehrmlive.com/web/index.php/auth/login

Let’s dive in.


What is Playwright?

Playwright is a modern open-source browser automation library developed by Microsoft. It supports multiple browsers including:

  • Chromium

  • Firefox

  • WebKit

Playwright provides APIs to:

  • Automate web UI interactions

  • Handle elements dynamically

  • Capture screenshots

  • Perform API testing

  • Run headless or headed browser sessions

  • Execute tests in parallel

Playwright supports multiple programming languages including Java, making it ideal for Java-based automation frameworks.


What is TestNG?

TestNG is a powerful Java testing framework inspired by JUnit and NUnit. It provides advanced testing capabilities such as:

  • Annotations (@Test, @BeforeClass, @AfterClass, etc.)

  • Test grouping

  • Parallel execution

  • Data-driven testing

  • HTML and XML reports

  • Parameterization

When combined with Playwright, TestNG handles test orchestration, while Playwright handles browser automation.


Why Integrate Playwright with TestNG?

Integrating Playwright with TestNG gives you the best of both worlds.

1️⃣ Structured Test Execution

TestNG provides lifecycle annotations like:

  • @BeforeSuite

  • @BeforeClass

  • @Test

  • @AfterClass

This helps you systematically manage browser setup and teardown.


2️⃣ Parallel Execution Support

TestNG allows parallel execution through testng.xml, and Playwright supports multiple independent BrowserContext instances. Together, they allow:

  • Cross-browser testing

  • Parallel test execution

  • Faster build cycles


3️⃣ Built-in Reporting

TestNG automatically generates:

  • HTML reports

  • XML reports

  • Execution summaries

This makes debugging and tracking automation results easy.


4️⃣ Data-Driven Testing

TestNG supports:

  • @DataProvider

  • Parameterized tests

  • External data sources

You can reuse the same Playwright test logic with multiple datasets.


5️⃣ CI/CD Friendly

Both Playwright and TestNG integrate smoothly with:

  • Maven

  • Gradle

  • Jenkins

  • GitHub Actions

  • Azure DevOps

This makes your automation pipeline production-ready.


How Playwright + TestNG Work Together

Let’s understand the integration flow.

Step 1: Add Maven Dependencies

You need both Playwright and TestNG dependencies inside your pom.xml.

<dependencies> <!-- Playwright Java --> <dependency> <groupId>com.microsoft.playwright</groupId> <artifactId>playwright</artifactId> <version>1.44.0</version> </dependency> <!-- TestNG --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.8.0</version> <scope>test</scope> </dependency> </dependencies>

After adding dependencies, run:

mvn clean install

This downloads all required libraries.


Project Structure Best Practice

A clean structure improves maintainability:

src └── test └── java ├── base │ └── BaseTest.java ├── tests │ └── LoginPageTest.java └── utils
  • BaseTest → Handles browser setup

  • Test classes → Contains actual test cases

  • Utils → Helper methods (optional)


Understanding TestNG Lifecycle with Playwright

Here’s how lifecycle management works:

AnnotationPurpose
@BeforeClassLaunch browser and open page
@TestPerform automation steps
@AfterClassClose browser

This ensures proper setup and teardown for each test class.


Example: Playwright + TestNG Assertions (OrangeHRM Login Page)

We will validate:

  • Page title

  • Login button visibility

  • Login button text

Website under test:
https://opensource-demo.orangehrmlive.com/web/index.php/auth/login


Complete LoginPageTest Class

import com.microsoft.playwright.*; import org.testng.Assert; import org.testng.annotations.*; public class LoginPageTest { Playwright playwright; Browser browser; BrowserContext context; Page page; @BeforeClass public void setUp() { playwright = Playwright.create(); browser = playwright.chromium() .launch(new BrowserType.LaunchOptions().setHeadless(false)); context = browser.newContext(); page = context.newPage(); page.navigate("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login"); } @Test public void testTitle() { String title = page.title(); Assert.assertTrue(title.contains("OrangeHRM"), "Title does not contain 'OrangeHRM'"); } @Test public void testLoginButtonVisible() { Locator loginButton = page.locator("button[type='submit']"); Assert.assertTrue(loginButton.isVisible(), "Login button is not visible"); } @Test public void testLoginButtonText() { Locator loginButton = page.locator("button[type='submit']"); String buttonText = loginButton.textContent().trim(); Assert.assertEquals(buttonText, "Login", "Login button text mismatch"); } @AfterClass public void tearDown() { browser.close(); playwright.close(); } }


What Are We Asserting?

AssertionWhat It Validates
Assert.assertTrue()Condition must be true
Assert.assertEquals()Actual value matches expected
title.contains("OrangeHRM")Page title verification
loginButton.isVisible()UI element visibility
textContent()Inner text validation


Important Playwright Methods for Assertions

MethodPurpose
page.title()Get page title
locator.textContent()Get element text
locator.isVisible()Check visibility
locator.getAttribute()Get attribute value
page.url()Validate URL

These methods fetch data which TestNG then validates.


Running Tests with TestNG XML (Parallel Execution)

Create a testng.xml file:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="PlaywrightSuite" parallel="tests" thread-count="2"> <test name="LoginTests"> <classes> <class name="LoginPageTest"/> </classes> </test> </suite>

This allows:

  • Multiple test classes to run simultaneously

  • Faster execution

  • Better resource usage

Because Playwright uses isolated BrowserContext, tests won’t interfere with each other.


Advantages of Playwright + TestNG Framework

1️⃣ Clean Separation of Responsibilities

  • Playwright → Browser Automation

  • TestNG → Test Management


2️⃣ High Scalability

You can:

  • Add multiple browsers

  • Run parallel tests

  • Execute thousands of test cases


3️⃣ Maintainable Code

Using annotations ensures:

  • Clean setup

  • Reusable methods

  • Easy debugging


4️⃣ CI/CD Ready

You can integrate with:

  • Jenkins

  • Azure DevOps

  • GitHub Actions

Maven makes execution simple:

mvn test


Best Practices for Playwright + TestNG Framework

✅ Use a Base Class

Avoid repeating browser setup in every test class.


✅ Use Explicit Waits When Needed

Although Playwright auto-waits, sometimes use:

page.waitForSelector("selector");


✅ Use Page Object Model (POM)

Separate locators from test logic for better maintainability.


✅ Enable Headless Mode in CI

.setHeadless(true)


✅ Capture Screenshots on Failure

You can use:

page.screenshot();

Integrate this inside TestNG listeners.


Common Interview Questions

Q1: Why use TestNG instead of JUnit?

TestNG supports better parallel execution, parameterization, and grouping.

Q2: Does Playwright support parallel testing?

Yes. Using BrowserContext instances along with TestNG parallel execution.

Q3: Is Playwright better than Selenium?

Playwright provides:

  • Auto-waiting

  • Faster execution

  • Built-in network interception

  • Better modern browser support


Conclusion

Integrating Playwright with TestNG in Java creates a powerful, scalable, and maintainable test automation framework.

You get:

  • Structured test lifecycle management

  • Strong assertion capabilities

  • Parallel execution

  • Built-in reporting

  • CI/CD integration

  • Modern browser automation

By combining Playwright’s powerful browser automation capabilities with TestNG’s structured testing framework, you can build enterprise-level UI automation frameworks that are efficient, reliable, and future-ready.


At a Glance Summary

✔ Use Playwright for browser automation
✔ Use TestNG for test orchestration
✔ Add Maven dependencies
✔ Structure tests using lifecycle annotations
✔ Use TestNG assertions for validation
✔ Configure parallel execution in testng.xml
✔ Integrate with CI/CD


Suggested Posts:

1. BrowserContext in Playwright
2. Automate GET API in Playwright
3. Automate POST API in Playwright
4. Handle Alerts in Playwright
5. How to Show Visible Elements in Playwright

Integration of Playwright, Cucumber, and TestNG




Complete Guide to Integrating Cucumber, Playwright, and TestNG in Java (Step-by-Step Tutorial)

Modern test automation frameworks demand speed, scalability, readability, and maintainability. If you are working in automation testing or preparing for SDET roles, integrating Cucumber, Playwright, and TestNG in Java is one of the most powerful combinations you can learn today.

This detailed, fully SEO-optimized and AdSense-friendly guide explains:

  • What is Cucumber, Playwright, and TestNG?

  • Role of each tool in automation

  • How integration works step-by-step

  • Complete Maven configuration

  • Feature file implementation

  • Step definitions with Playwright

  • TestNG runner configuration

  • Parallel execution setup

  • Benefits of this integration

  • Real-time project structure

By the end of this guide, you will clearly understand how to build a professional BDD automation framework using Java.


What is Cucumber, Playwright, and TestNG Integration?

Integrating Cucumber, Playwright, and TestNG in Java enables you to:

  • Write BDD-style test cases using Gherkin (.feature files)

  • Automate browser actions using Playwright

  • Manage execution using TestNG

  • Generate structured and readable reports

This combination is widely used in enterprise-level automation frameworks because it ensures:

  • Clean architecture

  • Collaboration between business and QA teams

  • Cross-browser testing

  • Parallel execution support

  • Advanced reporting capabilities


Role of Each Component in the Framework

Let’s understand the purpose of each tool clearly.


1. Playwright – The Browser Automation Engine

Playwright is a modern browser automation framework that supports:

  • Chromium

  • Firefox

  • WebKit

Playwright is responsible for:

  • Launching browsers

  • Navigating web pages

  • Clicking elements

  • Entering text

  • Handling alerts

  • Capturing screenshots

  • Validating UI elements

  • Performing cross-browser testing

It provides fast and reliable execution with auto-waiting features and modern selector strategies.

In simple words:

👉 Playwright performs actual browser operations.


2. Cucumber – The BDD Framework

Cucumber is a Behavior-Driven Development (BDD) framework that allows writing test scenarios in Gherkin language.

Gherkin format:

  • Given

  • When

  • Then

  • And

  • But

Example:

Given user launches the browser When user logs in with valid credentials Then homepage should be displayed

Benefits of Cucumber:

  • Makes test cases readable

  • Bridges gap between business and technical teams

  • Allows non-technical stakeholders to understand test scenarios

  • Encourages collaboration

In simple words:

👉 Cucumber defines what needs to be tested in plain English.


3. TestNG – The Test Execution Framework

TestNG is a powerful Java testing framework that provides:

  • Test configuration

  • Annotations (@Before, @After, etc.)

  • Parallel execution

  • Grouping tests

  • Data-driven testing

  • Detailed reporting

  • Retry mechanisms

TestNG controls:

  • How tests run

  • In what order they run

  • Whether they run in parallel

  • How reports are generated

In simple words:

👉 TestNG manages execution and reporting.


How Cucumber + Playwright + TestNG Integration Works

Let’s understand the flow step-by-step.


Step 1: Cucumber Defines Test Scenarios

Business or QA teams write scenarios inside feature files using Gherkin syntax.

Example:

Feature: Check title Scenario: Open login page and check title Given I launch the application Then I should see the page title as "Example Domain"

These feature files describe expected behavior.


Step 2: Step Definitions Use Playwright

Each Gherkin step is mapped to a Java method called Step Definition.

Inside these step definitions:

  • Playwright APIs are used.

  • Browser actions are performed.

  • Assertions validate results.

For example:

  • page.navigate()

  • page.title()

  • page.click()

Playwright executes real browser interactions.


Step 3: TestNG Executes Scenarios

TestNG acts as the test runner.

It:

  • Executes Cucumber feature files

  • Controls parallel execution

  • Generates execution reports

  • Integrates with Maven

TestNG can run scenarios in parallel using DataProvider.


Step 4: Unified Reporting

After execution:

  • Cucumber generates BDD-style reports.

  • TestNG generates execution reports (HTML/XML).

  • Reports can be integrated with advanced tools like Allure or Extent Reports.

This ensures:

  • Business-friendly reports

  • Developer-friendly logs


Tools Used in This Framework

  • Cucumber for BDD feature files

  • Playwright for Java for browser automation

  • TestNG for execution and reporting

  • Maven for dependency management

Website to be automated:

https://example.com


Step-by-Step Integration Setup

Let’s implement a simple example that verifies the title of https://example.com.


1. pom.xml Configuration

The pom.xml file manages project dependencies.

<project> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>cucumber-playwright-testng</artifactId> <version>1.0</version> <dependencies> <!-- Cucumber Dependencies --> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-java</artifactId> <version>7.14.0</version> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-testng</artifactId> <version>7.14.0</version> </dependency> <!-- TestNG --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.9.0</version> <scope>test</scope> </dependency> <!-- Playwright Java --> <dependency> <groupId>com.microsoft.playwright</groupId> <artifactId>playwright</artifactId> <version>1.44.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.2.5</version> <configuration> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </build> </project>

This configuration connects all tools together.


2. Feature File (Title.feature)

Location:

src/test/resources/features/Title.feature

Feature: Check title Scenario: Open login page and check title Given I launch the application Then I should see the page title as "Example Domain"

This describes expected behavior in simple English.


3. PlaywrightFactory.java

This utility class manages:

  • Playwright object

  • Browser instance

  • Page instance

  • Initialization

  • Cleanup

package utils; import com.microsoft.playwright.*; public class PlaywrightFactory { public static Playwright playwright; public static Browser browser; public static Page page; public static void init() { playwright = Playwright.create(); browser = playwright.chromium().launch( new BrowserType.LaunchOptions().setHeadless(false) ); page = browser.newPage(); } public static void close() { browser.close(); playwright.close(); } }

Why use a factory class?

  • Avoid duplicate code

  • Centralize browser management

  • Improve maintainability


4. Step Definition Class (LoginSteps.java)

This class connects feature steps to Playwright actions.

package steps; import com.microsoft.playwright.Page; import io.cucumber.java.After; import io.cucumber.java.Before; import io.cucumber.java.en.*; import utils.PlaywrightFactory; import static org.testng.Assert.assertEquals; public class LoginSteps { Page page; @Before public void setup() { PlaywrightFactory.init(); page = PlaywrightFactory.page; } @Given("I launch the application") public void i_launch_the_application() { page.navigate("https://example.com"); } @Then("I should see the page title as {string}") public void i_should_see_the_page_title_as(String expectedTitle) { String actualTitle = page.title(); assertEquals(actualTitle, expectedTitle); } @After public void tearDown() { PlaywrightFactory.close(); } }

Execution flow:

  • @Before initializes browser

  • Given step navigates to site

  • Then step validates title

  • @After closes browser


5. TestRunner.java (Cucumber + TestNG Runner)

package runners; import io.cucumber.testng.AbstractTestNGCucumberTests; import io.cucumber.testng.CucumberOptions; import org.testng.annotations.DataProvider; @CucumberOptions( features = "src/test/resources/features", glue = "steps", plugin = {"pretty", "html:target/cucumber-reports.html"}, monochrome = true ) public class TestRunner extends AbstractTestNGCucumberTests { @Override @DataProvider(parallel = true) public Object[][] scenarios() { return super.scenarios(); } }

Important points:

  • features → Location of feature files

  • glue → Location of step definitions

  • plugin → Reporting configuration

  • DataProvider(parallel = true) → Enables parallel execution


6. testng.xml (Optional Suite Execution)

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

This file allows suite-level control and execution.


Advantages of This Integration

1. Readable Test Scenarios

Cucumber makes scenarios easy to understand.

2. Powerful Browser Automation

Playwright ensures fast and reliable execution.

3. Parallel Execution

TestNG allows parallel execution for faster test cycles.

4. Scalability

This framework works well for:

  • Large enterprise projects

  • Continuous Integration pipelines

  • Multi-browser testing

5. Flexible Reporting

Both:

  • BDD reports

  • Technical execution reports

are generated.


Best Practices for Enterprise Projects

  • Use Page Object Model (POM)

  • Avoid hardcoded URLs

  • Store configurations in properties file

  • Use proper logging

  • Implement retry logic

  • Integrate with CI/CD tools


Conclusion

Integrating Cucumber, Playwright, and TestNG in Java creates a powerful, scalable, and maintainable automation framework.

Cucumber provides readability.
Playwright provides modern browser automation.
TestNG provides execution control and reporting.

This combination is ideal for:

  • Automation testers

  • SDET engineers

  • QA leads

  • Enterprise automation frameworks

If you are building a real-world automation project, this integration is highly recommended.