Skip to content

solosza/py-selenium-framework-mcp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Selenium Test Automation Framework

Python 3.12+ Selenium MCP License: MIT Tests

A production-ready, 4-layer test automation framework with AI-powered test generation via Model Context Protocol (MCP). Built with Python and Selenium. Designed for teams who need structure and manual testers transitioning to automation.

Tests → Roles → Tasks → Pages → WebInterface

What Is This?

This framework provides a clean, maintainable architecture for browser test automation. Instead of writing spaghetti Selenium code, you get organized layers that separate concerns:

  • Tests - What you're verifying (assertions only)
  • Roles - Who is doing it (GuestUser, RegisteredUser, Admin)
  • Tasks - What workflow they're performing (login, browse catalog, checkout)
  • Pages - How to interact with UI elements (click, type, select)

Result: Tests that are easy to read, maintain, and scale.

Who Is This For?

You Are This Framework Helps You
Manual Tester Learn automation with clear patterns, not messy scripts
Junior Automator Skip the "how do I organize this?" phase
QA Team Get a ready-made structure instead of building from scratch
Solo Developer Production-grade architecture for your projects

Features

Core Framework

  • 4-Layer Architecture - Clean separation of concerns
  • Page Object Model - UI changes don't break your tests
  • Role-Based Testing - Test as different user personas
  • Built-in Logging - Automatic logging at every layer
  • HTML Reports - Professional test reports out of the box
  • Chrome Support - Works with Chrome browser (other browsers can be added via WebInterface)
  • JSON Test Data - Externalized, maintainable test data

AI Integration (MCP Server)

  • 11 MCP Tools - Complete test generation workflow
  • User Story → Tests - Convert requirements to test scenarios automatically
  • Element Discovery - AI discovers page elements for you
  • Code Generation - Generate Page Objects, Tasks, Roles, Tests
  • Agent Agnostic - Works with Claude Code, Cursor, Windsurf, or any MCP-compatible AI agent

Quick Start

Get running in 5 minutes:

Prerequisites

  • Python 3.12 or higher
  • Chrome browser installed
  • Git

Installation

# Clone the repository
git clone https://github.com/solosza/py-selenium-framework-mcp.git
cd py-selenium-framework-mcp

# Create virtual environment (recommended)
python -m venv venv

# Activate virtual environment
# Windows:
venv\Scripts\activate
# macOS/Linux:
source venv/bin/activate

# Install dependencies
pip install -r requirements.txt

Run Your First Test

# Run all tests
pytest tests/ -v

# Run with HTML report
pytest tests/ -v --html=reports/report.html --self-contained-html

# Run specific test category
pytest tests/catalog/ -v

# Run in headless mode (no browser window)
pytest tests/ -v --headless=True

See Results

After running tests, open reports/report.html in your browser for a detailed report.

Test Results

Current test suite against Automation Practice:

Test Suite Tests Status
Invalid Credentials 6 All Passing
Browse Category 4 All Passing
Filter Products 4 All Passing
Sort by Price 4 All Passing
Registration 1/6 Environment Issues*
Valid Login 0/2 Environment Issues*
Quick View 1/4 Website Issues*
Logout 0/3 Skipped (requires login)

Total: 18 passing, 10 failing, 3 skipped

*Failures are due to test environment (no pre-registered user on live site) and website bugs - not framework issues. The framework architecture is fully validated.

Detailed Setup

Environment Configuration

The framework uses JSON configuration files:

framework/resources/config/environment_config.json

Default configuration points to Automation Practice demo site. To test your own application, update the URL:

{
  "DEFAULT": {
    "url": "https://your-app-url.com"
  }
}

Test Data

Test user data is stored in:

tests/data/test_users.json

Example structure:

{
  "registered_user": {
    "email": "test@example.com",
    "password": "SecurePass123!"
  },
  "new_user": {
    "email": "newuser@example.com",
    "first_name": "John",
    "last_name": "Doe"
  }
}

Browser Support

