Migration guide
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.js
into.spec.js
and put it in the same folder structure insidetest/e2e/specs
. - Require the test helpers from
@wordpress/e2e-test-utils-playwright
:const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );
- Change all occurrences of
describe
,beforeAll
,beforeEach
,afterEach
andafterAll
with thetest.
prefix. For instance,describe
turns intotest.describe
. - Use the fixtures API to require previously global variables like
page
andbrowser
. - Delete all the imports of
e2e-test-utils
. Instead, use the fixtures API to directly get theadmin
,editor
,pageUtils
andrequestUtils
. (However,admin
,editor
andpageUtils
are not allowed inbeforeAll
andafterAll
, rewrite them usingrequestUtils
instead.) - 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.*
).
The 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-utils
and paste it in theadmin
,editor
,page
orrequest
folder ine2e-test-utils-playwright
depending on the type of util. - Update any parts of the util that need to be rewritten:
- The
page
andbrowser
variables are available inadmin
,editor
andpageUtils
asthis.page
andthis.browser
. - All the other utils in the same class are available in
this
and bound to the same instance. You can remove anyimport
statements and usethis
to access them. - Consider updating the util to use TypeScript if you’re comfortable doing so.
- The
- Import the newly migrated util in
index.ts
and put it inside theAdmin
/Editor
/PageUtils
/RequestUtils
class as an instance field.
最終更新日: