本文档概述了将 Jest + Puppeteer 测试迁移到 Playwright 的典型流程,强调迁移过程也是重构或重写测试部分内容的好机会。建议在开始迁移前阅读最佳实践指南。
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );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.
packages/e2e-tests/specs, rename .test.js into .spec.js and put it in the same folder structure inside test/e2e/specs.@wordpress/e2e-test-utils-playwright:js<br />
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );describe, beforeAll, beforeEach, afterEach and afterAll with the test. prefix. For instance, describe turns into test.describe.page and browser.e2e-test-utils. Instead, use the fixtures API to directly get the admin, editor, pageUtils and requestUtils. (However, admin, editor and pageUtils are not allowed in beforeAll and afterAll, rewrite them using requestUtils instead.)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.
e2e-test-utils and paste it in the admin, editor, page or request folder in e2e-test-utils-playwright depending on the type of util.page and browser variables are available in admin, editor and pageUtils as this.page and this.browser.this and bound to the same instance. You can remove any import statements and use this to access them.index.ts and put it inside the Admin/Editor/PageUtils/RequestUtils class as an instance field.