AIPythonPlaywrightE2E27July25_02
Cucumber BDD Test Automation Framework with Playwright MCP Integration for E-commerce Testing
GitHub Stars
0
User Rating
Not Rated
Forks
0
Issues
0
Views
3
Favorites
0
Sauce Demo BDD Test Automation Framework
A comprehensive Cucumber BDD test automation framework in Python using Playwright for the Sauce Demo e-commerce application.
šÆ Overview
This framework implements Behavior-Driven Development (BDD) using pytest-bdd with Playwright browser automation. It's designed to test the Sauce Demo application (https://www.saucedemo.com/) with full integration to Playwright MCP (Model Context Protocol) server for enhanced browser automation capabilities.
šļø Framework Architecture
The framework follows the Page Object Model (POM) design pattern and implements BDD (Behavior-Driven Development) methodology:
ECommercePortal13/
āāā pages/ # Page Object Model classes
ā āāā __init__.py
ā āāā base_page.py # Base page with common methods
ā āāā login_page.py # Login page objects
ā āāā products_page.py # Products/Inventory page objects
ā āāā cart_page.py # Cart page objects
āāā features/ # Gherkin feature files
ā āāā authentication.feature
ā āāā inventory.feature
ā āāā cart.feature
āāā step_definitions/ # Python glue logic for BDD
ā āāā __init__.py
ā āāā common_steps.py # Shared step definitions
ā āāā auth_steps.py # Authentication steps
ā āāā cart_steps.py # Cart-specific steps
āāā tests/ # Test runners
ā āāā __init__.py
ā āāā test_authentication.py
ā āāā test_inventory.py
ā āāā test_cart.py
āāā TestData/ # Test data and configurations
ā āāā test_data.py
āāā reports/ # Generated test reports
āāā conftest.py # Pytest configuration and fixtures
āāā pytest.ini # Pytest settings
āāā requirements.txt # Dependencies
š Features
Core Features
- ā BDD Implementation: Cucumber-style Gherkin scenarios with pytest-bdd
- ā Page Object Model: Modular, maintainable page objects
- ā MCP Integration: Playwright MCP server for enhanced browser automation
- ā Multi-Browser Support: Chromium, Firefox, WebKit
- ā Parallel Execution: pytest-xdist for concurrent test execution
- ā Rich Reporting: HTML, JUnit XML, and Allure reports
- ā Retry Logic: Automatic retry for flaky tests
- ā Screenshot Capture: On failure and key checkpoints
- ā Comprehensive Logging: Detailed test execution logs
Test Coverage
- š Authentication Module (
@auth
): Login scenarios with valid/invalid credentials - š¦ Inventory Module (
@inventory
): Product listing, sorting, cart operations - š Cart Module (
@cart
): Cart management and validation - š„ Smoke Tests (
@smoke
): Critical functionality verification
š Test Cases Implemented
Authentication Module (@auth)
Test ID | Description | Tags |
---|---|---|
TC_AUTH_01 | Login with valid credentials | @auth @smoke |
TC_AUTH_01b | Login with invalid credentials | @auth |
TC_AUTH_02 | Login with empty username | @auth |
TC_AUTH_03 | Login with empty password | @auth |
TC_AUTH_04 | Login with locked out user | @auth |
Inventory Module (@inventory)
Test ID | Description | Tags |
---|---|---|
TC_INV_01 | Verify product listing | @inventory @smoke |
TC_INV_02 | Sort products by Name (AāZ) | @inventory |
TC_INV_03 | Sort products by Name (ZāA) | @inventory |
TC_INV_04 | Sort by Price (Low to High) | @inventory |
TC_INV_05 | Sort by Price (High to Low) | @inventory |
Cart Module (@cart)
Test ID | Description | Tags |
---|---|---|
TC_CART_01 | View cart contents | @cart @smoke |
TC_CART_02 | View empty cart | @cart |
TC_CART_03 | Add/verify item in cart | @cart |
TC_CART_04 | Remove item from cart | @cart |
TC_CART_05 | Continue shopping from cart | @cart |
š ļø Setup Instructions
Prerequisites
- Python 3.8+
- VS Code with MCP support
- Playwright MCP server configured
Installation
Clone the repository:
git clone <repository-url> cd ECommercePortal13
Create virtual environment:
python -m venv .venv .venv\Scripts\activate # Windows # source .venv/bin/activate # Linux/Mac
Install dependencies:
pip install -r requirements.txt
Install Playwright browsers:
playwright install
Verify MCP configuration: Ensure
.vscode/settings.json
contains:{ "mcpServers": { "playwright": { "command": "npx", "args": ["@playwright/mcp@latest"] } } }
š Running Tests
Run All Tests
pytest
Run by Module
# Authentication tests
pytest -m auth
# Inventory tests
pytest -m inventory
# Cart tests
pytest -m cart
Run Smoke Tests
pytest -m smoke
Run with Custom Options
# Parallel execution
pytest -n 4
# Generate HTML report
pytest --html=reports/report.html
# Run specific feature
pytest tests/test_authentication.py
# Run with retries
pytest --reruns 3 --reruns-delay 1
Run in Headless Mode
pytest --headless
š Reports and Logging
Generated Reports
- HTML Report:
reports/report.html
- Interactive test results - JUnit XML:
reports/junit.xml
- CI/CD integration - Pytest Log:
reports/pytest.log
- Detailed execution logs - Screenshots: Captured on failures in
reports/screenshots/
Viewing Reports
# Open HTML report
start reports/report.html # Windows
open reports/report.html # Mac
xdg-open reports/report.html # Linux
šŖ Playwright MCP Integration
The framework integrates with Playwright MCP server for:
- Enhanced Browser Control: Advanced browser automation capabilities
- Real-time Debugging: Live browser inspection during test execution
- Dynamic Element Handling: Intelligent wait strategies and element interactions
- Cross-browser Testing: Seamless switching between browser engines
MCP Commands Used
mcp_playwright_browser_navigate
: Navigate to URLsmcp_playwright_browser_click
: Click elementsmcp_playwright_browser_type
: Type textmcp_playwright_browser_snapshot
: Capture page statemcp_playwright_browser_wait_for
: Wait for conditions
š§ Configuration
Browser Configuration
Edit conftest.py
to modify browser settings:
BROWSER_TYPE = "chromium" # chromium, firefox, webkit
HEADLESS = False
TIMEOUT = 30000
VIEWPORT_SIZE = {"width": 1280, "height": 720}
Test Data
Modify TestData/test_data.py
for:
- User credentials
- Product information
- Expected text values
- Sort options
Retry Configuration
Configure retry logic in conftest.py
:
RETRY_CONFIG = {
"max_retries": 3,
"retry_delay": 1,
"retry_on_failure": True
}
š Writing New Tests
1. Create Feature File
# features/new_feature.feature
@new_module
Feature: New Feature
Scenario: New test scenario
Given precondition
When action
Then verification
2. Implement Step Definitions
# step_definitions/new_steps.py
@given('precondition')
def step_precondition(browser_context):
# Implementation
pass
3. Create Test Runner
# tests/test_new_feature.py
from pytest_bdd import scenarios
scenarios('../features/new_feature.feature')
class TestNewFeature:
@pytest.mark.new_module
def test_scenario(self, browser_context, test_metadata):
# Test implementation
pass
š Debugging and Troubleshooting
Common Issues
MCP Server Not Running:
- Verify MCP configuration in VS Code settings
- Restart VS Code
- Check MCP server logs
Element Not Found:
- Verify selectors in page objects
- Check wait strategies
- Update timeout values
Test Failures:
- Check screenshots in reports folder
- Review pytest logs
- Verify test data
Debug Mode
# Run with verbose output
pytest -v -s
# Debug specific test
pytest -k "test_name" -v -s --capture=no
š CI/CD Integration
GitHub Actions
# .github/workflows/tests.yml
name: Test Suite
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: 3.9
- name: Install dependencies
run: |
pip install -r requirements.txt
playwright install
- name: Run tests
run: pytest --html=reports/report.html
Jenkins Pipeline
pipeline {
agent any
stages {
stage('Setup') {
steps {
sh 'pip install -r requirements.txt'
sh 'playwright install'
}
}
stage('Test') {
steps {
sh 'pytest --junitxml=reports/junit.xml'
}
}
}
post {
always {
publishHTML([
allowMissing: false,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: 'reports',
reportFiles: 'report.html',
reportName: 'Test Report'
])
}
}
}
š Best Practices
- Page Objects: Keep page objects focused and maintainable
- Step Definitions: Write reusable, atomic step definitions
- Test Data: Externalize test data for easy maintenance
- Assertions: Use descriptive assertion messages
- Error Handling: Implement proper exception handling
- Documentation: Keep scenarios self-documenting with clear Given-When-Then
š¤ Contributing
- Fork the repository
- Create feature branch:
git checkout -b feature/new-feature
- Follow coding standards and add tests
- Commit changes:
git commit -m 'Add new feature'
- Push to branch:
git push origin feature/new-feature
- Submit pull request
š Support
For questions or issues:
- Create GitHub issue
- Review framework documentation
- Check existing test examples
- Consult Playwright MCP documentation
Happy Testing! šÆ
0
Followers
94
Repositories
0
Gists
0
Total Contributions