Skip to main content
Glama
README.md
# ๐ŸŽญ Playwright MCP - AI-Powered Test Automation (OrangeHRM)

[![Playwright](https://img.shields.io/badge/Playwright-1.60+-45ba4b?logo=playwright)](https://playwright.dev)
[![TypeScript](https://img.shields.io/badge/TypeScript-5.x-3178C6?logo=typescript)](https://www.typescriptlang.org/)
[![MCP](https://img.shields.io/badge/MCP-Model%20Context%20Protocol-FF6F00)](https://modelcontextprotocol.io)
[![OrangeHRM](https://img.shields.io/badge/OrangeHRM-Demo-orange)](https://opensource-demo.orangehrmlive.com)

A **Proof of Concept** project demonstrating **AI-powered browser test automation** using **Playwright** integrated with the **Model Context Protocol (MCP)**. Tests are written in **TypeScript** using the **Page Object Model (POM)** design pattern, targeting the [OrangeHRM demo application](https://opensource-demo.orangehrmlive.com).

---

## ๐Ÿ“‹ Table of Contents

- [Overview](#-overview)
- [Architecture](#-architecture)
- [Tech Stack](#-tech-stack)
- [Project Structure](#-project-structure)
- [Test Scenarios](#-test-scenarios)
- [Getting Started](#-getting-started)
- [Running Tests](#-running-tests)
- [Page Object Model](#-page-object-model)
- [Data-Driven Testing](#-data-driven-testing)
- [Playwright MCP vs Playwright CLI](#-playwright-mcp-vs-playwright-cli)
- [Resources](#-resources)

---

## ๐ŸŽฏ Overview

This project showcases how **AI + Playwright** can automate browser testing using **plain English prompts** via the **Model Context Protocol (MCP)**. Instead of writing every line of automation code manually, you describe what you want to test in natural language, and AI helps generate and execute the automation.

### Key Concepts

| Component                        | Role                                               | Analogy       |
| -------------------------------- | -------------------------------------------------- | ------------- |
| **LLM** (Large Language Model)   | Understands requests & generates instructions      | ๐Ÿง  Brain      |
| **Agent**                        | Executes tasks automatically                       | โšก Doer       |
| **MCP** (Model Context Protocol) | Connects AI with real tools (browsers, APIs, etc.) | ๐Ÿ”— Translator |

---

## ๐Ÿ— Architecture

```text
           Plain English Prompt
                    โ”‚
                    โ–ผ
        Large Language Model (LLM)
                    โ”‚
          Generates Instructions
                    โ”‚
                    โ–ผ
                AI Agent
                    โ”‚
         Executes the Instructions
                    โ”‚
                    โ–ผ
      Model Context Protocol (MCP)
                    โ”‚
     Connects to Real Applications
                    โ”‚
                    โ–ผ
          Playwright + Browser
                    โ”‚
                    โ–ผ
          Browser Automation
```

---

## ๐Ÿ’ป Tech Stack

| Technology                                               | Purpose                           |
| -------------------------------------------------------- | --------------------------------- |
| **[Playwright](https://playwright.dev)** ^1.60           | Browser automation framework      |
| **[TypeScript](https://www.typescriptlang.org/)**        | Programming language              |
| **[MCP](https://modelcontextprotocol.io)**               | AI-to-tool communication protocol |
| **[csv-parse](https://www.npmjs.com/package/csv-parse)** | CSV parsing for data-driven tests |
| **Node.js**                                              | Runtime environment               |

---

## ๐Ÿ“ Project Structure

```
โ”œโ”€โ”€ ๐Ÿ“‚ pages/                          # Page Object Model classes
โ”‚   โ”œโ”€โ”€ LoginPage.ts                   # Login page locators & actions
โ”‚   โ””โ”€โ”€ PimPage.ts                     # PIM module locators & actions
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‚ tests/                          # Test specifications
โ”‚   โ”œโ”€โ”€ example.spec.ts                # Sample Playwright test
โ”‚   โ”œโ”€โ”€ orangehrm-login-data-driven.spec.ts       # Data-driven login (inline)
โ”‚   โ”œโ”€โ”€ orangehrm-login-data-driven-csv.spec.ts   # Data-driven login (CSV)
โ”‚   โ”œโ”€โ”€ orangehrm-logout.spec.ts                  # Logout flow test
โ”‚   โ”œโ”€โ”€ orangehrm-admin-system-users.spec.ts      # Admin module test
โ”‚   โ”œโ”€โ”€ orangehrm-buzz-post.spec.ts               # Buzz social feed test
โ”‚   โ”œโ”€โ”€ pim-search.spec.ts                        # PIM employee search
โ”‚   โ””โ”€โ”€ add-employee.spec.ts                      # Add employee (POM)
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‚ test_data/                     # Test data files
โ”‚   โ””โ”€โ”€ loginData.csv                 # CSV test data for login
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‚ playwright-report/             # HTML test reports
โ”œโ”€โ”€ ๐Ÿ“‚ test-results/                  # Test execution artifacts
โ”‚
โ”œโ”€โ”€ ๐Ÿ“„ playwright.config.ts           # Playwright configuration
โ”œโ”€โ”€ ๐Ÿ“„ package.json                   # Dependencies & scripts
โ”œโ”€โ”€ ๐Ÿ“„ README.md                      # This file
โ”‚
โ”œโ”€โ”€ ๐Ÿ“„ Playwright_MCP_Guide.md        # Detailed MCP concepts guide
โ”œโ”€โ”€ ๐Ÿ“„ PlaywrightMCP_Vs_CLI.md        # MCP vs CLI comparison
โ”œโ”€โ”€ ๐Ÿ“„ playwright-context.md          # MCP test generator context
โ”œโ”€โ”€ ๐Ÿ“„ playwright-context-pom.md      # MCP POM test generator context
โ”œโ”€โ”€ ๐Ÿ“„ prompts.md                     # Sample AI prompts used
โ””โ”€โ”€ ๐Ÿ“„ notes.md                       # Architecture & concept notes
```

---

## ๐Ÿงช Test Scenarios

| Test File                                 | Description                                                     | Pattern           |
| ----------------------------------------- | --------------------------------------------------------------- | ----------------- |
| `orangehrm-login-data-driven.spec.ts`     | Login validation with inline data (valid + invalid credentials) | Data-Driven       |
| `orangehrm-login-data-driven-csv.spec.ts` | Login validation using CSV data source                          | Data-Driven (CSV) |
| `orangehrm-logout.spec.ts`                | Login, logout, and verify redirect to login page                | Linear            |
| `orangehrm-admin-system-users.spec.ts`    | Navigate to Admin โ†’ verify System Users page                    | Linear            |
| `orangehrm-buzz-post.spec.ts`             | Post a message on Buzz feed and verify it appears               | Linear            |
| `pim-search.spec.ts`                      | Search employees by name in PIM module                          | Linear            |
| `add-employee.spec.ts`                    | Add a new employee using Page Object Model                      | POM               |
| `example.spec.ts`                         | Default Playwright sample test                                  | Linear            |

---

## ๐Ÿš€ Getting Started

### Prerequisites

- [Node.js](https://nodejs.org/) (v18 or later)
- [npm](https://www.npmjs.com/)

### Installation

```bash
# Clone the repository
git clone https://github.com/pavanoltraining/POC_Playwright_MCP_orangehrm.git

# Navigate to the project directory
cd POC_Playwright_MCP_orangehrm

# Install dependencies
npm install

# Install Playwright browsers
npx playwright install chromium
```

---

## โ–ถ๏ธ Running Tests

### Run all tests

```bash
npx playwright test
```

### Run a specific test file

```bash
npx playwright test tests/orangehrm-login-data-driven.spec.ts
```

### Run tests in UI mode

```bash
npx playwright test --ui
```

### View HTML report

```bash
npx playwright show-report
```

### Run with debug mode

```bash
npx playwright test --debug
```

---

## ๐Ÿงฉ Page Object Model

The project uses the **Page Object Model (POM)** design pattern for maintainable and reusable test code.

### Example: `LoginPage.ts`

```typescript
export class LoginPage {
  readonly usernameInput = page.getByPlaceholder("Username");
  readonly passwordInput = page.getByPlaceholder("Password");
  readonly loginButton = page.getByRole("button", { name: "Login" });

  async login(username: string, password: string) {
    await this.usernameInput.fill(username);
    await this.passwordInput.fill(password);
    await this.loginButton.click();
  }
}
```

### Example: `PimPage.ts`

```typescript
export class PimPage {
  async navigateToPim() {
    /* ... */
  }
  async openAddEmployee() {
    /* ... */
  }
  async addEmployee(firstName: string, lastName: string) {
    /* ... */
  }
}
```

---

## ๐Ÿ“Š Data-Driven Testing

### Inline Data-Driven

Tests login with multiple credentials defined inline:

| Username | Password | Expected Result     |
| -------- | -------- | ------------------- |
| Admin    | admin123 | Dashboard           |
| fakeuser | fakepass | Invalid credentials |
| ESSUser1 | ess123   | Invalid credentials |

### CSV Data-Driven

Tests read test cases from `test_data/loginData.csv`:

```csv
Username,Password,Expected
Admin,admin123,Dashboard
fakeuser,fakepass,Invalid credentials
ESSUser1,ess123,Invalid credentials
```

---

## ๐Ÿค– Playwright MCP vs Playwright CLI

| Feature                 | Playwright CLI                      | Playwright MCP                       |
| ----------------------- | ----------------------------------- | ------------------------------------ |
| Purpose                 | Command-line tool for Playwright    | AI bridge between LLM and Playwright |
| Used By                 | Developers & Testers                | AI Agents                            |
| Input                   | Terminal commands                   | Natural language prompts             |
| Browser Control         | Directly through Playwright scripts | Through AI + MCP Server              |
| Requires Coding         | Yes                                 | Minimal coding                       |
| Generates Code          | No                                  | Yes (AI-generated)                   |
| Executes Tests          | Yes                                 | Yes                                  |
| Uses Accessibility Tree | No                                  | Yes                                  |
| Supports AI Automation  | No                                  | Yes                                  |
| Best For                | Traditional automation              | AI-powered automation                |

---

## ๐Ÿ“š Resources

- [Playwright Documentation](https://playwright.dev/docs/intro)
- [Model Context Protocol](https://modelcontextprotocol.io)
- [OrangeHRM Demo](https://opensource-demo.orangehrmlive.com)
- [GitHub Repository](https://github.com/pavanoltraining/POC_Playwright_MCP_orangehrm)

---

## ๐Ÿ“„ License

This project is for **educational and demonstration purposes** only.

---

_Built with โค๏ธ using Playwright + MCP + AI_