Showing posts with label Playwright with TestNG. Show all posts
Showing posts with label Playwright with TestNG. Show all posts

Playwright with TestNG Integration




Understanding the Roles

Playwright: Provides APIs to interact with browsers, automate web applications, handle UI elements, and perform end-to-end testing.

TestNG: Provides a testing framework with annotations, test execution flow, grouping, parameterization, parallel execution, and reporting.


Why Integrate Playwright with TestNG?

  • To run Playwright tests in a structured test framework.
  • To leverage TestNG’s annotations like @BeforeSuite, @BeforeTest, @Test, @AfterTest, etc., which make setup and teardown of Playwright objects systematic.
  • To achieve parallel test execution across browsers or test cases using TestNG’s parallel execution support.
  • To generate detailed reports (HTML/XML) of test execution.
  • To allow data-driven testing with TestNG’s parameterization features.

Integration Flow

(1) Dependencies: Add required dependencies (Playwright for browser                          automation, TestNG for test framework).

(2) Base Setup: 
  • Create a base test class where Playwright components (Browser,       BrowserContext, Page) are initialized and cleaned up.
  • Use TestNG lifecycle annotations (@BeforeClass, @AfterClass) for setup/teardown.
(3) Test Structure:
  • Each TestNG test method (@Test) contains the actual Playwright steps (like navigating, clicking, assertions).
  • Tests can be grouped, prioritized, or parameterized through TestNG features.
(4) Parallel Execution:
  • TestNG XML configuration (testng.xml) can be used to define suites, parallel execution, and parameters.
  • This enables multiple browsers or test cases to run in parallel using Playwright’s independent BrowserContext.
(5) Reports & Logs:
  • TestNG automatically generates reports showing passed/failed/skipped test cases.
  • Logs can be captured to debug Playwright test failures.

Benefits
  • Separation of Concerns: Playwright handles automation, TestNG manages test orchestration.
  • Scalability: Easy to scale test execution by combining TestNG parallelism with Playwright’s multiple browser instances.
  • Maintainability: Test code is organized into test classes and methods, improving readability and reusability.
  • Integration: Can be easily plugged into CI/CD pipelines since both TestNG and Playwright are well-supported with Maven/Gradle and Jenkins.


To assert in Playwright with Java using TestNG, we need to:

  • Set up a Playwright project with Java.
  • Use TestNG as the testing framework.
  • Write assertions using TestNG's Assert class.
  • Use Playwright's Locator API to find elements and retrieve text/attributes for validation.














Website to be automated:

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










Step-by-step Explanation

1. Maven Dependencies

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



2. Example: Playwright + TestNG Assertion Code

We'll test the title and login button visibility on OrangeHRM login page. below is the LoginPageTest class implemented by Playwright java and TestNG. 

@BeforeClass annotation is used on setUp() to setup things like initialization of browser and launching the url.

@Test annotation is used below to check the title in first test case and submit button is visible or not, in second test case.

