playwright-test-generator
Version:
Automated Page Object and Functional Test Generator for Playwright
106 lines ⢠4.65 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generatePageObjectCLI = generatePageObjectCLI;
const commander_1 = require("commander");
const inquirer_1 = __importDefault(require("inquirer"));
const chalk_1 = __importDefault(require("chalk"));
const test_1 = require("@playwright/test");
const PageObjectGenerator_1 = require("../generators/PageObjectGenerator");
const NavigationHelper_1 = require("../generators/NavigationHelper");
const TestSetup_1 = require("../utils/TestSetup");
const EnvironmentConfig_1 = require("../utils/EnvironmentConfig");
const program = new commander_1.Command();
program
.name('generate-page-object')
.description('Generate Page Object Models for Playwright')
.version('1.0.0')
.option('-e, --env <environment>', 'Environment (dev, qa, prod)', 'qa')
.option('-h, --headless', 'Run in headless mode', false)
.option('-o, --output <path>', 'Output directory')
.action(async (options) => {
await generatePageObjectCLI(options);
});
async function generatePageObjectCLI(options = {}) {
console.log(chalk_1.default.blue('š Playwright Page Object Generator'));
console.log(chalk_1.default.gray('=====================================\n'));
try {
// Load environment configuration
const envConfig = new EnvironmentConfig_1.EnvironmentConfig(options.env);
const config = await envConfig.load();
if (!envConfig.validateConfig(config)) {
console.log(chalk_1.default.yellow('\nš” Please ensure your environment variables are set correctly.'));
return;
}
// Interactive prompts
const answers = await inquirer_1.default.prompt([
{
type: 'input',
name: 'pageName',
message: 'Enter the page name (e.g., "User Management"):',
validate: (input) => input.length > 0 || 'Page name is required'
},
{
type: 'list',
name: 'navigationPath',
message: 'Select navigation path:',
choices: [
...Object.keys(NavigationHelper_1.NavigationHelper.getNavigationPaths()),
'custom'
]
},
{
type: 'input',
name: 'outputDir',
message: 'Output directory:',
default: options.output || './pageObjects',
when: !options.output
}
]);
console.log(chalk_1.default.yellow('\nš Launching browser...'));
const browser = await test_1.chromium.launch({ headless: options.headless });
const context = await browser.newContext();
const page = await browser.newPage();
try {
// Setup and navigate
const testSetup = new TestSetup_1.TestSetup(page, context);
await testSetup.setup({
baseUrl: config.baseUrl,
username: config.username,
password: config.password
});
// Navigate to target page
if (answers.navigationPath !== 'custom') {
console.log(chalk_1.default.yellow(`š§ Navigating via: ${answers.navigationPath}`));
await NavigationHelper_1.NavigationHelper.navigateToPath(page, answers.navigationPath);
}
else {
console.log(chalk_1.default.yellow('\nā³ Please manually navigate to the target page...'));
await inquirer_1.default.prompt([{
type: 'input',
name: 'continue',
message: 'Press Enter when ready...'
}]);
}
// Generate page object
const generator = new PageObjectGenerator_1.PageObjectGenerator(page);
const outputPath = await generator.generatePageObject(answers.pageName, answers.outputDir || options.output || './pageObjects');
console.log(chalk_1.default.green('\nā
Page object generated successfully!'));
console.log(chalk_1.default.gray(`š Output: ${outputPath}`));
}
finally {
await browser.close();
}
}
catch (error) {
console.error(chalk_1.default.red('ā Error:'), error);
process.exit(1);
}
}
if (require.main === module) {
program.parse();
}
//# sourceMappingURL=generatePageObject.js.map
;