Prompt Driven AI based Automation Framework using Playwright MCP in Python
Bridging Natural Language Intent and Sandboxed Browser Execution
A modern automation framework where QA engineers and non-technical stakeholders define end-to-end browser workflows in natural language prompts. ChatGPT compiles prompts into structured Model Context Protocol (MCP) action sequences, which are executed securely and deterministically through Playwright in Python.
Prompt Engine ➔ MCP Dispatcher ➔ Playwright Browser Engine
Testers write in natural language. ChatGPT interprets intent and returns structured action JSON. Predefined MCP Python handlers safely execute browser steps without arbitrary code execution.
1. Executive Summary
This case study presents the design and implementation of an enterprise prompt-driven automation framework built using Playwright for Python, Model Context Protocol (MCP) based orchestration, and the ChatGPT API.
The foundational premise of the solution is that QA engineers and business analysts author test scenarios in natural language prompts rather than writing traditional, brittle Playwright scripts. ChatGPT interprets the prompt, extracts domain parameters, and generates a strictly validated JSON execution sequence. The MCP execution layer subsequently maps each approved step to sandboxed Python Playwright functions that perform the browser interactions, state assertions, and telemetry captures.
2. Core Concept of the Project
The framework is architected around a streamlined three-phase pipeline that decouples test intent from low-level DOM selectors and browser execution protocols:
Natural Language Prompt
The tester or business analyst provides a scenario prompt describing the user journey, actions to take, and expected end-state validations in plain English.
ChatGPT Plan Generation
The prompt engine sends the scenario to ChatGPT with strict context and approved action schemas, returning a deterministic JSON plan of safe MCP steps.
MCP Execution Engine
The Python dispatcher runs the corresponding Playwright actions, handles authentication and assertions, and produces comprehensive test telemetry.
3. Example Prompt & Structured Execution Plan
ChatGPT does not directly manipulate or control the browser. Instead, it parses natural language into an approved, strongly-typed execution plan that the local engine can safely evaluate.
[
{ "action": "login", "user": "qa_user" },
{ "action": "open_connector_page" },
{ "action": "create_connector" },
{ "action": "fill_credentials" },
{ "action": "validate_connection" },
{ "action": "verify_connector_active" }
]4. Execution Architecture & Technology Stack
The execution layer maintains predefined MCP actions implemented in Python. ChatGPT can exclusively select from these approved operations, preventing arbitrary code execution while keeping the framework maintainable, resilient, and enterprise-secure.
5. Suggested Project Structure & Dependency Setup
A modular layout separating prompts, AI connectors, MCP action registries, and test execution harnesses:
Set up the virtual environment and install Playwright, OpenAI client SDK, Pytest parallel runner, and headless browser binaries:
OPENAI_API_KEY is configured in .env before launching tests.6. Predefined MCP Actions & Execution Dispatcher
Explore the modular Python components that power the prompt-to-browser execution pipeline:
from playwright.sync_api import Page, expect
def login(page: Page, username: str, password: str):
page.get_by_role('textbox', name='Username').fill(username)
page.get_by_role('textbox', name='Password').fill(password)
page.get_by_role('button', name='Sign in').click()
def open_connector_page(page: Page):
page.get_by_role('link', name='Connectors').click()
def create_connector(page: Page):
page.get_by_role('button', name='Add Connector').click()
def fill_credentials(page: Page, account_id: str, access_key: str):
page.get_by_role('textbox', name='Account ID').fill(account_id)
page.get_by_role('textbox', name='Access Key').fill(access_key)
def validate_connection(page: Page):
page.get_by_role('button', name='Validate').click()
def verify_connector_active(page: Page):
expect(page.get_by_text('Connector is active')).to_be_visible()8. Writing Tests with Prompts & Enterprise Value
The test case is no longer an imperative script bound to brittle XPath/CSS locators—it is a clear, reusable business intention:
Zero Playwright Code for QA Engineers
QA engineers focus entirely on test coverage, boundary conditions, and acceptance criteria without writing browser plumbing.
Cross-Functional Collaboration
Product Managers and Business Analysts can author and review executable specifications directly in plain English.
Reusable Across Environments
The same prompt runs across local, staging, and production environments by swapping parameter bindings and target URLs.
Strict MCP Security & Predictability
Execution remains deterministic and guarded. The AI cannot execute arbitrary code or bypass security boundaries.
10. AI-Assisted Failure Analysis & Triage
When a step fails, the framework immediately captures the error stack, screenshot path, trace summary, and DOM snapshot. These details are packaged and analyzed via ChatGPT to generate instant root-cause diagnostics and locator remediation:
def analyze_failure(error_message: str, dom_summary: str) -> str:
response = client.responses.create(
model='gpt-4.1-mini',
input=f'''
Error: {error_message}
DOM summary: {dom_summary}
Explain the likely cause and suggest a stable locator strategy.
'''
)
return response.output_text[Root Cause]: Button 'Add Connector' was renamed in v2.4 build to 'Create New Connector'. Accessible role changed to role='button', name='Create New Connector'.
[Suggested Fix]: Update MCP action create_connector to check for both locator variants or adopt page.get_by_test_id('connector-add-btn').
11. Enterprise Security Considerations
ChatGPT generates only registered MCP actions. It has zero capability to generate raw shell instructions, evaluate arbitrary scripts, or introduce unauthorized browser hooks.
Passwords, tokens, and access keys are resolved via environment variables and encrypted key stores—never fed into model training or third-party prompts.
Generated plans undergo JSON schema validation before dispatcher handoff. Unrecognized action names or missing required parameters trigger immediate test aborts.
Screenshots and network traces are scrubbed for sensitive customer data and stored within secure access-controlled CI/CD artifact storage.
12. CI/CD Integration (Jenkins & GitHub Actions)
Automated parallel execution and test artifact archiving seamlessly fit into enterprise delivery pipelines:
pipeline {
agent any
stages {
stage('Setup') {
steps {
sh 'python -m venv venv'
sh '. venv/bin/activate && pip install -r requirements.txt'
sh '. venv/bin/activate && playwright install --with-deps'
}
}
stage('Run Prompt Tests') {
steps {
sh '. venv/bin/activate && pytest -n auto'
}
}
stage('Archive') {
steps {
archiveArtifacts artifacts: 'reports/**, traces/**, screenshots/**'
}
}
}
}13. End-to-End Execution Flow
14. Observed Results & Engineering Impact
15. Future Enhancements & Conclusion
Conclusion
This project demonstrates a transformative shift from code-centric automation to prompt-driven automation. Testers describe expected behavior in natural language, ChatGPT converts the request into a structured execution plan, and the MCP Playwright engine executes the browser automation reliably and securely.
The combination of Playwright for Python, MCP orchestration, and the ChatGPT API creates a scalable, maintainable, and intelligent automation framework ideally suited for enterprise validation workflows.
Ready to Modernize Your Test Automation?
Talk with CBNITS QA automation architects about implementing prompt-driven, MCP-sandboxed testing frameworks for your enterprise web applications.
