End-to-End Testing with Playwright
- Set Up Playwright
- First Playwright Test
- Vaadin Components with DramaFinder
- Selecting Components with Plain Playwright
- Running the Test
- More about Playwright
Playwright 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 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.
Set Up Playwright
Add the Playwright dependency to your project’s pom.xml file:
Source code
pom.xml
pom.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.
First Playwright Test
The test on this page uses the following view:
Source code
MainView.java
MainView.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/:
Source code
MainViewTest.java
MainViewTest.javapackage 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
}
}-
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.
-
End-to-end tests are slower than unit tests. A JUnit tag lets you include or exclude them.
-
Injects the port that the web server started on.
-
Launches a headless Chromium once for the test class. Closing the
Playwrightinstance also closes the browser. -
Opens a new page for each test, so that the tests don’t share browser state.
-
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.
-
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 in the Playwright documentation.
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 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:
Source code
pom.xml
pom.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:
Source code
OrderView.java
OrderView.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:
Source code
OrderViewTest.java
OrderViewTest.java@Test
void orderShowsConfirmation() {
ComboBoxElement.getByLabel(page, "Country").selectItem("Germany");
DatePickerElement.getByLabel(page, "Delivery date")
.setValue(LocalDate.of(2030, 1, 15));
ButtonElement.getByText(page, "Order").click();
NotificationElement
.getByText(page, "Delivery to Germany on 2030-01-15")
.assertOpen();
}DramaFinder has elements for most Vaadin components, and base classes that replace the setup code shown above. See the DramaFinder documentation.
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.
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:
Source code
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 for the full list of slot selectors.
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:
Source code
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.
Running the Test
Run the test from your IDE, or with Maven:
Source code
terminal
mvn testStarting the server and the browser takes a few seconds. To run only the faster tests, exclude the tests that have the playwright tag:
Source code
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.