Vaadin UI Testing with Playwright


In my Vaadin projects, I usually use Karibu Testing for unit and/or Vaadins TestBench for end-to-end tests.

Karibu Testing is an open source testing framework developed by Martin Vysny. Karibu Testing mocks the Vaadin Servlet environment and is the perfect choice when you want to do test-driven development. It’s very fast because no servlet container and no browser are involved.

TestBench is the commercial UI testing framework based on Selenium. It extends Selenium with Webcomponents and Vaadin-specific functionality. Testing with TestBench is often slow and flaky. I encountered non-reproducible problems when running the test. So I was looking for an alternative. This article explores Playwright, a powerful alternative promising faster execution and broader browser support.

Introducing Playwright

Two years ago, Microsoft launched Playwright. Playwright is available for Node.js, Python, Java, and C# and promises:

– Any browser, any platform, one API
– Resilient, no flaky tests
– No trade-offs, no limits
– Full isolation, fast execution
– Powerful tooling

“No flaky tests” and “fast execution” – sounds exactly what I was looking for!

Getting Started with Playwright

Add Playwright Dependency

To start with Playwright, you have to add a dependency. In my case, I’m using Maven:


Create a Base Test Class

To simplify the writing of the tests, I recommend creating a base class. Before all tests, we create an instance of Playwright and launch the browser. And before each test, a new BrowserContext and a new page are created to isolate the test methods.

public class PlaywrightIT {

    private static Playwright playwright;
    private static Browser browser;
    protected Page page;
    private BrowserContext browserContext;

    static void beforeAll() {
        playwright = Playwright.create();
        BrowserType browserType = playwright.chromium();
        BrowserType.LaunchOptions launchOptions = new BrowserType.LaunchOptions();
        launchOptions.headless = false;
        browser = browserType.launch(launchOptions);

    static void afterAll() {

    void beforeEach() {
        browserContext = browser.newContext();
        page = browserContext.newPage();

    void afterEach() {

Writing Playwright Tests for Vaadin Applications

The application we want to test is an open-source application I’ve created to manage track and field events.
The dashboard shows series and competitions with the rankings.

Navigating and Interacting with Elements

In the first test, we want to navigate to the dashboard. The test must check if the title equals Dashboard and count how many series are displayed. With the method locator of the page instance, we can use CSS selectors to select the elements on the page.

public class DashboardViewIT extends PlaywrightIT {

    void series_are_displayed() {

        Locator viewTitle = page.locator("#view-title");

        Locator seriesLayouts = page.locator(".series-layout");

Easy, isn’t it? So let’s look at a more difficult test.

Handling Login and Protected Pages

The application also has a password-protected part for managing the data of the track and field events. The login screen is displayed when the user tries to access this part.

As with other UI testing frameworks, it’s recommended to create so-called page objects to encapsulate parts of the UI. The LoginPO has a single method that fills in the username and password and clicks on the sign-in button.

public class LoginPO {

    private final Page page;

    public LoginPO(Page page) { = page;

    public void login(String username, String password) {
        page.fill("vaadin-text-field[name='username'] > input", username);
        page.fill("vaadin-password-field[name='password'] > input", password);"vaadin-button");

We use the page object in the next test. In the “before each” method, we navigate to the organization’s page and sign in using the LoginPO. Then we check the title to see if the navigation after the login was successful.

void login() {

    new LoginPO(page).login(System.getenv("JTAF4_TEST_USERNAME"), System.getenv("JTAF4_TEST_PASSWORD"));

    Locator locator = page.locator("#view-title");

After successful login, the protected page is displayed.

Custom Elements for Vaadin Grids

In the test method, we first get the grid and then check if the expected data is contained in the grid.

GridElement organizationsGrid = new GridElement(

organizationsGrid.waitForCellByTextContent("Concours InterSection");
organizationsGrid.waitForCellByTextContent("Turnverein Erlach");

The used GridElement is a custom class to make using the Vaadin Grid webcomponent simpler in tests. For example, the scrollToIndex method executes JavaScript on the Grid object in the DOM.

public class GridElement {

    private final ElementHandle elementHandle;

    public GridElement(ElementHandle elementHandle) {
        this.elementHandle = elementHandle;

    public void scrollToIndex(int index) {
        elementHandle.evaluate("(grid, index) => grid.scrollToIndex(index)", index);

    public ElementHandle waitForCellByTextContent(String textContent) {
        return elementHandle.waitForSelector(String.format("vaadin-grid-cell-content >> '%s'", textContent));

    public ElementHandle getCellByTextContent(String textContent) {
        return elementHandle.querySelector(String.format("vaadin-grid-cell-content >> '%s'", textContent));

After that, we click the Add button and fill some test data into the input fields, and hit Save:



Now we can check if the grid contains the added organization, and finally, we click on the delete button to remove the test data.




Compared to TestBench, Playwright is running much faster. On the other side, TestBench is made for Vaadin therefore selecting and interacting with Vaadin components is easier.

I will continue to write tests with Playwright to gain more experience with it.
Stay tuned for more blogs about Playwright and Vaadin.

Related Posts

Simon Martinelli
Follow Me