Sponsored by Deepsite.site

Cypress Po And Tests Generator

Created By
Juan Pablo Realini5 months ago
With an already setup Cypress framework, pass the URL of the page you need to create a Page Object class and test cases for, and it will create the page object class with all locators, action methods (type, click), get methods, and main workflow methods, and a test file (cy.ts or cy.js) containing proposed positive and negative test cases, already coded based on the page object. Sample prompt: "Create the page object and tests for URL"
Content

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.

Server Config

{
  "mcpServers": {
    "cypress-mcp": {
      "command": "npx",
      "args": [
        "-y",
        "@jprealini/cypress-mcp"
      ]
    }
  }
}
Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
ChatWiseThe second fastest AI chatbot™
Playwright McpPlaywright MCP server
Serper MCP ServerA Serper MCP Server
Tavily Mcp
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
DeepChatYour AI Partner on Desktop
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
CursorThe AI Code Editor
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
WindsurfThe new purpose-built IDE to harness magic
Amap Maps高德地图官方 MCP Server
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.