@aleph-alpha/config-playwright

中文文档
1.0.1发布于 2026/08/10Apache-2.0TypeScriptESM + CJS零依赖近 30 天下载 1.5万

Aleph Alpha 前端项目共享 Playwright 配置 。 提供可扩展配置的工厂,基础Page对象模型类,以及认证的测试固定.

中文使用文档
文档目录9 节
  1. @aleph-alpha/ 配置游戏作家
  2. 什么v1舰
  3. 安装
  4. 使用量
  5. mountPlaywrightConfig
  6. BasePage
  7. authenticatedTest
  8. .gitignore 添加数
  9. 从v. 4.0移出

@aleph-alpha/ 配置游戏作家

Aleph Alpha 前端项目共享 Playwright 配置 。 提供可扩展配置的工厂,基础Page对象模型类,以及认证的测试固定.


什么v1舰

导出类型目的
mountPlaywrightConfig函数回返a PlaywrightTestConfig 显示对象 defineConfig
BasePage类页面对象模型的基础类
authenticatedTest剧作家固定滴入为 test 在需要登录的测试中
expect再出口剧作家 expect - 随同导入 authenticatedTest 从一个地方

安装

@playwright/test 是一个对等依赖, 您必须同时安装它 :

pnpm add -D @aleph-alpha/config-playwright @playwright/test

使用量

mountPlaywrightConfig

// playwright.config.ts
import { defineConfig } from '@playwright/test'
import { mountPlaywrightConfig } from '@aleph-alpha/config-playwright'

export default defineConfig({
  ...mountPlaywrightConfig({ baseURL: process.env['BASE_URL'] ?? 'http://localhost:4200' }),
  // Add any project-specific options after spreading:
  // webServer: { command: 'pnpm start', url: 'http://localhost:4200' },
})

参数

参数类型需求默认
baseURLstring对—— 说
ciboolean没有!!process.env['CI']

ci 从中自动检测到 CI 环境变量。 明确通过它只是为了覆盖检测(例如用 CI 语义在本地运行的脚本).

返回配置值

外地当地(ci=false(中文(简体) )CI (中文(简体) )ci=true(中文(简体) )
testDire2ee2e
timeout60 000 毫秒(60 000 毫秒(
retries02
workersundefined (玩法默认)1
forbidOnlyfalsetrue
use.storageState.auth/state.json.auth/state.json
use.traceon-first-retryon-first-retry
use.videoretain-on-failureretain-on-failure
use.screenshotonly-on-failureonly-on-failure
reporter[['html'], ['json', ...]][['html'], ['json', ...]]
projects铬+火花铬+火花

JSON记者写信给 test-results/results.json。 。 。 .


BasePage

页面对象模型模式的基础类. 在您的 POM 文件中扩展它 .

// e2e/pages/login.page.ts
import { BasePage } from '@aleph-alpha/config-playwright'
import type { Page } from '@playwright/test'

export class LoginPage extends BasePage {
  constructor(page: Page) {
    super(page)
  }

  async login(user: string, password: string) {
    await this.navigateTo('/login')
    await this.page.fill('[data-testid="username"]', user)
    await this.page.fill('[data-testid="password"]', password)
    await this.page.click('[type="submit"]')
    await this.waitForLoad()
  }
}

方法

方法签署说明
navigateTo(path: string) => Promise<void>电话 page.goto(path)
waitForLoad() => Promise<void>等着 networkidle 装入状态

this.page 暴露为 protected 用于子类.


authenticatedTest

Playwright 的倒置替换 test 那个预装 .auth/state.json 作为 storageState。 。 。 .

// e2e/dashboard.spec.ts
import { authenticatedTest, expect } from '@aleph-alpha/config-playwright'

authenticatedTest('shows dashboard', async ({ page }) => {
  await page.goto('/dashboard')
  await expect(page.getByRole('heading')).toContainText('Dashboard')
})

认证合同

authenticatedTest 读取认证状态 .auth/state.json您必须在您的 globalSetup 在测试之前.

// e2e/global-setup.ts  (wire up in your own playwright.config.ts)
import { chromium } from '@playwright/test'

export default async function globalSetup() {
  const browser = await chromium.launch()
  const page = await browser.newPage()

  await page.goto(process.env['BASE_URL'] + '/login')
  await page.fill('[name="username"]', process.env['E2E_USER']!)
  await page.fill('[name="password"]', process.env['E2E_PASSWORD']!)
  await page.click('[type="submit"]')

  await page.context().storageState({ path: '.auth/state.json' })
  await browser.close()
}

从你的参考 playwright.config.ts数字 :

export default defineConfig({
  ...mountPlaywrightConfig({ baseURL: '...' }),
  globalSetup: './e2e/global-setup',
})

环境变量

变量用于说明
CImountPlaywrightConfig自动启用的 CI 模式( 重复、 禁用、 单工)
BASE_URL您的配置基础 URL 传递到 mountPlaywrightConfig
E2E_USER您的全球设置认证用户名( 此软件包未使用)
E2E_PASSWORD您的全球设置授权密码( 本包未消耗)

.gitignore 添加数

在您的应用程序中添加这些 .gitignore数字 :

.auth/
test-results/
playwright-report/

从v. 4.0移出

页:1单曲
testDir: './src/e2e'testDir: 'e2e'
storageState: '/tmp/storage-state.json'storageState: '.auth/state.json'
globalSetup 设置为不存在的路径globalSetup 未设置 —— 连接到 defineConfig
slowMo: 800 浏览器项目删除
reporter: 'html'reporter: [['html'], ['json', { outputFile: 'test-results/results.json' }]]
ci 所需经费ci 选项 - 默认为 !!process.env['CI']
没有 BasePage 导出BasePage 再出口
没有 authenticatedTest 导出authenticatedTest + 键 expect 再出口
@playwright/test 内 dependencies@playwright/test 现在是一个 peerDependency - 明确加入你的应用软件 devDependencies

版本与发布记录

@aleph-alpha/config-playwright 共发布 140 个版本,首次发布于 2025/03/10;过去 12 个月发布了 80 个版本。

版本发布日期距今
1.0.1latest2026/08/102 个月前
1.0.02026/06/154 个月前
0.4.02026/05/056 个月前
0.3.02025/12/1810 个月前
0.2.1342025/12/1110 个月前
0.2.1322025/12/1110 个月前

其他发布通道:canary → 0.2.9-canary.6a2fa49c.0。

关于 @aleph-alpha/config-playwright 的常见问题

@aleph-alpha/config-playwright 是什么?

Aleph Alpha 前端项目共享 Playwright 配置 。 提供可扩展配置的工厂,基础Page对象模型类,以及认证的测试固定.

如何安装 @aleph-alpha/config-playwright?

在项目目录运行 npm install @aleph-alpha/config-playwright,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@playwright/test(>=1.36.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@aleph-alpha/config-playwright 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aleph-alpha/config-playwright 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@aleph-alpha/config-playwright 可以商用吗?

@aleph-alpha/config-playwright 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@aleph-alpha/config-playwright 的下载量有多少?

根据 npm 官方下载统计,@aleph-alpha/config-playwright 在 2026/10/06 – 2026/11/04 这 30 天内被下载 14,624 次,日均约 487 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aleph-alpha/config-playwright 还在维护吗?

最新版本 1.0.1 发布于 2026/08/10(2 个月前);至今共发布 140 个版本,其中过去 12 个月发布了 80 个;首次发布于 2025/03/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。