> Markdown version of [E2E Testing with Playwright](https://vaadin.com/docs/next/flow/testing/playwright). Section index: [llms.txt](https://vaadin.com/docs/next/flow/llms.txt)

# End-to-End Testing with Playwright

[Playwright](https://playwright.dev/java/) is an open source end-to-end testing library developed by Microsoft. It works well with Vaadin applications. Its Java API runs your tests in Chromium, Firefox, and WebKit, and its locators find Vaadin components by their role, label, and text, including the parts inside shadow DOM.

Playwright doesn’t track when the Vaadin server has processed a request. Instead, its actions and assertions retry until the page reaches the expected state or a timeout passes, which covers the server round-trips of a Vaadin application in most tests. For UI components with a more complex structure, such as `ComboBox`, `DatePicker`, and `Grid`, the [DramaFinder](#dramafinder) library adds a typed Java API on top of Playwright.

This page describes how to set up Playwright for a Spring Boot-based Vaadin application and how to write a test. In other kinds of applications, only starting and stopping the server differs. For example, you can use the `pre-integration-test` and `post-integration-test` phases, as shown in [Getting Started with TestBench](https://vaadin.com/docs/next/flow/testing/end-to-end/getting-started.md).

## <a id="set-up-playwright"></a>Set Up Playwright

Add the Playwright dependency to your project’s `pom.xml` file:

`pom.xml`

```xml
<dependency>
    <groupId>com.microsoft.playwright</groupId>
    <artifactId>playwright</artifactId>
    <version>1.65.0</version> <!-- Check the latest version at https://playwright.dev/java/ -->
    <scope>test</scope>
</dependency>
```

The test on this page also uses `spring-boot-starter-test` to start the application. Add it in the `test` scope unless your project already has it.

Playwright downloads the browsers it needs the first time a test runs.

## <a id="first-playwright-test"></a>First Playwright Test

The test on this page uses the following view:

`MainView.java`

```java
@Route
public class MainView extends VerticalLayout {

    public MainView() {
        var msg = new Paragraph();
        // Add an id to make the element easy to find in a test
        msg.setId("msg");

        var button = new Button("Click me", e -> msg.setText("Clicked!"));

        add(button, msg);
    }
}
```

Add a test class for the view to your test directory, for example `src/test/java/org/example/`:

`MainViewTest.java`

```java
package org.example;

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

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.server.LocalServerPort;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.options.AriaRole;

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) // (1)
@Tag("playwright") // (2)
class MainViewTest {

    @LocalServerPort // (3)
    int port;

    static Playwright playwright;
    static Browser browser;
    Page page;

    @BeforeAll
    static void launchBrowser() { // (4)
        playwright = Playwright.create();
        browser = playwright.chromium().launch();
    }

    @AfterAll
    static void closeBrowser() {
        playwright.close();
    }

    @BeforeEach
    void openView() { // (5)
        page = browser.newPage();
        page.navigate("http://localhost:" + port + "/");
    }

    @AfterEach
    void closePage() {
        page.close();
    }

    @Test
    void clickingButtonShowsMessage() {
        page.getByRole(AriaRole.BUTTON,
                new Page.GetByRoleOptions().setName("Click me")).click(); // (6)

        assertThat(page.locator("#msg")).hasText("Clicked!"); // (7)
    }
}
```

1. Starts the application with a web server on a random port before the tests run. A random port lets the tests run while a development server is running.

2. End-to-end tests are slower than unit tests. A JUnit tag lets you include or exclude them.

3. Injects the port that the web server started on.

4. Launches a headless Chromium once for the test class. Closing the `Playwright` instance also closes the browser.

5. Opens a new page for each test, so that the tests don’t share browser state.

6. Finds the button the way a user does, by its role and text, and clicks it. Playwright waits for the button to appear, so the test doesn’t need to wait for the view to load.

7. Playwright’s assertions retry until they pass or time out, which gives the server round-trip time to complete. Reading the text with `textContent()` and asserting it with JUnit doesn’t wait, and can fail if the response hasn’t arrived yet.

Prefer the locators that match what a user sees, such as `getByRole()`, `getByLabel()`, and `getByText()`. They also find elements inside the shadow DOM of Vaadin components. See [Locators](https://playwright.dev/java/docs/locators) in the Playwright documentation.

## <a id="dramafinder"></a>Vaadin Components with DramaFinder

Some Vaadin components take several steps to operate with plain Playwright. Selecting an item in a `ComboBox`, for example, means opening the overlay, finding the item, and clicking it. [DramaFinder](https://github.com/parttio/dramafinder) is an open source library that wraps Vaadin components in typed Java classes for Playwright. Its elements find a component by its label or text, operate it in one call, and wait for the server round-trip. DramaFinder is maintained by the Vaadin community.

Add the dependency next to Playwright:

`pom.xml`

```xml
<dependency>
    <groupId>org.vaadin.addons</groupId>
    <artifactId>dramafinder</artifactId>
    <version>2.1.0</version> <!-- Check the latest version at https://github.com/parttio/dramafinder -->
    <scope>test</scope>
</dependency>
```

The elements only need a Playwright `Page`, so they work in a test class set up as above. The following view has a combo box and a date picker:

`OrderView.java`

```java
@Route("order")
public class OrderView extends VerticalLayout {

    public OrderView() {
        var country = new ComboBox<String>("Country");
        country.setItems("Finland", "Germany", "Sweden");

        var delivery = new DatePicker("Delivery date");

        var order = new Button("Order", e -> Notification.show("Delivery to "
                + country.getValue() + " on " + delivery.getValue()));

        add(country, delivery, order);
    }
}
```

A test that opens the `order` route fills in the fields and checks the notification:

`OrderViewTest.java`

```java
import static org.vaadin.addons.dramafinder.element.query.ElementQueries.find;

@Test
void orderShowsConfirmation() {
    find(page, ComboBoxElement.class).withLabel("Country").single()
            .selectItem("Germany");
    find(page, DatePickerElement.class).withLabel("Delivery date").single()
            .setValue(LocalDate.of(2030, 1, 15));
    find(page, ButtonElement.class).withText("Order").single().click();

    find(page, NotificationElement.class)
            .withText("Delivery to Germany on 2030-01-15").single()
            .assertOpen();
}
```

The `find()` method finds a UI component by its type and a filter, such as its label or text. To search inside another UI component, call `find()` on its element, for example `dialog.find(ButtonElement.class)`.

DramaFinder has elements for most Vaadin components, and base classes that replace the setup code shown above. See the [DramaFinder documentation](https://parttio.github.io/dramafinder/).

## <a id="selecting-components-with-plain-playwright"></a>Selecting Components with Plain Playwright

Vaadin components are Web Components, so part of their structure lives in shadow DOM and part in slotted light DOM. Without a helper library, a couple of cases trip up hand-written selectors.

### <a id="dialog-action-buttons-are-slotted"></a>Dialog Action Buttons Are Slotted

A \[classname]\`ConfirmDialog’s confirm, cancel, and reject buttons are **slotted light-DOM children**, not part of its shadow DOM. Target the slotted button directly:

```java
page.locator("vaadin-confirm-dialog vaadin-button[slot='confirm-button']").click();
// also: [slot='cancel-button'] and [slot='reject-button']
```

A visibility check based on `offsetParent` skips slotted content and misses these buttons, and synthesizing a `click()` on a guessed wrapper element is unreliable — especially when a confirm dialog is stacked over another overlay, such as an editor, where you need the real button. See [Confirm Dialog styling](https://vaadin.com/docs/next/components/confirm-dialog/styling.md) for the full list of slot selectors.

### <a id="grid-cell-content-is-recycled"></a>Grid Cell Content Is Recycled

`Grid` renders each cell’s content into a `vaadin-grid-cell-content` element and **reuses** these elements as rows scroll or the grid refreshes. Detached copies of previously rendered content can linger in the document, so a broad query such as `document.querySelectorAll('vaadin-grid-cell-content')` may report rows that the user no longer sees.

Read grid state from the **live** cells only — scope the query to the grid and let Playwright match attached, visible elements — rather than enumerating every cell-content element in the document:

```java
// Matches only attached, visible cell content within the grid
Locator cells = page.locator("vaadin-grid vaadin-grid-cell-content:visible");
```

Alternatively, wait for the refresh to settle (for example, with an explicit `waitFor()`) before asserting, or reload the page to clear detached content.

## <a id="running-the-test"></a>Running the Test

Run the test from your IDE, or with Maven:

```terminal
mvn test
```

Starting the server and the browser takes a few seconds. To run only the faster tests, exclude the tests that have the `playwright` tag:

```terminal
mvn test -DexcludedGroups="playwright"
```

With the commercial TestBench load testing plugin, you can also reuse Playwright tests as load test scenarios, as described in [Load Testing With Playwright](https://vaadin.com/docs/next/flow/testing/load-testing/playwright.md).

## <a id="more-about-playwright"></a>More about Playwright

- [Official Playwright Java documentation](https://playwright.dev/java/)

- [DramaFinder documentation](https://parttio.github.io/dramafinder/)

- [Vaadin UI Testing with Playwright](https://martinelli.ch/ui-testing-with-vaadin-and-playwright/)

`A8496E86-4D72-11EE-BE56-0242AC120002`
