playwright-ai-codegen-lib
Version:
A utility to auto-generate Playwright PageObjects and test scripts using OpenAI and DOM extraction.
38 lines (35 loc) • 1.77 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const pageObjectsDir = path_1.default.resolve(process.cwd(), 'pageObjects');
const testsOutputDir = path_1.default.resolve(process.cwd(), 'tests');
fs_1.default.mkdirSync(testsOutputDir, { recursive: true });
const files = fs_1.default.readdirSync(pageObjectsDir).filter(file => file.endsWith('Page.ts'));
for (const file of files) {
const className = file.replace('.ts', '');
const classImportPath = `../../pageObjects/${className}`;
const pageVar = className.charAt(0).toLowerCase() + className.slice(1);
// Read the page object file to extract method names
const content = fs_1.default.readFileSync(path_1.default.join(pageObjectsDir, file), 'utf-8');
const methodMatches = [...content.matchAll(/async\s+(\w+)\s*\(/g)];
const methods = methodMatches.map(match => match[1]);
const testFileName = `${className}.spec.ts`;
const testFilePath = path_1.default.join(testsOutputDir, testFileName);
const testCode = `
import { test, expect } from '@playwright/test';
import { ${className} } from '${classImportPath}';
test.describe('${className} Tests', () => {
test('Run all ${className} methods', async ({ page }) => {
await page.goto('https://example.com');
const ${pageVar} = new ${className}(page);
${methods.map(m => ` await ${pageVar}.${m}();`).join('\n')}
});
});
`.trim();
fs_1.default.writeFileSync(testFilePath, testCode, 'utf-8');
console.log(`✅ Test generated: ${testFilePath}`);
}