本 MCP(模型上下文协议)服务器可自动为任意网页生成完整的 Cypress 页面对象类 以及 全面的测试套件。
服务器会生成 两个文件:
{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('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')
})
})
})
服务器能够智能检测常见模式并生成相应的测试:
npm install
启动服务器:
npx tsx main.ts
与 MCP 客户端配合使用:
服务器提供了一个 generateLocator 工具,该工具接受一个 URL 参数。
工具调用示例:
{
"method": "tools/call",
"params": {
"name": "generateLocator",
"arguments": {
"url": "https://example.com/login"
}
}
}
响应格式: 服务器会返回页面对象类和测试套件:
// ===== 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 服务器实现。puppeteer:网页抓取和页面渲染。cheerio:HTML 解析和元素选择。zod:模式验证。typescript:类型安全。服务器包含全面的错误处理,用于处理以下情况:
服务器使用 Puppeteer 并采用以下设置:
若要添加对新元素类型、交互方法或测试模式的支持,请修改 main.ts 中的 generatePageObjectClass 和 generateCypressTests 函数。