@wordpress/e2e-test-utils-playwright provides end-to-end test utilities for WordPress using Playwright. It offers fixtures and helpers for testing WordPress admin, the block editor, and WordPress functionality.
Installation
Install the package as a dev dependency:- Node.js: >= 18.12.0
- npm: >= 8.19.2
- WordPress: >= 5.6.0 or Gutenberg: >= 9.2.0
Quick Start
1
Install dependencies
2
Create a test file
tests/example.spec.js
3
Run tests
Core API
test
Extended Playwright test module with WordPress-specific fixtures.admin- Admin utilities for WordPress admin interfaceeditor- Block editor utilitiespageUtils- Generic page interaction utilitiesrequestUtils- REST API utilitiespage- Standard Playwright page object
expect
Playwright/Jest’s expect function for assertions.Fixtures
Admin
Utilities for WordPress admin interface.Admin Methods
Admin Methods
Editor
Utilities for the WordPress block editor.Editor Methods
Editor Methods
PageUtils
Generic utilities for page interactions.PageUtils Methods
PageUtils Methods
pressKeys(keys)
Press keyboard shortcuts:primary- Ctrl (Windows/Linux), Cmd (Mac)shiftaltctrl
RequestUtils
Utilities for WordPress REST API interactions.1
Set up RequestUtils
2
Use in tests
RequestUtils Methods
RequestUtils Methods
Common Test Patterns
Testing Block Insertion
Testing Admin Pages
Testing with API Setup
Using Custom Editor Setup
Accessing Canvas Elements
The editor uses an iframe, so you must useeditor.canvas to access elements inside:
Configuration
Playwright Config
Create aplaywright.config.js file:
playwright.config.js
Test Environment Setup
Set up WordPress environment variables:playwright.config.js
Failed Test Artifacts
When tests fail, artifacts are saved to help with debugging.Default Location
Artifacts are stored in theartifacts/ directory by default.
Custom Location
Set a custom artifacts directory:Artifact Types
- Screenshots - captured when tests fail
- Videos - recorded test execution
- Traces - detailed execution traces
Viewing Artifacts
Advanced Usage
Custom Fixtures
Extend the test fixtures:Multiple WordPress Instances
Test against different WordPress versions:playwright.config.js
Performance Testing
Measure page performance:Running Tests
Basic Commands
With wp-scripts
If using@wordpress/scripts:
package.json
Terminal
Best Practices
Use beforeEach for common setup
Use beforeEach for common setup
Clean up with afterEach/afterAll
Clean up with afterEach/afterAll
Use data-testid for reliable selectors
Use data-testid for reliable selectors
Wait for elements properly
Wait for elements properly
Use API for setup when possible
Use API for setup when possible
Troubleshooting
Elements not found in editor
Elements not found in editor
Issue: Can’t find blocks or editor elementsSolution: Use
editor.canvas to access iframe content:Flaky tests
Flaky tests
Issue: Tests pass/fail intermittentlySolutions:
- Use
toBeVisible()instead of checking existence - Increase timeout for slow operations
- Wait for network idle before assertions
Authentication issues
Authentication issues
Issue: Can’t access admin pagesSolution: Verify credentials in RequestUtils setup:
Timeout errors
Timeout errors
Issue: Tests timeout waiting for elementsSolution: Increase timeout in config:
playwright.config.js
Examples Repository
Find more examples in the Gutenberg repository.Package Information
- Version: 1.40.0
- Node.js: >= 18.12.0
- npm: >= 8.19.2
- License: GPL-2.0-or-later
- Peer Dependencies:
@playwright/test>= 1@types/node^20.17.10
Related Packages
- @wordpress/scripts - Build and test scripts
- @wordpress/env - Local WordPress environment
- @playwright/test - Playwright testing framework