---
name: tevin-write-e2etest
description: 为项目编写 Playwright E2E 测试脚本的技能。适用于实现组件或功能的端到端测试,避免使用 MCP,改用传统的测试脚本方式。
license: MIT
compatibility: 项目使用 @playwright/test、React、Vite、TypeScript
metadata:
author: tevin
使用 @playwright/test 编写 E2E 测试,不使用 MCP。
测试目录: test/e2e/
路由模式: hash 路由,#/preview/pages/<path>
开发服务器: pnpm dev (http://localhost:5173)
import { test, expect } from '@playwright/test';
test.describe('功能名称', () => {
test.beforeEach(async ({ page }) => {
// 访问包含该功能的页面
await page.goto('http://localhost:5173/#/preview/pages/xxx/Page.tsx');
});
test('测试场景描述', async ({ page }) => {
// 使用 page.waitForSelector 或 expect 等待元素
await expect(page.locator('text=预期文本')).toBeVisible();
});
test('交互功能测试', async ({ page }) => {
// 点击操作
await page.click('text=可点击文本');
// 验证结果
await expect(page.locator('text=结果文本')).toBeVisible();
});
});
http://localhost:5173/#/preview/pages/<component>/<Page>.tsxpnpm dev 访问确认test 块描述一个独立场景test.beforeEach 统一初始化expect 断言而非 console.log 验证pnpm devpnpm exec playwright test test/e2e/<spec>.tstext= 定位文本,避免脆弱的 CSS 选择器expect(locator).toBeVisible() 而非 page.waitForTimeout()should display menu items菜单导航测试:typescript test('菜单点击跳转', async ({ page }) => { await page.click('text=菜单项'); await expect(page.locator('text=目标页面内容')).toBeVisible(); });
表单输入测试:typescript test('表单提交', async ({ page }) => { await page.fill('input[placeholder="输入框"]', '测试值'); await page.click('button:has-text("提交")'); await expect(page.locator('text=成功消息')).toBeVisible(); });
@playwright/mcp 已从项目移除).spec.tspnpm exec playwright install