playwright-test-workflow
Version:
Global test workflow package for Playwright with custom reporters
84 lines (67 loc) • 2.81 kB
JavaScript
const fs = require('fs-extra');
const path = require('path');
const commands = {
init: initWorkflow,
help: showHelp
};
function showHelp() {
console.log(`
Test Workflow CLI
Usage:
test-workflow init [project-name] Initialize test workflow in current directory
test-workflow help Show this help message
Examples:
test-workflow init my-app Creates test workflow for "my-app"
test-workflow init Creates test workflow for current directory
`);
}
async function initWorkflow(projectName) {
const currentDir = process.cwd();
const templateDir = path.join(__dirname, '..', 'templates');
const reportersDir = path.join(__dirname, '..', 'reporters');
try {
// Create test-results directory
await fs.ensureDir(path.join(currentDir, 'test-results'));
// Create tests directory
await fs.ensureDir(path.join(currentDir, 'tests'));
// Copy template files
if (await fs.pathExists(templateDir)) {
const testMdTemplate = await fs.readFile(path.join(templateDir, 'test.md'), 'utf8');
const configTemplate = await fs.readFile(path.join(templateDir, 'playwright.config.js'), 'utf8');
// Replace placeholders
const finalTestMd = testMdTemplate.replace('[PROJECT_NAME]', projectName || 'Current Project')
.replace('[DATE]', new Date().toLocaleDateString());
// Write files
await fs.writeFile(path.join(currentDir, 'test.md'), finalTestMd);
// Only write config if it doesn't exist
const configPath = path.join(currentDir, 'playwright.config.js');
if (!await fs.pathExists(configPath)) {
await fs.writeFile(configPath, configTemplate);
}
// Copy reporters
await fs.copy(reportersDir, path.join(currentDir, 'reporters'));
console.log('✅ Test workflow initialized successfully!');
console.log('📁 Created: test.md, test-results/, tests/, reporters/');
console.log('⚙️ Updated: playwright.config.js (if not exists)');
console.log('');
console.log('Next steps:');
console.log('1. Edit test.md to add your specific test cases');
console.log('2. Create test files in tests/ directory');
console.log('3. Run: npx playwright test');
console.log('4. Check test-results/ for individual test group reports');
} else {
console.error('❌ Template files not found');
}
} catch (error) {
console.error('❌ Error initializing workflow:', error.message);
}
}
// Main execution
const command = process.argv[2];
const arg = process.argv[3];
if (commands[command]) {
commands[command](arg);
} else {
console.log('❌ Unknown command. Use "test-workflow help" for usage.');
}