The framework currently supports Chrome browser. The architecture is designed to support additional browsers by extending the WebInterface class - contributions welcome!

How to Use

Running Tests

# All tests
pytest tests/ -v

# By marker (category)
pytest tests/ -v -m catalog
pytest tests/ -v -m auth
pytest tests/ -v -m smoke

# Single test file
pytest tests/catalog/test_browse_category.py -v

# Single test function
pytest tests/catalog/test_browse_category.py::test_browse_women_category -v

# With HTML report
pytest tests/ -v --html=reports/report.html --self-contained-html

# Headless mode (CI/CD)
pytest tests/ -v --headless=True

Understanding Test Output

tests/catalog/test_browse_category.py::test_browse_women_category PASSED [25%]
  • PASSED - Test succeeded
  • FAILED - Assertion failed (check report for details)
  • ERROR - Test crashed (setup/teardown issue)
  • SKIPPED - Intentionally skipped (missing prerequisite)

Architecture

The 4-Layer Pattern

┌─────────────────────────────────────────────────────────────┐
│ TEST LAYER                                                  │
│ • Arrange, Act, Assert only                                 │
│ • Calls ONE role method                                     │
│ • Asserts via Page Object state-checks                      │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│ ROLE LAYER                                                  │
│ • User personas (GuestUser, RegisteredUser)                 │
│ • Orchestrates multiple tasks into workflows                │
│ • Contains user credentials/context                         │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│ TASK LAYER                                                  │
│ • Single business operations (login, add_to_cart)           │
│ • Calls page object methods                                 │
│ • Domain-focused, not UI-focused                            │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│ PAGE OBJECT LAYER                                           │
│ • One class per page/component                              │
│ • Locators as class constants                               │
│ • Atomic methods (click, type, select)                      │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│ WEB INTERFACE                                               │
│ • Selenium wrapper                                          │
│ • Handles waits, logging, error handling                    │
│ • Single point of browser interaction                       │
└─────────────────────────────────────────────────────────────┘

Why This Structure?

Problem How This Framework Solves It
UI changes break everything Locators in ONE place (Page Objects)
Tests are hard to read Tests only have assertions, logic is in Roles/Tasks
Code duplication Tasks are reusable across tests
Hard to test different users Roles encapsulate user-specific behavior
Debugging is painful Automatic logging at every layer

Quick Reference Rules

GOLDEN RULES:
1. Locators ONLY in Page Objects
2. Tasks/Roles return NOTHING (None)
3. Tests assert via Page Object state-check methods
4. No inheritance - composition only
5. One responsibility per layer

For complete architecture documentation, see FRAMEWORK.md.

MCP Integration: AI-Powered Test Generation

The framework includes an MCP (Model Context Protocol) server that enables AI coding agents to generate tests automatically. This is the bridge from manual testing to automation.

What is MCP?

Model Context Protocol is an open standard that allows AI agents to interact with external tools. This framework provides 11 specialized tools for test automation.

Supported AI Agents

The MCP server works with any MCP-compatible AI coding agent:

  • Claude Code (Anthropic)
  • Cursor (with MCP support)
  • Windsurf (Codeium)
  • Any future MCP-compatible agent

The 11 MCP Tools

Code Generation Tools (Bottom-Up Workflow: 1→6)

# Tool Purpose
1 generate_tests_from_user_story Convert user story → test scenarios (Given-When-Then)
2 discover_page_elements Discover interactive elements on any page
3 generate_page_object Generate POM code from discovered elements
4 generate_task Create task workflow methods from POM
5 generate_role Create role class from tasks
6 generate_test_template Generate complete pytest test code

Utility Tools

Tool Purpose
list_tests Catalog all available tests
get_framework_structure Map framework architecture
run_test Execute tests and return results
analyze_failure AI-powered debugging for failed tests
get_test_coverage Calculate test coverage

MCP Setup

Prerequisites

  • Python 3.12+
  • An MCP-compatible AI agent (Claude Code, Cursor, etc.)

Installation

# Install MCP server dependencies
pip install -r mcp_server/requirements.txt

