Skip to content

使用 Azure Playwright 实现大规模云端浏览器测试

解决本地测试资源受限的问题:利用 Azure 托管的浏览器集群,让 Playwright 测试能够快速扩展到数十个并发 Worker,并直接将结果同步至 Azure 门户进行分析。

为什么需要这个技能

在进行端到端(E2E)测试时,本地运行大量测试用例会导致 CPU 和内存迅速耗尽,导致测试速度缓慢且不稳定(Flaky tests)。

通过集成 Azure Playwright 工作区,开发者可以将浏览器运行环境迁移到云端。这意味着你不再需要管理复杂的本地浏览器依赖,可以通过简单的配置启动数十个并行 Worker 运行测试,并利用 Azure 门户的集成报告功能快速定位失败原因,极大地缩短了 CI/CD 的反馈周期。

适用场景

  • 需要在短时间内运行数千个 E2E 测试用例的规模化场景。
  • 希望在统一的云端环境下消除“我本地能跑通,但在 CI 上失败”的环境差异。
  • 需要将测试报告直接集成到 Azure 企业级管理门户中。
  • 在 GitHub Actions 或 Azure Pipelines 中构建高性能的自动化测试流水线。

核心工作流

1. 安装与环境配置

推荐使用自动生成配置的初始化命令:

bash
npm init @azure/playwright@latest

并配置云端服务 URL 环境变量:

bash
PLAYWRIGHT_SERVICE_URL=wss://eastus.api.playwright.microsoft.com/playwrightworkspaces/{workspace-id}/browsers

2. 构建服务配置文件

创建 playwright.service.config.ts,通过 createAzurePlaywrightConfig 将本地配置与云端环境关联,并指定认证方式(推荐使用 Entra ID):

typescript
import { defineConfig } from "@playwright/test";
import { createAzurePlaywrightConfig, ServiceOS } from "@azure/playwright";
import { DefaultAzureCredential } from "@azure/identity";
import config from "./playwright.config";

export default defineConfig(
  config,
  createAzurePlaywrightConfig(config, {
    os: ServiceOS.LINUX,
    credential: new DefaultAzureCredential(),
  })
);

3. 执行大规模测试

使用特定的配置文件并增加 --workers 参数来提升并行度:

bash
npx playwright test --config=playwright.service.config.ts --workers=20

4. 集成 Azure 报告

在配置中添加 @azure/playwright/reporter,即可将测试结果自动上传至 Azure 门户。

下载和安装

下载 azure-microsoft-playwright-testing-ts 中文版 Skill ZIP

解压后将目录放入你的 AI 工具 skills 文件夹,重启工具后即可使用。具体路径参考内附的 USAGE.zh.md

你可能还需要

暂无推荐