块编辑器开发文档

自动化测试

💡 云策文档标注

概述

本文档解释了为什么 Gutenberg 项目选择 Playwright 作为端到端测试框架,并概述了其关键优势。Playwright 提供了多浏览器支持、内置测试运行器等功能,以提升测试效率和可靠性。

关键要点

  • Playwright 支持 Chromium、Firefox 和 WebKit,提供比 Puppeteer 更广泛的浏览器覆盖。
  • 内置测试运行器 @playwright/test 支持并行执行、fixtures 和自动等待,减少测试不稳定性。
  • 自动等待和 web-first 断言减少手动 waitFor* 调用,同时暴露影响用户的性能问题。
  • 提供更好的调试工具,如 trace viewer、UI 模式和内置检查器。
  • 使用 fixtures 替代全局变量,便于处理多页面或标签页以及并行测试。
  • 鼓励使用 Page Object Model 模式,提高测试的可读性和可维护性。

📄 原文内容

Why is Playwright the tool of choice for end-to-end tests?

There exists a rich ecosystem of tooling available for web-based end-to-end automated testing. Gutenberg uses Playwright as its end-to-end testing framework. The project previously used Puppeteer but has since fully migrated to Playwright. Here are the reasons Playwright was chosen:

  • Multi-browser support. Playwright supports Chromium, Firefox, and WebKit out of the box, providing broader browser coverage compared to Puppeteer’s Chrome-only approach.
  • Built-in test runner. @playwright/test provides a powerful test runner with parallel execution, fixtures, and auto-waiting, reducing flakiness and improving developer experience.
  • Auto-waiting and web-first assertions. Playwright automatically waits for elements to be actionable before performing actions, reducing the need for manual waitFor* calls while still surfacing legitimate performance issues that affect users.
  • Better debugging tools. Playwright offers a trace viewer, UI mode, and a built-in inspector for step-by-step debugging.
  • Fixtures over global variables. Playwright’s fixture model injects page, browser, and other parameters into tests, making it easier to work with multiple pages or tabs and to run tests in parallel.
  • Page Object Model. Playwright encourages the Page Object Model pattern for reusable utility functions, improving test readability and maintainability.

For more details on writing end-to-end tests, see the End-to-End Testing guide.