MCP Cypress Page Object & Test Generator

MCP Cypress Page Object & Test Generator

An MCP server that automatically generates TypeScript Page Object classes and comprehensive Cypress test suites for any web page, supporting element interactions, workflow tests, and both positive and negative testing scenarios.

Category
访问服务器

README

MCP Cypress Page Object & Test Generator

This MCP (Model Context Protocol) server automatically generates complete Cypress Page Object classes AND comprehensive test suites for any web page.

Features

  • Web Scraping: Uses Puppeteer to fetch and render web pages
  • HTML Parsing: Uses Cheerio to parse HTML and extract element information
  • Page Object Generation: Creates complete TypeScript Page Object classes with:
    • Private element locators
    • Public getter methods
    • Interaction methods (click, type, select, etc.)
    • Workflow methods for common test scenarios
  • Test Suite Generation: Creates comprehensive Cypress test files with:
    • Positive test cases for all elements
    • Negative test cases for error handling
    • Workflow tests for common scenarios
    • Accessibility, performance, and edge case tests

Generated Output

The server generates two files:

1. Page Object Class ({ClassName}.ts)

export class ExampleComLoginPage {
  // Private elements
  #elements = {
    button_login: () => cy.get('#login-button'),
    input_username: () => cy.get('input[name="username"]'),
    link_home: () => cy.contains('a', 'Home')
  }

  // Public getters
  get ButtonLogin() { return this.#elements.button_login() }
  get InputUsername() { return this.#elements.input_username() }
  get LinkHome() { return this.#elements.link_home() }

  // Interaction methods
  clickButtonLogin() { return this.#elements.button_login().click() }
  typeInputUsername(text: string) { return this.#elements.input_username().type(text) }
  clickLinkHome() { return this.#elements.link_home().click() }

  // Workflow methods
  login(username: string, password: string) {
    this.typeInputUsername(username)
    this.typeInputPassword(password)
    this.clickButtonLogin()
    return this
  }
}

2. Test Suite ({ClassName}.cy.ts)

import { ExampleComLoginPage } from './ExampleComLoginPage'

describe('ExampleComLoginPage Tests', () => {
  let page: ExampleComLoginPage
  
  beforeEach(() => {
    cy.visit('https://example.com/login')
    page = new ExampleComLoginPage()
  })
  
  describe('Element Interactions', () => {
    it('should click button_login', () => {
      page.clickButtonLogin()
    })
    
    it('should type in input_username', () => {
      page.typeInputUsername('test input')
      page.getInputUsername().should('have.value', 'test input')
    })
  })
  
  describe('Login Workflow', () => {
    it('should login with valid credentials', () => {
      page.login('validuser@example.com', 'validpassword')
      cy.url().should('not.include', '/login')
    })
    
    it('should show error with invalid credentials', () => {
      page.login('invalid@example.com', 'wrongpassword')
      cy.contains('Invalid credentials').should('be.visible')
    })
  })
  
  describe('Error Handling', () => {
    it('should handle network errors gracefully', () => {
      cy.intercept('GET', '**', { forceNetworkError: true })
      cy.visit('https://example.com/login')
    })
  })
})

Test Categories Generated

Positive Test Cases

  • Element Interactions: Click, type, clear, check/uncheck for all detected elements
  • Workflow Tests: Login, search, navigation workflows
  • Form Validation: Successful form submissions
  • Element Visibility: All elements are visible and accessible

Negative Test Cases

  • Error Handling: Network errors, server errors, slow connections
  • Validation Errors: Empty fields, invalid formats, required field validation
  • Edge Cases: Large inputs, special characters, unicode text
  • Accessibility: ARIA labels, keyboard navigation

🔧 Additional Test Types

  • Performance Tests: Load times, rapid interactions
  • Responsive Tests: Different viewport sizes
  • Accessibility Tests: ARIA compliance, keyboard navigation
  • Security Tests: Input sanitization, XSS prevention

Element Types Supported

  • Buttons: Click interactions with validation
  • Input Fields: Type, clear, check/uncheck (for checkboxes/radio)
  • Links: Click interactions with navigation verification
  • Select Dropdowns: Select options with validation
  • Textareas: Type and clear with content verification
  • Forms: Submit interactions with success/error handling

Workflow Detection

The server intelligently detects common patterns and generates appropriate tests:

  • Login Forms: Username/password validation, error handling
  • Search Forms: Query validation, results verification
  • Navigation: Home links, breadcrumbs, menu items
  • Form Submissions: Success states, validation errors

Installation

npm install

Usage

  1. Start the server:

    npx tsx main.ts
    
  2. Use with an MCP client: The server exposes a generateLocator tool that accepts a URL parameter.

    Example tool call:

    {
      "method": "tools/call",
      "params": {
        "name": "generateLocator",
        "arguments": {
          "url": "https://example.com/login"
        }
      }
    }
    
  3. Response format: The server returns both the Page Object class and test suite:

    // ===== PAGE OBJECT CLASS =====
    // Save this as: ExampleComLoginPage.ts
    export class ExampleComLoginPage { ... }
    
    // ===== CYPRESS TESTS =====
    // Save this as: ExampleComLoginPage.cy.ts
    describe('ExampleComLoginPage Tests', () => { ... }
    

Example Usage in Tests

// Use the generated Page Object
import { ExampleComLoginPage } from './ExampleComLoginPage'

describe('Login Page', () => {
  const page = new ExampleComLoginPage()
  
  it('should login successfully', () => {
    page.login('username', 'password')
    page.verifyPageLoaded()
  })
})

// Run the generated test suite
// npx cypress run --spec "cypress/e2e/ExampleComLoginPage.cy.ts"

Dependencies

  • @modelcontextprotocol/sdk: MCP server implementation
  • puppeteer: Web scraping and page rendering
  • cheerio: HTML parsing and element selection
  • zod: Schema validation
  • typescript: Type safety

Error Handling

The server includes comprehensive error handling for:

  • Invalid URLs
  • Network connectivity issues
  • Page loading failures
  • HTML parsing errors

Browser Configuration

The server uses Puppeteer with the following settings:

  • Headless mode for server environments
  • No-sandbox mode for containerized deployments
  • Network idle waiting for dynamic content

Contributing

To add support for new element types, interaction methods, or test patterns, modify the generatePageObjectClass and generateCypressTests functions in main.ts.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选