Configure Your AI Agent

For Claude Code:

# Add the MCP server to Claude Code
claude mcp add qa-automation -- python /path/to/mcp_server/server.py

For Other Agents: Configure your agent to run the MCP server:

Command: python mcp_server/server.py

From User Story to Working Test

You describe what you want to test. The AI builds everything for you.

You say:

"I want to test that a guest user can browse the Women category and see products"

The AI does the rest:

  1. Creates test scenarios from your description
  2. Discovers buttons, links, and forms on the page
  3. Generates the Page Object (code that knows how to click and type)
  4. Generates the Task (code that performs actions like "browse category")
  5. Generates the Role (code that represents "guest user")
  6. Generates the test (ties it all together)
  7. Runs the test and shows you the results

What you get:

def test_browse_women_category(web_interface, config):
    guest = GuestUser(web_interface, config["url"])

    guest.browse_category("Women")

    assert product_list_page.has_products()

That's it. You described the test in plain English. The AI wrote the code.

Example Conversation

You: I need to test that users can filter products by size

AI: I'll create that test for you.

    First, here are the test scenarios I came up with:
    1. Filter by size S - shows only small products
    2. Filter by size M - shows only medium products
    3. Filter by invalid size - handles gracefully

    I found the filter elements on the page and generated all the code.
    The test is ready. Want me to run it?

You: Yes

AI: Test passed! The filter correctly shows only size "S" products.

No manual coding required. You describe, the AI builds.

Self-Healing Tests

When a test fails, the AI doesn't just report the error. It fixes it.

Website changed?

AI: The test failed - the "Add to Cart" button moved to a different location.
    I found the new element and updated the Page Object.
    Re-running... Test passed!

Generated code has errors?

AI: The test failed - my generated code had a syntax error.
    I checked the framework patterns, fixed the code to match.
    Re-running... Test passed!

The AI analyzes failures, looks at existing code patterns in your framework, updates the code to match, and re-runs automatically. Whether it's a website change or a code generation mistake, the AI learns from your codebase and fixes it.

MCP Server Development

The MCP tools are in mcp_server/tools/. Each tool follows a consistent pattern:

async def tool_name(arguments: dict) -> str:
    """Tool description."""
    # Implementation
    return json.dumps(result)

To add new tools:

  1. Create mcp_server/tools/tool_XX_name.py
  2. Register in mcp_server/server.py
  3. Follow existing patterns

For Manual Testers: Your Learning Path

New to automation? Start here:

Step 1: Understand the Layers

Think of it like your manual testing:

Manual Testing Framework Layer
"I'm testing as a guest user" Role (GuestUser)
"I need to browse products" Task (browse_category)
"I click the Women menu link" Page Object (click method)
"I verify products are displayed" Test (assertion)

Step 2: Read a Simple Test

Open tests/catalog/test_browse_category.py:

def test_browse_women_category(web_interface, config):
    # Arrange - Set up
    guest = GuestUser(web_interface, config["url"])
    product_list_page = ProductListPage(web_interface)

    # Act - Do the action
    guest.browse_category("Women")

    # Assert - Verify result
    assert product_list_page.has_products(), "Products should be displayed"

That's it. The test reads like a user story.

Step 3: Trace the Flow

  1. Test creates a GuestUser
  2. Calls guest.browse_category("Women")
  3. Role calls catalog_tasks.browse_category("Women")
  4. Task calls navigation_menu.click_category("Women")
  5. Page Object calls self.web.click(locator)

Step 4: Write Your First Test

Copy an existing test and modify:

  1. Pick a test file to copy
  2. Change the category or action
  3. Run it: pytest tests/your_test.py -v

Step 5: Go Deeper

Read FRAMEWORK.md for complete patterns and examples.

Project Structure

