Playwright with Junit

  



In Playwright for Javaassertions are used to verify conditions during automated browser testing—such as checking whether a particular element is visible, enabled, contains the right text, etc.

Playwright for Java uses JUnit or TestNG for assertions. You can also use Playwright’s Assertions class directly (available in the com.microsoft.playwright.assertions package).


How Playwright and JUnit Work Together

  • JUnit provides the test structure, while Playwright handles browser automation.
  • Each test method in JUnit can represent a scenario, such as logging into a website, filling a form, or verifying UI behavior.
  • Playwright is integrated inside these test methods to launch browsers, navigate pages, and perform actions.

Benefits of Using Playwright with JUnit

(a) Organized Test Execution: JUnit makes it easy to group tests, run them together, and get structured reports.

(b) Assertions and Validations: JUnit provides built-in assertion methods to validate Playwright’s actions and outputs.

(c) Lifecycle Management: JUnit has @Before and @After style annotations to manage setup (e.g., starting Playwright/browser) and cleanup (closing browser).

(d) Parallel Execution: Both JUnit and Playwright support running tests in parallel, which helps speed up automation suites.

(e) Integration with CI/CD: JUnit results (in XML/HTML format) are easily integrated with tools like Jenkins, GitHub Actions, or Maven for continous testing.











Common Assertion Types in Playwright Java

Here are some common assertions:


Assertion TypeMethod in Java
Element is visibleAssertions.assertThat(locator).isVisible();
Element has textAssertions.assertThat(locator).hasText("value");
Element is enabledAssertions.assertThat(locator).isEnabled();
Element is hiddenAssertions.assertThat(locator).isHidden();
Element contains valueAssertions.assertThat(locator).hasValue("value");
Title is correctpage.title() and assert using Assertions or JUnit



Example: Assert on a Login Page
















import com.microsoft.playwright.*;
import com.microsoft.playwright.assertions.PlaywrightAssertions;
import org.junit.jupiter.api.*;

import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;

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

    @BeforeAll
    static void setup() {
        playwright = Playwright.create();
        browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
        context = browser.newContext();
        page = context.newPage();
    }

    @Test
    void testLoginPageAssertions() {
        // Navigate to login page
        page.navigate("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");

        // Assertion: Page title should contain "OrangeHRM"
        String title = page.title();
        Assertions.assertTrue(title.contains("OrangeHRM"), "Page title does not match");

        // Assertion: Check if username input field is visible
        Locator usernameField = page.locator("input[name='username']");
        assertThat(usernameField).isVisible();

        // Assertion: Check if login button is enabled
        Locator loginButton = page.locator("button[type='submit']");
        assertThat(loginButton).isEnabled();

        // Assertion: Check placeholder in username field
        Assertions.assertEquals("Username", usernameField.getAttribute("placeholder"));
    }

    @AfterAll
    static void tearDown() {
        context.close();
        browser.close();
        playwright.close();
    }
}


Code explanation:


SectionPurpose
Playwright.create()Initializes Playwright engine.
browser.newContext()Creates a fresh browser context (like incognito).
page.navigate()Opens the specified login URL.
LocatorIdentifies elements like input fields and buttons.
assertThat(locator).Playwright’s built-in assertions (visibility, text, etc.).
Assertions.assertEquals or assertTrueJUnit assertions for values like title, attributes.