# @aleph-alpha/config-playwright 中文文档

规范地址：https://npmcn.com/package/%40aleph-alpha/config-playwright/

版本：1\.0\.1
npm 元数据更新时间：2026-10-28T11:38:11.097Z
中文译文更新时间：2026-11-05T21:58:10.957Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40aleph-alpha/config-playwright
原文对照：https://npmcn.com/package/%40aleph-alpha/config-playwright/?lang=original
项目主页：https://docs.aleph-alpha.com/

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：130 kB，23 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- peer 依赖：@playwright/test \>=1\.36\.0
- 首次发布：2025-03-10T15:01:01.758Z
- 最新版本发布：2026-08-10T14:16:57.593Z
- 近 30 天下载量：14624（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：140（近 12 个月 80 个）

## 中文简介

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

## 安装

```sh
npm install @aleph-alpha/config-playwright
```

也可以使用：`pnpm add @aleph-alpha/config-playwright`、`yarn add @aleph-alpha/config-playwright`、`bun add @aleph-alpha/config-playwright`

## README

## @aleph-alpha/ 配置游戏作家

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

* * *

## 什么v1舰

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

* * *

## 安装

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

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

* * *

## 使用量

### `mountPlaywrightConfig`

```ts
// 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' },
})
```

#### 参数

| 参数 | 类型 | 需求 | 默认 |
| --- | --- | --- | --- |
| `baseURL` | `string` | 对 | —— 说 |
| `ci` | `boolean` | 没有 | `!!process.env['CI']` |

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

#### 返回配置值

| 外地 | 当地(`ci=false`(中文(简体) ) | CI (中文(简体) )`ci=true`(中文(简体) ) |
| --- | --- | --- |
| `testDir` | `e2e` | `e2e` |
| `timeout` | 60 000 毫秒( | 60 000 毫秒( |
| `retries` | `0` | `2` |
| `workers` | `undefined` (玩法默认) | `1` |
| `forbidOnly` | `false` | `true` |
| `use.storageState` | `.auth/state.json` | `.auth/state.json` |
| `use.trace` | `on-first-retry` | `on-first-retry` |
| `use.video` | `retain-on-failure` | `retain-on-failure` |
| `use.screenshot` | `only-on-failure` | `only-on-failure` |
| `reporter` | `[['html'], ['json', ...]]` | `[['html'], ['json', ...]]` |
| `projects` | 铬+火花 | 铬+火花 |

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

* * *

### `BasePage`

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

```ts
// 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`。 。 。 .

```ts
// 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` 在测试之前.

```ts
// 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`数字 :

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

#### 环境变量

| 变量 | 用于 | 说明 |
| --- | --- | --- |
| `CI` | `mountPlaywrightConfig` | 自动启用的 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 是什么？

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 和提交记录判断。

