Playwright Java helper for Vaadin Flow

I’ve started to create a library to ease the test of a Vaadin application with Playwright.
The focus has been on each component to help how to assert or get information without knowing how they are structured internally.
For example if you want to test if a textfield is invalid and has an error message you can do this:

// get the textfield by accessible name
TextFieldElement textfield = TextFieldElement.getByLabel(page, "Validated Textfield");
textfield.setValue("1");
textfield.assertInvalid();
textfield.assertErrorMessage("Minimum length is 6 characters");

The API is different than TestBench since the project is relying on Playwright assertions and is trying to follow Playwright’s best practices: Best Practices | Playwright.
So, internally, to get the error message of the textfield I will build a errorMessageLocator then use Playwright assertions to run the tests.

The library has not been published yet in the directory and if you have any feedback on the API that would definitely help.

There is also the mopo library, we will very likely merge the 2 projects since it doesn’t make a lot of sense to have 2 Playwright wrapper for Vaadin.

In theory, it’s not Flow related but since it’s written in Java I guess a Hilla user will write the tests in Typescript. I don’t think there is an easy way to combine it in one library (make a playwright Typescript/Java library, but all the suggestions are welcomed).

The source code can be found here: GitHub - parttio/dramafinder
There is a TODO list here: dramafinder/TODO.md at master · parttio/dramafinder · GitHub

I will post updates here, once it’s published.

4 Likes

Amazing! That’s a great idea and well be very helpful as I’m using Playwright a lot

The helper library has been released in Maven Central and in the directory: Drama Finder - Vaadin Add-on Directory

It’s very likely buggy but I’ll update the library and the tests :slight_smile:

Components implemented

  • Big Decimal Field
  • Button
  • Checkbox
  • Date Picker
  • Date Time Picker
  • Details
  • Dialog
  • Email Field
  • Integer Field
  • ListBox (multiple and single)
  • Notification
  • Password Field
  • Popover
  • Progress bar
  • Radio Button
  • Select
  • Tabs
  • TextArea
  • TextField
  • Time Picker
2 Likes

I’ve released a new version 1.1.0 with the combobox, grid, treegrid…

I generated the documentation with mintlify : https://parttio-dramafinder-4.mintlify.app/

It’s probably easier to read than the javadoc :slight_smile:

It still need a lot of work but feedbacks are welcomed.

3 Likes

That’s is awesome!

Thanks for the great work @Jean-Christophe.1

1 Like

I just realized that the elements have getById. That’s something that is not recommended and also Playwright doesn’t have that.

I would highly recommend top follow the API of Playwright and provide:

then as Mopo user it would be great to have all the methods from the Mopo classes to make a transition from Mopo to Dramafinder easy.

For example:

I haven’t tried to migrate from Mopo.
But for the grid you should replace new GridPw(gridLocator) to new GridElement(gridLocator)

The grid is quite specific since there is no label, test, placeholder, alt text, title and most of the components should use the accessibleName.

I’m not sure what would be the recommended way for a Grid (In my opinion we should have an accessible name for our Grid).

Currently I would probably use the GridElement get(Page page) = get the first grid in the page or GridElement get(Locator parent) = get the first grid in this part of the page.

But yes getById is not great.

1 Like

@Jean-Christophe.1
I’ve migrated the first project to Dramafinder. Works like a charm!
Thank you :rocket:

3 Likes

A couple of ways to implement getById:

  1. Override the testId attribute. This is what I use. See Locators | Playwright Java
Playwright playwright = Playwright.create();
playwright.selectors().setTestIdAttribute("id");
//Then use the Page.getByTestId(String id) method.
  1. Use the css selector for attr-value pairs.
page.locator("[id=\"theId\"]");

My point was that using IDs is not recommended.

I have always wondered how people test internationalization when the code is based on getByLabel and getByPlaceholder :->

Playwright is recommending to make tests on something visual/meaningful.

Of course of your application is in multiple languages you should know in which language your tests is running.
I can also understand that you don’t want to change your tests if only a label has been updated.

Dramafinder is built based on this recommendation, but it’s fairly easy to use a id or testid.

1 Like

I’m Swiss and all apps are in there languages. We use labels for selection with parametrized tests because this way we also test the translations

1 Like

Fine and good when your development and test teams are separate.

But I have spent many hours trying to figure out why my playwright code isn’t finding that “visual/meaningful” element. I’d bet anyone working in this area has the same experience. And that playwright “pick locator” is often useless when working with vaadin windows.

If the same developer/team is building both the application and the playwright test code, using IDs can save a lot of hassle. It is the least fragile option.

We’re not going to solve best practices here. Just useful to share experiences.

When it comes to IDs it’s recommended to use data-testid.

There will be direct support on the component in the upcoming 25.1:

@SimonMartinelli Thanks I didn’t know

I’m curious, is the issue with windows something you’ve experienced also with Vaadin 25? Given that Dialogs and other overlays now use the browser-native popover feature, searching for contents inside could become easier; I haven’t had the time to test it yet myself.

I’m waiting to find out, but unfortunately stuck on a showstopper bug, see CssImport with themeFor does not work in production mode. · Issue #23689 · vaadin/flow · GitHub

Things get challenging, e.g., when you have a Dialog opened from another Dialog and have to restrict the search to the one with the “opened” attribute. In the past, I preferred to use “Locator inside a Locator” paths, but that does not play well with Dialog and Shadow DOM. I will not be surprised if some of my “make it work somehow” Locators break in Vaadin 25. A good thing, since it means better stability in the long run.

I only recently learned about the dramafinder project, and it has some approaches that are better than mine. Better to work on team code, rather than solo stuff, so I will move to that project over time.

Normally the changes in the overlays ( dialog, combo box,…) should make things easier but will break your tests. The content of the overlay is a slot so in light dom.

Dramafinder is quite recent. The first version was our after Vaadin 25.

Feel free to use it and create some bug or feature requests. I’m pretty sure they are flaky test methods or issues. But it’s hopefully much easier than using plain Playwright.

I am looking forward to using it and have “someone to share the burden with”.

The project looks very nice, although possibly lacking the inevitable dirty-hacks required for overcoming the brutal reality of UI testing:

For example, I used to rely on vaadin-menu-bar-item isEnabled() and then I couldn’t.

And today I might be discovering that vaadin-confirm-dialog between version 24 and 25 no longer responds “correctly/intuitively” to isVisible() and I might have to switch my code to one of isExists()/isEnabled()/attribute(“opened”). But only half a day lost to that, so only suspicions and not able to confirm. C’est la guerre.