The Test Team helps manage testing and triage across the WordPress ecosystem. They focus on user testing of the editing experience and WordPress dashboard, replicating and documenting bug reports, and supporting a culture of review and triage across the project.
In the spirit of improving the E2E developer experience, I’d like to make a case for migrating CoreCoreCore is the set of software required to run WordPress. The Core Development Team builds WordPress.’s browser automation library to Playwright. I was asked to write this post after opening an experimental pull request, where I migrated a selected portion of specs to Playwright, making them available for running both locally and on CI. That happened some time ago, so there’s already been quite a bit of discussion going on there! Having said that, I’m going to try making the case again here, taking the feedback I’ve received so far into consideration. I also encourage you to check out the PR to see the implementation details and Playwright advantages in action.
Why drop Puppeteer in favor of Playwright?
It’s easier to write stable tests.
There are a few reasons why. Please read on to find out which ones I think are the most relevant for the project.
The APIAPIAn API or Application Programming Interface is a software intermediary that allows programs to interact with each other and share data in limited, clearly defined ways.
Playwright’s API is almost identical to Puppeteer’s, which means that the developer transition should be close to effortless. I think that’s a big factor here, as it significantly lowers the cost of this transition also from the migrationMigrationMoving the code, database and media files for a website site from one server to another. Most typically done when changing hosting companies. side. A good thing to start with! 🤞
Playwright performs a range of actionability checks on the elements before making actions to ensure these actions behave as expected. It auto-waits for all the relevant checks to pass and only then performs the requested action. (…)
I think it’s the number one reason for the stability improvement over Puppeteer. In practice, it means the following:
No need to perform any additional presence checks
Most of the DOM changes happen asynchronously, so in order to avoid flaky behavior, a test usually explicitly wait for an element before performing an action on it. A good example would be the most frequently used click action, which usually looks like this when performed with Puppeteer:
With Playwright, thanks to the auto-waiting mechanism it becomes just:
await page.click( 'button' )
No need to disable CSSCSSCSS is an acronym for cascading style sheets. This is what controls the design or look and feel of a site. animations
This is thanks to the stability check, which makes sure the element has a bounding box and is not moving before the action is performed. I think this is a major advantage because it allows to fully test the application, including the CSS animations, which are an integral part of the user interface.
In my PR, as a proof of concept, I removed the code that disables CSS animations and the forced reduced motion, which slowed down the refactored tests (21 suites) by around 37 seconds. This number will grow with every test, but judging by the current data it shouldn’t be more than a few minutes in total. I’d say the trade-off would still be worth it, but this can be discussed and decided upon later.
What do you think about testing without animations? Should they be enabled if it’s possible, even for the cost of extra wait?
In general, all of the above comes down to writing less and simpler code to get the same or better results than Puppeteer. If you go to my PR, you’ll notice that there are more lines removed than added in the refactored tests!
With Playwright, the tests and test utilities are easier to write and follow, and the environment requires less customization (e.g. disabled animations) which actually makes it closer to what users are experiencing.
Are there any downsides to the auto-waiting mechanism?
There were some concerns about how this mechanism could affect the performance tests. Because it could potentially become a blocking factor for this migration, I decided to migrate Gutenberg’s performance specs to Playwright as a proof of concept and see what happens. So far, thankfully it looks like there isn’t much difference between Puppeteer and Playwright — the performance metrics are very close, which would be the desired outcome.
Do you think there could be any downsides to the auto-waiting mechanism? Please let me know in the comments!
The Advanced Selectors Support
This part has changed a bit due to some feedback received in the PR. Originally, I mentioned text selectors and layout-based selectors as the number one reason for making the tests and utilities easier to write and follow, as well as making them more resilient. While prioritizing user-facing attributes is still a good practice for most applications, GutenbergGutenbergThe Gutenberg project is the new Editor Interface for WordPress. The editor improves the process and experience of creating new content, making writing rich content much simpler. It uses ‘blocks’ to add richness rather than shortcodes, custom HTML etc. https://wordpress.org/gutenberg/ is a bit different in this regard. Apparently, CSS classes are considered to be the API there, so they change less often than the user interface. Nevertheless, as some folks noticed advanced selectors are still a big win as they’d allow to, e.g. drop the use of cumbersome XPath selectors and more with powerful selector chaining. Currently, with Puppeteer, CSS selectors can only be used.
The built-in Inspector is also a big advantage of Playwright. It’s quite intuitive and has some neat features like stepping through the script while running headfully or dynamically recording actions to a script — a really convenient way of quickly drafting the test. See the video below for a short demo of the script recorder.
Playwright offers a complete tracing solution. Trace can be recorded and stored in a zip file, which then can be viewed via the Trace Viewer GUI:
On the image above, you can see that the trace is displayed in a form of a film strip. Each frame can contain Before, Action, and After snapshots visualizing a complete action execution. On the left-hand side is a list of all the actions Playwright performed. Each of them can be inspected in more detail in the section on the right-hand side, where you can switch between the action log, location in the source code, and the network log.
I think it’s great to have that kind of functionality out of the box. It also shows how Playwright is intended to be a complete testing solution. With Puppeteer, there aren’t really any first-party tools for debugging, as far as I’m aware – The suggested way is to either slow down the tests in headful mode with DevTools open or use the Node.js Debugger when running headlessly.
If there’s a goal to expand testing to browsers other than Chrome, it wouldn’t be an issue for Playwright as it supports all other major players: Firefox, WebKit, and Microsoft Edge. At the time of writing this, Puppeteer supports only Chrome and Chromium, and the official support of Firefox is currently experimental.
Playwright has a first-party test-runner, which is very similar to Jest test-runner (currently used for Puppeteer) but written from scratch. It contains a lot of end-to-end testing utils, tooling, concurrency, reporting, assertions, artifacts, etc., and extensive configuration support. Another quite nice thing to have without having to install and rely on third-party libraries!
I think it deserves a mention here, as it’s easy to navigate and really well written, in my opinion. Please take a few minutes and check it out for yourself at https://playwright.dev/docs/api/class-playwright – maybe you’ll find even more reasons to switch to Playwright? 😉
Writing good, stable E2E tests is often a struggle. If there’s a chance of improving that, especially with such a low cost, then it should be done. I would be happy to work on this task if there’s a consensus to move it forward.
Thanks for reading. I’m looking forward to all the feedback!
End-to-end (E2E) tests allow simulating user flows for an application, a site and validating them. They allow for automated testing of user interactions while providing a safety net for continuous development.
Over the past two years, the groundwork for implementing e2e tests in WordPress CoreCoreCore is the set of software required to run WordPress. The Core Development Team builds WordPress. has been laid. However, there hasn’t been much communication about the technical stack that is used for these tests or the next steps for adding more tests in WordPress.
The purpose of this post is to inform contributors about the current status of e2e testing in WordPress Core, the technologies being used, and what is currently being done to implement these tests.
What stack are we using for WordPress Core e2e tests?
E2e testing in WordPress Core is implemented with Jest as the testing framework and Puppeteer for browser interactions. This stack is relevant for Core use case because it is present in other parts of the CMS codebase.
On the other hand, it is used by GutenbergGutenbergThe Gutenberg project is the new Editor Interface for WordPress. The editor improves the process and experience of creating new content, making writing rich content much simpler. It uses ‘blocks’ to add richness rather than shortcodes, custom HTML etc. https://wordpress.org/gutenberg/ for its tests. So we are not starting from scratch for the implementation in Core, as there is already some preliminary work done with the @wordpress/e2e-tests package and the @wordpress-e2e-utils utility package, which includes several basic flows that can be reused in tests.
What tests are currently implemented in Core?
Currently, only the setup for testing and a basic test of the WordPress Dashboard homepage are implemented in Core here. The @wordpress/e2e-tests-utils utility package (in the Gutenberg repository) contains various tests for installing, disabling, activating themes and plugins, navigating the different admin pages, etc.
What is the Test Team working on?
There are currently efforts being made to implement more e2e tests in WordPress Core. Starting with this ticket, opened by @isabel_brison that presents an overview of the different tests that will be implemented.
There are also pull requests, and branches from contributors who have already implemented tests for some parts of WordPress:
You must be logged in to post a comment.