@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' },
})
参数
| 参数 | 类型 | 需求 | 默认 |
|---|---|---|---|
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 文件中扩展它 .
// 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',
})
环境变量
| 变量 | 用于 | 说明 |
|---|---|---|
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 |