Web与API自动化测试框架

基于Playwright的Web和API自动化测试框架,支持页面对象模型、API请求构建与验证、测试数据管理及Allure/HTML报告生成。

  • by Jeeva0506
  • JavaScript
  • 本地部署
  • 更新于
在 GitHub 查看 Jeeva0506/Playwright_MCP_Automation_Framework

使用说明

剧作家自动化框架

概述

该框架支持使用Playwright实现强大的Web和API自动化,具有MCP-风格的迭代、诱惑报告、严格的代码质量和模块化ES6结构。它包括:

  • Web UI测试(POM结构)
  • API测试(请求生成器、响应验证器、断言)
  • 测试数据管理
  • Allure/HTML报告
  • MCP式重试

先决条件

  • Node.js>=18
  • npm>=9

安装

  1. 克隆存储库:
    git clone <your-repo-url>
    cd Playwright
    
  2. 安装依赖项:
    npm install
    
  3. 安装Playwright浏览器:
    npx playwright install
    

项目结构

├── src/
│   ├── main/pages/         # Page Objects for UI
│   ├── api/                # API request builders & validators
│   └── utils/              # Assertion utilities
├── tests/
│   ├── api/                # API test specs
│   └── ...                 # UI test specs
├── test/
│   └── api/test_data.json  # API test data
├── .env                    # Environment variables
├── playwright.config.js    # Playwright config (projects for UI & API)
├── package.json            # Scripts & dependencies
├── README.md               # This file

环境设置

  1. 配置 .env 对于基本URL和凭据:
    BASE_URL=https://www.amazon.in/
    PETSTORE_URL=https://petstore.swagger.io/v2
    AMAZON_USER=
    AMAZON_PASS=
    
  2. 确保 .env 在...里 .gitignore 为了安全。

运行测试

Web UI测试

  • 运行所有UI测试(Chromium):
    npm run test:chromium
    
  • 在其他浏览器中运行:
    npm run test:firefox
    npm run test:webkit
    

API测试

  • 运行所有API测试:
    npm run test:api
    
  • MCP风格迭代(最多3次尝试):
    npm run test:api:mcp
    

代码质量

  • 棉绒和自动修复:
    npm run lint
    npm run lint:fix
    
  • 格式和检查:
    npm run format
    npm run format:check
    

报告

  • 生成诱惑报告:
    npm run allure:generate
    
  • Open Allure报告:
    npm run allure:open
    
  • 打开Playwright HTML报告:
    npm run show-report
    

MCP(模型上下文协议)执行

  • MCP-style API测试执行通过 test:api:mcp 脚本,它将自动重试失败的API测试多达3次。
  • 每次运行后都会生成Allure和HTML报告。

扩展框架

  • 在下添加新页面对象 src/main/pages/ 对于UI。
  • 在下添加新的API请求生成器/验证器 src/api/.
  • 在下添加新的测试规范 tests/.
  • 将测试数据添加到 test/api/test_data.json.

调试和日志

  • 断言类和验证器为调试生成有用的日志。
  • Allure和Playwright报告提供了详细的运行历史。

许可证

麻省理工学院