py-selenium-framework-mcp/
├── framework/                    # Core framework (reusable)
│   ├── interfaces/
│   │   └── web_interface.py      # Selenium wrapper
│   ├── pages/                    # Page Objects
│   │   ├── auth/                 # Login, Registration pages
│   │   └── catalog/              # Product listing, filters
│   ├── tasks/                    # Business operations
│   │   ├── common/               # Shared tasks (login, navigate)
│   │   └── catalog/              # Catalog-specific tasks
│   ├── roles/                    # User personas
│   │   ├── auth/                 # RegisteredUser
│   │   └── guest/                # GuestUser
│   └── resources/
│       ├── config/               # Environment settings
│       ├── chromedriver/         # Driver factory
│       └── utilities/            # Logging, helpers
│
├── tests/                        # Test scenarios
│   ├── conftest.py               # Pytest fixtures
│   ├── data/                     # Test data (JSON)
│   ├── auth/                     # Authentication tests
│   └── catalog/                  # Catalog tests
│
├── docs/                         # Documentation (gitignored)
│   └── DEFECT_LOG.md             # Issue tracking
│
├── mcp_server/                   # MCP AI integration
│   ├── server.py                 # MCP server entry point
│   ├── requirements.txt          # MCP dependencies
│   ├── tools/                    # 11 MCP tools
│   │   ├── tool_01_generate_tests_from_user_story.py
│   │   ├── tool_02_discover_page_elements.py
│   │   ├── tool_03_generate_page_object.py
│   │   ├── tool_04_generate_task.py
│   │   ├── tool_05_generate_role.py
│   │   ├── tool_06_generate_test_template.py
│   │   └── ... (tools 07-11)
│   └── utils/                    # Code generation utilities
│
├── FRAMEWORK.md                  # Complete architecture reference
├── CLAUDE.md                     # AI assistant instructions
└── README.md                     # This file

Test Examples

The included tests serve dual purposes:

For Portfolio Review

These tests demonstrate production-quality automation:

  • 33 test scenarios across authentication and catalog
  • 18 passing tests validating framework architecture
  • Real-world error handling and edge cases

For Learning & Reference

Use these as templates for your own tests:

Test File What It Demonstrates
test_browse_category.py Simple happy path testing
test_invalid_credentials.py Negative testing patterns
test_filter_products.py Complex user workflows
test_sort_by_price.py State verification patterns

Contributing

The 4-layer architecture (Roles → Tasks → Pages → WebInterface) is framework-agnostic. The current implementation uses Selenium, but the patterns work anywhere.

Framework Ports Wanted

Wanted: Community implementations for:

  • Playwright (Python)
  • Cypress (JavaScript)
  • Puppeteer (Node.js)

The key abstraction point is WebInterface - implement the same interface for your framework of choice.

How to Contribute

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/your-feature)
  3. Follow existing code patterns
  4. Submit a pull request

Code Standards

  • Follow the 4-layer architecture
  • Locators only in Page Objects
  • Tasks/Roles return None
  • Use @autologger decorators
  • See FRAMEWORK.md for patterns

Troubleshooting

Common Issues

ChromeDriver version mismatch

selenium.common.exceptions.SessionNotCreatedException

Solution: The framework uses webdriver-manager which auto-downloads the correct driver. If issues persist, update Chrome browser.

Element not found / Timeout

selenium.common.exceptions.TimeoutException

Solution: The target website may be slow. Increase timeout in web_interface.py or check if the site is accessible.

Tests fail on first run Some tests require a registered user that doesn't exist on the demo site. This is expected - see Test Results.

Getting Help

  • Architecture questions: Read FRAMEWORK.md
  • Bug reports: Open an issue at GitHub Issues with Python version, error message, and steps to reproduce

License

MIT License - See LICENSE for details.

Free to use, modify, and distribute. Attribution appreciated but not required.

Author

Built by solosza as a portfolio project demonstrating QA engineering and test architecture skills.

Acknowledgments

  • Architecture patterns inspired by enterprise test automation frameworks
  • Demo site: Automation Practice
  • Selenium WebDriver team
  • pytest and pytest-html maintainers

Questions? Open an issue or check the documentation.

Found this useful? Star the repo to show support!

About

Production-grade Python Selenium test automation framework with MCP integration.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages