电商测试自动化框架

一个基于Playwright、Python和pytest-bdd的全面测试自动化框架,支持行为驱动开发(BDD)和页面对象模型(POM),适用于电商平台的自动化测试。

  • by mamidisrisaiteja
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 mamidisrisaiteja/ecommerceportal_14aug

使用说明

电子商务测试自动化框架

一个全面的测试自动化框架 剧作家, python, pytest bdd,以及增强的报告功能。该框架实现了 页面对象模型 设计模式和支持 行为驱动开发(BDD) 使用Gherkin语法。

🚀 特性

  • 剧作家整合:跨浏览器测试支持(Chromium、Firefox、WebKit)
  • BDD支持:带有pytest-bdd的小黄瓜特征文件
  • 页面对象模型:可维护和可扩展的测试架构
  • 增强的报告功能:带有截图、视频和痕迹的诱惑报告
  • Excel集成:使用MCP Excel服务器进行测试数据管理
  • 并行执行:支持并行测试执行
  • 多环境支持:可配置的测试环境
  • 跨平台:适用于Windows、macOS和Linux

📁 项目结构

ECommercePortal_14Aug/
├── framework/                    # Core framework components
│   ├── __init__.py
│   ├── config.py                # Configuration management
│   ├── pages/                   # Page Object Model classes
│   │   ├── __init__.py
│   │   ├── base_page.py        # Base page class
│   │   ├── login_page.py       # Login page objects
│   │   ├── inventory_page.py   # Inventory page objects
│   │   └── cart_page.py        # Cart page objects
│   └── utils/                  # Utility modules
│       ├── __init__.py
│       ├── browser_manager.py  # Browser lifecycle management
│       ├── data_manager.py     # Excel data handling
│       └── report_manager.py   # Enhanced reporting
├── tests/                      # Test files
│   ├── conftest.py            # Pytest configuration
│   ├── features/              # Gherkin feature files
│   │   ├── authentication.feature
│   │   ├── inventory.feature
│   │   └── cart.feature
│   ├── step_definitions/      # BDD step definitions
│   │   ├── test_authentication_steps.py
│   │   ├── test_inventory_steps.py
│   │   └── test_cart_steps.py
│   ├── test_authentication.py # Authentication tests
│   ├── test_inventory.py     # Inventory tests
│   └── test_cart.py          # Cart tests
├── TestData/                  # Test data files
│   └── TestCaseDocument.xlsx # Excel test case document
├── reports/                   # Generated reports
│   ├── allure-results/       # Allure results
│   ├── screenshots/          # Test screenshots
│   └── videos/              # Test videos
├── config.yaml              # Configuration file
├── requirements.txt         # Python dependencies
├── pyproject.toml          # Poetry configuration
├── run_tests.py            # Test execution script
└── run_tests.bat           # Windows batch script

🛠️ 安装

先决条件

  • Python 3.8+
  • Node.js 14+(适用于Playwright浏览器)
  • Git

安装说明

  1. 克隆存储库:

    git clone https://github.com/mamidisrisaiteja/ECommercePortal_14Aug.git
    cd ECommercePortal_14Aug
    
  2. 创建虚拟环境:

    python -m venv venv
    
    # Windows
    venv\Scripts\activate
    
    # macOS/Linux
    source venv/bin/activate
    
  3. 安装依赖项:

    pip install -r requirements.txt
    
  4. 安装Playwright浏览器:

    playwright install
    
  5. 配置环境:

    cp .env.example .env
    # Edit .env file with your configuration
    

🧪 运行测试

快速开始

# Run all tests
python run_tests.py

# Run specific test suite
pytest tests/test_authentication.py -v

# Run with specific browser
pytest --browser=firefox -v

# Run in parallel
pytest -n 4 -v

# Run with Allure reporting
pytest --alluredir=reports/allure-results
allure serve reports/allure-results

BDD功能测试

# Run authentication features
pytest tests/step_definitions/test_authentication_steps.py::test_valid_login -v

# Run all BDD tests
pytest tests/step_definitions/ -v

测试配置

编辑 config.yaml 要自定义测试设置,请执行以下操作:

base_url: "https://www.saucedemo.com"
timeout: 30000
headless: false
browser: "chromium"
screenshots_dir: "reports/screenshots"
videos_dir: "reports/videos"

📊 测试报告

诱惑报告

使用以下内容生成全面的HTML报告:

# Generate and serve Allure report
pytest --alluredir=reports/allure-results
allure serve reports/allure-results

特征:

  • 测试执行时间表
  • 故障截图
  • 分步执行细节
  • 测试指标和趋势
  • 环境信息

HTML报告

# Generate simple HTML report
pytest --html=reports/report.html --self-contained-html

🏗️ 框架架构

页面对象模型

该框架实现了页面对象模型模式:

  • 基础页面:所有页面的通用功能
  • 登录页面:登录页面特定元素和操作
  • 库存页面:产品列表页面操作
  • 购物车页面:购物车功能

BDD实施

使用pytest-bdd进行行为驱动开发:

Feature: User Authentication
  Scenario: Valid user login
    Given I am on the login page
    When I enter valid credentials
    Then I should be redirected to the inventory page

配置管理

使用以下方式进行集中配置:

  • YAML配置文件
  • 环境变量
  • 命令行参数

📋 测试用例

该框架包括以下测试用例:

身份验证(TC_AUTH_01、TC_AUTH_ 02)

  • 有效的用户登录
  • 无效凭据处理
  • 错误消息验证

库存管理(TC_INV_01、TC_INV_02)

  • 产品列表展示
  • 将产品添加到购物车
  • 产品信息验证

购物车(TC_Cart_01)

  • 查看购物车内容
  • 从购物车中删除商品
  • 结账过程

🔧 MCP服务器集成

该框架利用模型上下文协议(MCP)服务器:

Excel MCP服务器

  • 从Excel文件读取测试数据
  • 动态测试用例生成
  • 数据驱动测试支持

剧作家MCP服务器

  • 增强的浏览器自动化
  • 实时元件检测
  • 跨浏览器兼容性测试

🚀 CI/CD集成

GitHub操作

工作流程示例:

name: Test Automation
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: '3.11'
      - name: Install dependencies
        run: |
          pip install -r requirements.txt
          playwright install
      - name: Run tests
        run: pytest --alluredir=allure-results
      - name: Upload results
        uses: actions/upload-artifact@v3
        with:
          name: allure-results
          path: allure-results

🤝 贡献

  1. 分叉存储库
  2. 创建要素分支
  3. 进行更改
  4. 添加新功能的测试
  5. 提交拉取请求

📄 许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。

🆘 支持

如有疑问和支持:

  • 在GitHub存储库中创建问题
  • 联系人:[您的电子邮件]
  • 文档:[Wiki链接]

🏆 功能展示

✅ 已完成的功能

  • ✅ 页面对象模型实现
  • ✅ BDD与Gherkin场景
  • ✅ Allure报告集成
  • ✅ Excel测试数据管理
  • ✅ MCP服务器集成
  • ✅ 跨浏览器测试
  • ✅ 并行测试执行
  • ✅ 故障时的屏幕截图
  • ✅ 视频录制支持
  • ✅ 多环境配置

🔄 即将推出的功能

  • 🔄 API测试集成
  • 🔄 移动测试支持
  • 🔄 性能测试
  • 🔄 视觉回归测试
  • 🔄 数据库验证

内置于❤️ 使用Playwright、Python和MCP服务器