@AfterClass annotation is used to close browser and playwright objects.


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() {
        // Assertion using TestNG: Check if title contains "OrangeHRM"
        String title = page.title();
        Assert.assertTrue(title.contains("OrangeHRM"), "Title does not contain 'OrangeHRM'");
    }

    @Test
    public void testLoginButtonVisible() {
        // Check if login button is visible
        Locator loginButton = page.locator("button[type='submit']");
        Assert.assertTrue(loginButton.isVisible(), "Login button is not visible");
    }

    @Test
    public void testLoginButtonText() {
        // Check login button text
        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 We are Asserting:

AssertionWhat it Validates
Assert.assertTrue(condition)Validates a condition is true
Assert.assertEquals(actual, expected)Compares actual vs expected values
loginButton.isVisible()Checks if the button is rendered and visible
title.contains("OrangeHRM")Checks if title contains expected text



Useful Playwright Methods for Assertion

Playwright MethodUse
page.title()Get page title
locator.textContent()Get inner text of element
locator.isVisible()Check if element is visible
locator.getAttribute("attr")Get attribute value



At a Glance:
  • Use TestNG's Assert class for assertions
  • Fetch values using Playwright (title, text, attributes).
  • Structure tests with @Test@BeforeClass@AfterClass.
  • Combine the power of Playwright automation and TestNG assertions for full UI validation.


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

 



Integrating CucumberPlaywright, and TestNG in Java enables you to write BDD (Behavior Driven Development) style tests using Gherkin (.feature files), automate browser interactions using Playwright, and manage execution using TestNG.


Role of Each Component

Playwright:

A powerful automation framework that interacts with modern browsers (Chromium, Firefox, WebKit). It is mainly responsible for performing browser-based actions like navigating pages, clicking elements, validating results, handling alerts, and more. It provides the engine for execution of test steps.

Cucumber:

A BDD (Behavior-Driven Development) framework that allows writing test scenarios in Gherkin language (Given-When-Then format). Its purpose is to make test cases readable and understandable for both technical and non-technical stakeholders. It provides the structure for test scenarios and bridges the gap between business requirements and automated test scripts.

TestNG:

A test management and execution framework in Java. It provides advanced features like test configuration, grouping, parallel execution, annotations, data-driven testing, and detailed reporting. It manages how and when tests are executed, integrates with build tools (Maven/Gradle), and generates structured results.


How Integration Works

Step 1: Cucumber defines test scenarios

Business or QA teams write scenarios in Gherkin. Example:

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

Step 2: Step Definitions in Java call Playwright

Each step in the feature file is mapped to a step definition (Java method). Inside these step definitions, Playwright APIs are used to perform browser actions like opening a page, entering data, or verifying results.

Step 3: TestNG executes scenarios

TestNG acts as the test runner. It executes Cucumber feature files (via Cucumber runner classes) and manages the lifecycle of the tests. TestNG annotations help configure test execution order, parallel runs, retries, or grouping of test suites.

Step 4: Unified Reporting and Logs

TestNG generates test execution reports (HTML/XML), while Cucumber generates BDD reports. These can also be integrated into advanced reporting dashboards like Allure or Extent Reports. This way, both the technical (developer/QA) and business teams can consume the results in formats they understand.


Benefits of Integration

Readable Scenarios: Cucumber ensures tests are described in plain English, improving collaboration.

Powerful Browser Automation: Playwright provides modern, fast, and cross-browser execution.

Robust Test Management: TestNG handles execution strategies (parallelism, suites, groups).

Scalability: The combination is suited for enterprise-level projects where test readability, execution speed, and configurability are crucial.

Flexible Reporting: Both business-friendly BDD reports and developer-focused TestNG reports are available.



Tools Used:

  • Cucumber for BDD feature files and step definitions

  • Playwright for Java for browser automation

  • TestNG for test execution and reporting

  • Maven for dependency management


Website to be automated: https://example.com










Below are the steps showing integration of Cucumber, Playwright and TestNG



pom.xml:

<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>
      <!-- Surefire for TestNG -->
      <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>

Title.feature (Gherkin feature file)

Below is the feature file having feature as 'Check title' and Scenario as 'Open login page and check title'. Steps in feature file are implemented by gherkins keywords and expected title of page is 'Example Domain'

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"


PlaywrightFactory.java

PlaywrightFactory is a utility class which consists of Playwright, Browser and Page objects, it also contain init() to initialize browser and close() to close browser and Playwright object.


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();
    }
}


LoginSteps.java

LoginSteps is a step definition class, which is implimentation of Title.feature file. In this, all feature file steps are implemented by annotations like @Given, @Then, etc. which are gherkin keywords of respective steps in feature file. @Before and @After is used for initialization and tear down work respectively.


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();
    }
}


TestRunner.java (Cucumber TestNG runner)

TestRunner class is used to execute tests written in feature files and implemented in step definition files. Here path of feature file is given by 'features' keyword and path of step definition by 'glue' keyword. All these things are implemented under @CucumberOptions annotation. This TestRunner class is implemented by Cucumber and TestNG.


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();
    }
}


testng.xml

TestNG.xml is optional file which contains TestRunner class path represented in class tag. We can execute TestRunner class by TestNG.xml also.

<!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>


Suggested Posts:

1. Playwright with TestNG Integration
2. Automate Login Page in Playwright
3. Comma Selectors in Playwright
4. Handle Alerts in Playwright
5. How to Show Visible Elements in Playwright