This document outlines a typical flow of migrating a Jest + Puppeteer test to Playwright. Note that the migration process is also a good opportunity to refactor or rewrite parts of the tests. Please read the best practices guide before starting the migration.
Migration steps for tests
- Choose a test suite to migrate in
packages/e2e-tests/specs, rename.test.jsinto.spec.jsand put it in the same folder structure insidetest/e2e/specs. - Require the test helpers from
@wordpress/e2e-test-utils-playwright:
js
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); - Change all occurrences of
describe,beforeAll,beforeEach,afterEachandafterAllwith thetest.prefix. For instance,describeturns intotest.describe. - Use the fixtures API to require previously global variables like
pageandbrowser. - Delete all the imports of
e2e-test-utils. Instead, use the fixtures API to directly get theadmin,editor,pageUtilsandrequestUtils. (However,admin,editorandpageUtilsare not allowed inbeforeAllandafterAll, rewrite them usingrequestUtilsinstead.) - If there’s a missing util, try to inline the operations directly in the test if there are only a few steps. If you think it deserves to be implemented as a test util, then follow the guide below.
- Manually migrate other details in the tests following the proposed best practices. Note that even though the differences in the API of Playwright and Puppeteer are similar, some manual changes are still required.
Migration steps for test utils
Before migrating a test utility function, think twice about whether it’s necessary. Playwright offers a lot of readable and powerful APIs which make a lot of the utils obsolete. Try implementing the same thing inline directly in the test first. Only follow the below guide if that doesn’t work for you. Some examples of utils that deserve to be implemented in the e2e-test-utils-playwright package include complex browser APIs (like pageUtils.dragFiles and pageUtils.pressKeys) and APIs that set states (requestUtils.*).
e2e-test-utils-playwright package is not meant to be a drop-in replacement of the Jest + Puppeteer’s e2e-test-utils package. Some utils are only created to ease the migration process, but they are not necessarily required.
Playwright utilities are organized a little differently from those in the e2e-test-utils package. The e2e-test-utils-playwright package has the following folders that utils are divided up into:
– admin – Utilities related to WordPress admin or WordPress admin’s user interface (e.g. visitAdminPage).
– editor – Utilities for the block editor (e.g. clickBlockToolbarButton).
– pageUtils – General utilities for interacting with the browser (e.g. pressKeys).
– requestUtils – Utilities for making REST API requests (e.g. activatePlugin). These utilities are used for setup and teardown of tests.
- Copy the existing file in
e2e-test-utilsand paste it in theadmin,editor,pageorrequestfolder ine2e-test-utils-playwrightdepending on the type of util. - Update any parts of the util that need to be rewritten:
- The
pageandbrowservariables are available inadmin,editorandpageUtilsasthis.pageandthis.browser. - All the other utils in the same class are available in
thisand bound to the same instance. You can remove anyimportstatements and usethisto access them. - Consider updating the util to use TypeScript if you’re comfortable doing so.
- The
- Import the newly migrated util in
index.tsand put it inside theAdmin/Editor/PageUtils/RequestUtilsclass as an instance field.