README.md•9.86 kB
# Automation Script Generator MCP Server
An MCP (Model Context Protocol) server for automated test script generation using the WDIO framework. This server processes test scenarios directly from user prompts and generates complete WDIO test suites with **intelligent file management** that analyzes existing files to determine whether to update them or create new ones.
## Features
- **🧠 Smart File Analysis**: Intelligently analyzes existing test files to prevent duplicates and maintain organized structure
- **Direct User Input**: Process test scenarios from user prompts (no external dependencies)
- **Repository Analysis**: Analyze existing patterns and conventions
- **WDIO Code Generation**: Generate complete test suites with:
- Feature files (Gherkin syntax)
- Step definition files (WDIO functions)
- Page Object Model files (selectors and methods)
- Component files (test data collections)
- **Schema Validation**: Comprehensive input validation using JSON Schema
- **Code Review & Enhancement**: Automated code review with improvements
- **Intelligent Decision Making**:
- Updates existing features when similarity > 60%
- Reuses page objects with matching selectors
- Extends step definitions when steps are similar
- Creates new files only when necessary
- **Pattern Recognition**: Reuse existing functions and maintain consistency
## Schema Validation
The MCP server includes robust input validation using [AJV (Another JSON Schema Validator)](https://ajv.js.org/). All tool inputs are validated against their defined schemas before execution.
### Validation Features
- **Type Checking**: Ensures correct data types (string, object, array, etc.)
- **Required Fields**: Validates that all required parameters are provided
- **String Constraints**: Enforces minimum length requirements for important fields
- **Additional Properties**: Prevents extra/unknown properties in requests
- **Enum Validation**: Validates against predefined allowed values
- **Detailed Error Messages**: Provides clear feedback on validation failures
## 🧠 Smart File Analysis
The server now includes intelligent file management that analyzes your existing test structure before generating new files. This prevents duplication and maintains an organized test suite.
### How It Works
1. **Feature Similarity Analysis**: Compares new scenarios with existing features using keyword matching
2. **Selector Overlap Detection**: Identifies when new tests share page elements with existing page objects
3. **Step Definition Reuse**: Finds existing step definitions that can be extended rather than duplicated
4. **Strategic Decision Making**: Determines whether to update existing files or create new ones
### Decision Logic
- **Feature Similarity > 60%**: Updates existing feature file with new scenarios
- **Matching Selectors Found**: Extends existing page object with new elements
- **Step Similarity > 80%**: Reuses and extends existing step definitions
- **No Matches Found**: Creates new test files
### Smart Analysis Examples
```javascript
// Scenario: "User Login with 2FA"
// Decision: Update existing login.feature (85% similarity)
// Action: Add new scenario to existing file
// Reason: Keywords match: "user", "login", "credentials"
// Scenario: "Product Catalog Search"
// Decision: Create new product-catalog.feature
// Action: Generate new test suite
// Reason: No similar features found (<30% similarity)
// Scenario: "Login Page Validation"
// Decision: Update existing login.page.js
// Action: Add validation selectors to page object
// Reason: Selectors overlap: usernameInput, passwordInput
```
### Benefits
- **Maintains Organized Structure**: Keeps related tests together
- **Prevents Duplicate Code**: Avoids redundant test scenarios and step definitions
- **Reduces Maintenance**: Fewer files to maintain and update
- **Improves Reusability**: Maximizes reuse of existing test components
### Example Validation
```javascript
// ✅ Valid request
{
"database_id": "test-db-123",
"filter": {
"tags": ["LOGIN"],
"status": "ready"
}
}
// ❌ Invalid request - missing required field
{
"filter": { "tags": ["LOGIN"] }
// Error: must have required property 'database_id'
}
// ❌ Invalid request - extra properties
{
"database_id": "test-db-123",
"invalid_property": "not allowed"
// Error: must NOT have additional properties
}
```
### Testing Validation
Run the validation demo to see schema validation in action:
```bash
npm run test:validation
# or
node validation-demo.js
```
## Process Flow
1. **Input from Notion**: Fetch scenario_title, tags (test_id), gherkin syntax, selectors, and data items
2. **Repository Analysis**: Read existing patterns and validate formats
3. **Code Generation**: Create feature, steps, page, and component files
4. **Code Review**: Improve, add docs, enhance, validate POM, check for reusable functions
## Setup
### Prerequisites
- Node.js 18+
- Notion integration token
- Notion database with test scenarios
### Installation
1. Clone or create the project:
```bash
git clone <repository-url>
cd automation-script-generator
```
2. Install dependencies:
```bash
npm install
```
3. Configure environment:
```bash
cp .env.example .env
# Edit .env with your Notion token and database ID
```
### Environment Configuration
Create a `.env` file with the following variables (optional):
```env
DEFAULT_REPO_PATH=./test-repository
OUTPUT_BASE_PATH=./generated
WDIO_CONFIG_PATH=./wdio.conf.js
```
## Usage
### Starting the MCP Server
```bash
npm start
```
The server will run on stdio and expose the following tool:
### Main Tool: `process_test_scenario`
Process a complete test scenario from user input and generate all necessary WDIO files.
**Parameters:**
- `scenario_title` (required): Title of the test scenario
- `gherkin_syntax` (required): Complete Gherkin syntax with Given/When/Then steps
- `selectors` (required): UI element selectors as key-value pairs
- `output_directory` (required): Base directory for generated files
- `tags` (optional): Test ID tags (e.g., ["@login", "@smoke", "@TEST-001"])
- `data_items` (optional): Test data items and configurations
- `repo_path` (optional): Path to existing repository for pattern analysis
### Additional Tools
The following individual tools are also available for granular control:
#### `analyze_repository_patterns`
Analyze repository for existing patterns and conventions.
#### `generate_feature_file`
Generate WDIO feature file with Gherkin syntax.
#### `generate_steps_file`
Generate step definitions file.
#### `generate_page_file`
Generate page object file.
#### `generate_component_file`
Generate component file with test data.
#### `review_and_enhance_code`
Review and enhance generated code.
## Example Workflow
Here's how to use the MCP server to generate a complete test suite from a user prompt:
```javascript
// Single call to process complete scenario
await process_test_scenario({
scenario_title: "User Login Functionality",
tags: ["@login", "@smoke", "@TEST-001"],
gherkin_syntax: `Feature: User Login
Scenario: Successful login with valid credentials
Given I am on the login page
When I enter valid username "user@example.com"
And I enter valid password "password123"
And I click the login button
Then I should be redirected to the dashboard
And I should see the welcome message`,
selectors: {
usernameInput: '[data-testid="username-input"]',
passwordInput: '[data-testid="password-input"]',
loginButton: '[data-testid="login-button"]',
welcomeMessage: ".welcome-message",
},
data_items: {
validUser: {
username: "user@example.com",
password: "password123",
},
invalidUser: {
username: "invalid@example.com",
password: "wrongpassword",
},
},
output_directory: "./generated-tests",
repo_path: "./existing-tests", // Optional: for pattern analysis
});
```
This single call will:
1. Analyze existing repository patterns (if repo_path provided)
2. Generate feature file with Gherkin syntax
3. Generate step definitions with WDIO functions
4. Generate page object with selectors and methods
5. Generate test data component (if data_items provided)
6. Review and enhance all generated code
7. Return summary of all generated files
## Generated File Structure
The server generates files following WDIO best practices:
```
test/
├── features/
│ └── *.feature # Gherkin scenarios
├── step-definitions/
│ └── *.steps.js # Step implementations
├── pageobjects/
│ └── *.page.js # Page Object Models
└── data/
└── *.data.js # Test data components
```
## Code Quality Features
- **Documentation**: Automatic JSDoc comments
- **POM Patterns**: Page Object Model best practices
- **Function Reuse**: Detection of existing step functions
- **Naming Conventions**: Consistent naming across files
- **Error Handling**: Proper error handling in generated code
- **WDIO Integration**: Full compatibility with WebDriverIO framework
## Testing Validation
Run the validation demo to see schema validation in action:
```bash
npm run test:validation
```
## User Prompt Format
See [USER-PROMPT-EXAMPLES.md](./USER-PROMPT-EXAMPLES.md) for detailed examples of how to format user prompts for the MCP server.
## Contributing
1. Fork the repository
2. Create a feature branch
3. Make changes following the coding standards
4. Add tests for new functionality
5. Submit a pull request
## License
ISC License
## Support
For issues and questions:
1. Check the examples in the `/examples` directory
2. Review the user prompt examples in `USER-PROMPT-EXAMPLES.md`
3. Review the MCP documentation at https://modelcontextprotocol.io
4. Open an issue in the repository