
Cypress页面对象测试生成器
STDIO自动生成Cypress页面对象类和测试套件
自动生成Cypress页面对象类和测试套件
This MCP (Model Context Protocol) server automatically generates complete Cypress Page Object classes AND comprehensive test suites for any web page.
The server generates two files:
{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 } }
{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('[email protected]', 'validpassword') cy.url().should('not.include', '/login') }) it('should show error with invalid credentials', () => { page.login('[email protected]', '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') }) }) })
The server intelligently detects common patterns and generates appropriate tests:
npm install
Start the server:
npx tsx main.ts
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" } } }
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', () => { ... }
// 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"
@modelcontextprotocol/sdk
: MCP server implementationpuppeteer
: Web scraping and page renderingcheerio
: HTML parsing and element selectionzod
: Schema validationtypescript
: Type safetyThe server includes comprehensive error handling for:
The server uses Puppeteer with the following settings:
To add support for new element types, interaction methods, or test patterns, modify the generatePageObjectClass
and generateCypressTests
functions in main.ts
.