refactor_to_pom
Converts migrated Playwright tests to Page Object Model pattern by extracting selectors and creating reusable page object classes with methods.
Instructions
Refactors a migrated Playwright test to use Page Object Model pattern. Extracts actual selectors and creates proper page object classes with methods.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| testContent | Yes | The migrated Playwright test content to refactor | |
| filePath | No | File path for generating appropriate page object names | |
| existingPageObjects | No | Optional JSON string of existing page objects to extend or reuse |
Implementation Reference
- src/handlers/toolHandlers.js:115-146 (handler)The handler function that orchestrates the "refactor_to_pom" tool execution. It calls the transformer function and formats the result for the MCP client.
export async function handleRefactorToPom(args) { const { testContent, filePath = 'test.spec.js' } = args; const result = refactorToPom(testContent, filePath); return { content: [{ type: 'text', text: `# Page Object Model Refactoring ## Generated Page Object: ${result.pageObject.className} ### File: ${result.pageObject.fileName} \`\`\`javascript ${result.pageObject.content} \`\`\` ### Extracted Information: - URLs: ${result.pageInfo.urls.length} - Locators: ${result.pageInfo.locators.length} - Actions: ${result.pageInfo.actions.length} ### Next Steps: 1. Create file: \`pages/${result.pageObject.fileName}\` 2. Import the page object in your test 3. Replace direct page interactions with page object methods 4. Add additional methods as needed for your test scenarios `, }], }; } - src/handlers/tools.js:53-73 (registration)The tool registration and schema definition for "refactor_to_pom".
name: 'refactor_to_pom', description: 'Refactors a migrated Playwright test to use Page Object Model pattern. Extracts actual selectors and creates proper page object classes with methods.', inputSchema: { type: 'object', properties: { testContent: { type: 'string', description: 'The migrated Playwright test content to refactor', }, filePath: { type: 'string', description: 'File path for generating appropriate page object names', }, existingPageObjects: { type: 'string', description: 'Optional JSON string of existing page objects to extend or reuse', }, }, required: ['testContent'], }, }, - src/transformers/pom.js:172-185 (helper)The core implementation function "refactorToPom" which uses AST extraction to generate Page Object Model code.
export function refactorToPom(testContent, filePath = 'test.spec.js') { const pageInfo = extractPageInfo(testContent); const pageName = generatePageName(filePath); const pageObjectClass = generatePageObjectClass(pageName, pageInfo); return { pageObject: { className: pageName, fileName: `${pageName}.js`, content: pageObjectClass, }, pageInfo, }; }