SuperAPI AI 工具集

playwright-test-architecture

测试与质量

查看项目

更新于 10-16 02:00

技能介绍

用途

Playwright Test Architecture 用于帮助搭建 Playwright 测试项目的配置与结构,重点在于测试组织、项目配置与执行优化,目标是构建可扩展、可维护的 Playwright 测试套件。适用于设置 Playwright 测试项目、组织测试套件并规划配置与项目结构的场景。

安装与更新

安装 Playwright 及浏览器:

# Install Playwright with browsers
npm init playwright@latest

# Install specific browsers
npx playwright install chromium firefox webkit

# Install dependencies only
npm install -D @playwright/test

更新 Playwright:

npm install -D @playwright/test@latest
npx playwright install

查看已安装版本:

npx playwright --version

主要能力

项目配置

提供基础配置与高级配置示例。基础配置包括 testDir、timeout、expect.timeout、fullyParallel、forbidOnly、retries、reporter、use(baseURL、trace、screenshot、video)以及 projects 与 webServer。

高级配置进一步涵盖 testMatch、testIgnore、globalSetup、globalTeardown、expect 中 toHaveScreenshot 的 maxDiffPixels 与 toMatchSnapshot 的 threshold、workers、maxFailures、outputDir、preserveOutput、多报告器(html、json、junit、list)、更细的 use 选项(headless、viewport、ignoreHTTPSErrors、bypassCSP、actionTimeout、navigationTimeout、locale、timezoneId、permissions、geolocation、colorScheme、contextOptions.recordVideo)以及多项目配置。项目中可定义 setup 项目并通过 dependencies 让其他项目依赖它,覆盖桌面与移动浏览器(Desktop Chrome、Desktop Firefox、Desktop Safari、Pixel 5、iPhone 13)、品牌浏览器(Microsoft Edge、Google Chrome)以及自定义 viewport(如 tablet)。webServer 可配置 command、url、reuseExistingServer、timeout、stdout、stderr。

测试组织模式

提供按功能、按页面、按用户旅程以及推荐混合方式等多种目录结构。混合方式示例:e2e(端到端用户旅程)、features(功能测试,含 auth、products、profile 子目录)、integration(API 与集成测试)、visual(视觉回归测试)。

测试文件结构

展示基础与进阶测试结构,包括 test.describe、test.beforeAll、test.beforeEach、test.afterEach、test.afterAll,以及嵌套 describe 分组;失败时可附加截图等调试信息。

并行执行与分片

通过 test.describe.configure 设置 mode 为 parallel 或 serial;支持命令式分片:

npx playwright test --shard=1/4
npx playwright test --shard=2/4
npx playwright test --shard=3/4
npx playwright test --shard=4/4

并提供 GitHub Actions matrix 分片示例。worker 可配置为 undefined(使用全部 CPU)、指定数量、百分比或按 CI 区分。

重试策略

支持配置级重试(全局、按条件、按项目)、测试级控制(test.fixme 跳过、test.slow 三倍超时)、describe 级 retries 配置。

报告器

内置报告器包括 list、line、dot、html、json、junit、github,可组合使用并配置输出路径。还提供自定义报告器示例,实现 onBegin、onTestBegin、onTestEnd、onEnd 等钩子。

多环境配置

通过 ENV 变量选择 local、staging、production 等环境,集中管理 baseURL、apiURL、timeout 等;也展示环境文件方式。

录制能力

Trace 支持 on-first-retry、on、retain-on-failure、off,以及带 screenshots 与 snapshots 的详细模式。视频支持 on-first-retry、retain-on-failure、on、off 及自定义尺寸。截图支持 only-on-failure、on、off。

全局 setup 与 teardown

全局 setup 可执行登录并保存 storageState(auth.json);全局 teardown 可清理认证状态与测试数据。

测试分组与标签

可在单个测试或 describe 上打标签(如 @smoke、@critical、@regression),并通过 --grep 按标签运行。

使用方式

按资料所示方式安装依赖与浏览器,编写 playwright.config.ts 进行项目配置,按推荐的目录结构组织测试文件,结合并行、分片、重试、报告器、多环境与录制选项运行与调试测试;必要时使用全局 setup/teardown 与标签筛选。

适用场景

适合需要为 Web 项目搭建 Playwright 端到端测试体系、统一配置多浏览器与多设备项目、在 CI 中并行分片执行、进行视觉回归与集成测试、以及需要跨环境(本地、预发、生产)管理测试配置的团队。

限制

资料未说明该技能的适用版本限制、性能指标、收费与许可信息,暂未提供。