playwright-core

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Playwright Core Testing

Playwright核心测试

Opinionated, production-tested Playwright guidance — every pattern includes when (and when not) to use it.
46 reference guides covering the full Playwright testing surface: selectors, assertions, fixtures, network mocking, auth, visual regression, accessibility, API testing, debugging, and more — with TypeScript and JavaScript examples throughout.
具有明确实践导向、经生产环境验证的Playwright指南——每种模式都说明了适用场景(以及不适用的场景)。
46份参考指南覆盖Playwright测试的全场景:选择器、断言、测试夹具、网络模拟、认证、可视化回归测试、无障碍测试、API测试、调试等——全程包含TypeScript和JavaScript示例。

Golden Rules

黄金准则

  1. getByRole()
    over CSS/XPath
    — resilient to markup changes, mirrors how users see the page
  2. Never
    page.waitForTimeout()
    — use
    expect(locator).toBeVisible()
    or
    page.waitForURL()
  3. Web-first assertions
    expect(locator)
    auto-retries;
    expect(await locator.textContent())
    does not
  4. Isolate every test — no shared state, no execution-order dependencies
  5. baseURL
    in config
    — zero hardcoded URLs in tests
  6. Retries:
    2
    in CI,
    0
    locally
    — surface flakiness where it matters
  7. Traces:
    'on-first-retry'
    — rich debugging artifacts without CI slowdown
  8. Fixtures over globals — share state via
    test.extend()
    , not module-level variables
  9. One behavior per test — multiple related
    expect()
    calls are fine
  10. Mock external services only — never mock your own app; mock third-party APIs, payment gateways, email
  1. 优先使用
    getByRole()
    而非CSS/XPath
    —— 不受标记变更影响,与用户查看页面的方式一致
  2. 绝对不要使用
    page.waitForTimeout()
    —— 改用
    expect(locator).toBeVisible()
    page.waitForURL()
  3. 优先使用Web原生断言 ——
    expect(locator)
    会自动重试;
    expect(await locator.textContent())
    则不会
  4. 每个测试保持独立 —— 不共享状态,无执行顺序依赖
  5. 在配置中设置
    baseURL
    —— 测试中绝对不要硬编码URL
  6. 重试次数:CI环境设为2,本地设为0 —— 在关键环境暴露不稳定测试问题
  7. 追踪记录:设为
    'on-first-retry'
    —— 生成丰富的调试产物,同时避免拖慢CI速度
  8. 优先使用测试夹具而非全局变量 —— 通过
    test.extend()
    共享状态,而非模块级变量
  9. 每个测试对应一个行为 —— 允许包含多个相关的
    expect()
    调用
  10. 仅模拟外部服务 —— 绝不模拟自身应用;仅模拟第三方API、支付网关、邮件服务等

Guide Index

指南索引

Writing Tests

编写测试

What you're doingGuideDeep dive
Choosing selectorslocators.mdlocator-strategy.md
Assertions & waitingassertions-and-waiting.md
Organizing test suitestest-organization.mdtest-architecture.md
Playwright configconfiguration.md
Fixtures & hooksfixtures-and-hooks.md
Test datatest-data-management.md
Auth & loginauthentication.mdauth-flows.md
API testing (REST/GraphQL)api-testing.md
Visual regressionvisual-regression.md
Accessibilityaccessibility.md
Mobile & responsivemobile-and-responsive.md
Component testingcomponent-testing.md
Network mockingnetwork-mocking.mdwhen-to-mock.md
Forms & validationforms-and-validation.md
File uploads/downloadsfile-operations.mdfile-upload-download.md
Error & edge caseserror-and-edge-cases.md
CRUD flowscrud-testing.md
Drag and dropdrag-and-drop.md
Search & filter UIsearch-and-filter.md
操作场景指南深度解析
选择选择器locators.mdlocator-strategy.md
断言与等待assertions-and-waiting.md
组织测试套件test-organization.mdtest-architecture.md
Playwright配置configuration.md
测试夹具与钩子fixtures-and-hooks.md
测试数据test-data-management.md
认证与登录authentication.mdauth-flows.md
API测试(REST/GraphQL)api-testing.md
可视化回归测试visual-regression.md
无障碍测试accessibility.md
移动端与响应式测试mobile-and-responsive.md
组件测试component-testing.md
网络模拟network-mocking.mdwhen-to-mock.md
表单与验证forms-and-validation.md
文件上传/下载file-operations.mdfile-upload-download.md
错误与边界用例error-and-edge-cases.md
CRUD流程测试crud-testing.md
拖拽测试drag-and-drop.md
搜索与筛选UI测试search-and-filter.md

Debugging & Fixing

调试与修复

ProblemGuide
General debugging workflowdebugging.md
Specific error messageerror-index.md
Flaky / intermittent testsflaky-tests.md
Common beginner mistakescommon-pitfalls.md
问题场景指南
通用调试流程debugging.md
特定错误信息error-index.md
不稳定/间歇性失败测试flaky-tests.md
常见新手误区common-pitfalls.md

Framework Recipes

框架实践方案

FrameworkGuide
Next.js (App Router + Pages Router)nextjs.md
React (CRA, Vite)react.md
Vue 3 / Nuxtvue.md
Angularangular.md
框架指南
Next.js(App Router + Pages Router)nextjs.md
React(CRA、Vite)react.md
Vue 3 / Nuxtvue.md
Angularangular.md

Specialized Topics

专项主题

TopicGuide
Multi-user & collaborationmulti-user-and-collaboration.md
WebSockets & real-timewebsockets-and-realtime.md
Browser APIs (geo, clipboard, permissions)browser-apis.md
iframes & Shadow DOMiframes-and-shadow-dom.md
Canvas & WebGLcanvas-and-webgl.md
Service workers & PWAservice-workers-and-pwa.md
Electron appselectron-testing.md
Browser extensionsbrowser-extensions.md
Security testingsecurity-testing.md
Performance & benchmarksperformance-testing.md
i18n & localizationi18n-and-localization.md
Multi-tab & popupsmulti-context-and-popups.md
Clock & time mockingclock-and-time-mocking.md
Third-party integrationsthird-party-integrations.md
主题指南
多用户与协作测试multi-user-and-collaboration.md
WebSockets与实时功能测试websockets-and-realtime.md
浏览器API(地理定位、剪贴板、权限)browser-apis.md
内嵌框架(iframes)与Shadow DOMiframes-and-shadow-dom.md
Canvas与WebGL测试canvas-and-webgl.md
服务工作线程与PWA测试service-workers-and-pwa.md
Electron应用测试electron-testing.md
浏览器扩展测试browser-extensions.md
安全测试security-testing.md
性能与基准测试performance-testing.md
国际化(i18n)与本地化i18n-and-localization.md
多标签页与弹窗测试multi-context-and-popups.md
时钟与时间模拟clock-and-time-mocking.md
第三方集成测试third-party-integrations.md

Architecture Decisions

架构决策

QuestionGuide
Which locator strategy?locator-strategy.md
E2E vs component vs API?test-architecture.md
Mock vs real services?when-to-mock.md
问题指南
选择哪种定位器策略?locator-strategy.md
端到端测试 vs 组件测试 vs API测试?test-architecture.md
模拟服务还是使用真实服务?when-to-mock.md