Playwright测试框架

一个基于TypeScript的Playwright测试框架,提供页面对象模型和API测试规范,适用于Web应用和API的自动化测试。

  • by dubilyer
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 dubilyer/pl_mcp

使用说明

剧作家测试框架(TypeScript)

该项目包含用于web应用程序测试的页面对象模型(POM)类和使用带有TypeScript的Playwright的API规范文件,遵循更新的mcp_instr.md指南。

项目结构

pl_mcp/
├── pom/                          # Page Object Model files
│   ├── BasePage.ts              # Base class with ARIA-based locators
│   └── ContactFormPage.ts       # Contact form with user-facing locators
├── api/                          # API specification files
│   └── DigiDatesApi.ts          # DigiDates API with response logging
├── tests/                        # Test files
│   ├── api/digidates-api.spec.ts     # API test with logging
│   └── web/skipper-contact-form.spec.ts # Web test with ARIA locators
├── playwright.config.ts         # TypeScript Playwright configuration
├── tsconfig.json                # TypeScript config with path mapping
├── package.json                 # Dependencies with TypeScript & Node types
└── mcp_instr.md                # Updated testing guidelines

先决条件

  • Node.js (版本16或更高)
  • npm 或 纱线 包管理器
  • TypeScript (包含在开发依赖项中)

安装

npm install

更新测试指南合规性

该项目遵循严格的更新规则 mcp_instr.md:

✅ 全球测试规则:

  • 规则1: ✅ 使用有意义的辅助函数在15个语句下进行测试
  • 规则3: ✅ 扁平结构-无if/else、循环、try-catch、console.log测试
  • 规则5: ✅ 吸烟枪原理——预先定义测试数据
  • 规则7: ✅ 带有利益相关者/行动/期望的描述性测试标题
  • 规则10: ✅ 仅基于ARIA的定位器(getByRole、getByText)
  • 规则15: ✅ 无显式等待-已删除waitForTimeout

✅ Web测试规则:

  • 规则10: ✅ 面向用户的ARIA定位器(page.getByRole('button', {name: 'Submit'}))
  • 规则9: ✅ 无位置选择器(无 .first(), .nth())
  • 规则8: ✅ 使用以下命令自动重试断言 await expect()

✅ API测试规则:

  • 规则5: ✅ 响应体日志记录 console.log(JSON.stringify(responseBody))
  • 响应验证: ✅ 全面的模式验证

✅ 编码实践:

  • 实践2: ✅ 没有console.log测试(仅在API规范中用于日志记录)
  • 实践3: ✅ 硬编码值的常量
  • 实践6: ✅ TypeScript接口和类型
  • 实践9: ✅ 导入路径短(@/api/DigiDatesApi)

运行测试

# Run DigiDates API tests
npm run test:digidates

# Run Skipper contact form web tests  
npm run test:contact

# Run all tests
npm run test:all

API测试

DigiDates API测试

文件: tests/api/digidates-api.spec.ts
测试:“用户可以从API检索unix时间戳、验证闰年和计算年龄”

特性:

  • ✅ 响应日志记录:记录到控制台的所有API响应
  • ✅ 确凿的证据:预先定义为常量的预期值
  • ✅ 全面验证:Unix时间戳、闰年逻辑、年龄计算
  • ✅ 类型安全:测试数据和年龄数据接口
  • ✅ 短期进口:用途 @/api/DigiDatesApi

Web测试

船长联系表测试

文件: tests/web/skipper-contact-form.spec.ts 测试:“用户可以提交联系表并收到确认或预期的reCaptcha响应”

特性:

  • ✅ ARIA定位器: getByRole('textbox', {name: /name/i}), getByRole('button', {name: /submit/i})
  • ✅ 无位置选择器:已删除 .first(), .nth() 用法
  • ✅ 无明确等待:已删除 waitForTimeout() 电话
  • ✅ 辅助函数:有意义的方法,如 fillContactFormWithTestData()
  • ✅ 常量:TEST_DATA定义为常量对象
  • ✅ 短期进口:用途 @/pom/ContactFormPage

关键改进

✅ 完全符合ARIA:所有定位器都使用带有可访问性名称的getByRole、getByText
✅ 响应正文记录:记录API响应以进行调试
✅ 无基于时间的等待:删除了waitForTimeout以提高可靠性
✅ TypeScript路径映射:短导入路径(@/api/,@/pom/)
✅ 吸烟枪原理:预先定义的所有预期值
✅ 辅助函数:有意义地封装多步操作
✅ 类型安全:全面的TypeScript接口和类型
✅ 常量用法:测试中没有硬编码值

MCP集成

该项目旨在与Playwright MCP服务器配合使用,以便在与兼容的MCP客户端配合使用时实现高级自动化功能。