/** * Enhanced TEC Template Testing Script * * Comprehensive E2E testing for the enhanced TEC Community Events template * Tests 100% field population success rate with all new WordPress fields */ const { chromium } = require('playwright'); // Test configuration const config = { baseUrl: process.env.UPSKILL_STAGING_URL || 'https://upskill-staging.measurequick.com', timeout: 45000, testCredentials: { username: 'test_trainer', password: 'TestTrainer123!' }, testEventData: { // Core WordPress fields title: 'Advanced HVAC Diagnostics Workshop - Enhanced Template Test', content: `
Join us for an intensive workshop covering advanced HVAC diagnostic techniques. This enhanced template test verifies all field population capabilities.
Bring: Laptop, basic tools, and eagerness to learn!
`, excerpt: 'Join us for an intensive HVAC diagnostics workshop covering advanced techniques, electrical troubleshooting, and energy efficiency. Perfect for technicians looking to enhance their diagnostic skills.', // Enhanced taxonomy fields categories: [1, 2], // Will map to actual category IDs tags: ['HVAC', 'diagnostics', 'workshop', 'training', 'certification', 'energy-efficiency'], // Featured image (will use a test image) featured_image: { id: null, // Will be set during test url: null }, // TEC specific fields venue: 'Test Training Center', organizer: 'Test HVAC Training Company', start_date: '2025-09-15', start_time: '09:00', end_date: '2025-09-15', end_time: '17:00', cost: '299' } }; console.log('๐งช Enhanced TEC Template E2E Testing Suite'); console.log(`๐ Testing URL: ${config.baseUrl}`); console.log('๐ Testing enhanced template with 100% field population'); console.log(''); async function runEnhancedTemplateTest() { const browser = await chromium.launch({ headless: false, // Show browser for demonstration slowMo: 1000 // Slow down for visual verification }); const context = await browser.newContext({ viewport: { width: 1920, height: 1080 } }); const page = await context.newPage(); // Enhanced console logging page.on('console', msg => { if (msg.type() === 'log' && ( msg.text().includes('HVAC') || msg.text().includes('Enhanced') || msg.text().includes('Field Population') )) { console.log(`๐ Browser: ${msg.text()}`); } }); // Track errors const pageErrors = []; page.on('pageerror', error => { pageErrors.push(error.message); console.error(`โ Page Error: ${error.message}`); }); try { console.log('๐ Step 1: Login and navigate to event creation'); // Navigate to login await page.goto(`${config.baseUrl}/training-login/`); await page.waitForLoadState('networkidle'); // Login const loginSuccess = await performLogin(page, config.testCredentials); if (!loginSuccess) { throw new Error('Login failed'); } console.log('๐ Step 2: Navigate to enhanced event creation form'); // Navigate to event creation - try multiple possible URLs const eventCreateUrls = [ '/trainer/event/create/', '/events/community/add/', '/community/events/add/', '/events/add/' ]; let eventFormFound = false; for (let url of eventCreateUrls) { try { await page.goto(`${config.baseUrl}${url}`); await page.waitForTimeout(2000); // Check if enhanced template is active const enhancedIndicator = await page.querySelector('.hvac-success-indicator'); if (enhancedIndicator) { console.log(`โ Enhanced template found at: ${url}`); eventFormFound = true; break; } } catch (e) { console.log(`โน๏ธ URL ${url} not accessible, trying next...`); } } if (!eventFormFound) { // Try to find create event link from dashboard await page.goto(`${config.baseUrl}/trainer/dashboard/`); await page.waitForTimeout(2000); const createEventLink = await page.locator('a').filter({ hasText: /create.*event/i }).first(); if (await createEventLink.count() > 0) { await createEventLink.click(); await page.waitForLoadState('networkidle'); eventFormFound = true; } } if (!eventFormFound) { throw new Error('Could not find enhanced event creation form'); } console.log('๐ Step 3: Verify enhanced template is active'); await verifyEnhancedTemplate(page); console.log('๐ Step 4: Test enhanced field population system'); const populationResults = await testEnhancedFieldPopulation(page, config.testEventData); console.log('๐ Step 5: Test individual field functionality'); const fieldTests = await testIndividualFields(page); console.log('๐ Step 6: Test form submission'); const submissionResult = await testFormSubmission(page); console.log('๐ Step 7: Generate comprehensive test report'); const testReport = generateTestReport({ template_verification: true, field_population: populationResults, individual_fields: fieldTests, form_submission: submissionResult, page_errors: pageErrors }); console.log('\n๐ ENHANCED TEMPLATE TEST COMPLETE'); console.log('='.repeat(50)); console.log(testReport); // Take final screenshot await page.screenshot({ path: 'test-results/enhanced-template-final.png', fullPage: true }); return testReport; } catch (error) { console.error('โ Enhanced template test failed:', error.message); // Take error screenshot await page.screenshot({ path: 'test-results/enhanced-template-error.png', fullPage: true }); throw error; } finally { await browser.close(); } } async function performLogin(page, credentials) { try { // Find login form elements const usernameField = await page.locator('input[name="log"], #user_login, input[type="text"]').first(); const passwordField = await page.locator('input[name="pwd"], #user_pass, input[type="password"]').first(); const submitButton = await page.locator('input[type="submit"], button[type="submit"], .wp-submit').first(); if (await usernameField.count() === 0 || await passwordField.count() === 0) { throw new Error('Login form not found'); } await usernameField.fill(credentials.username); await passwordField.fill(credentials.password); await submitButton.click(); await page.waitForLoadState('networkidle'); // Verify login success const loginError = await page.locator('.login_error, .error, #login_error').first(); if (await loginError.count() > 0) { const errorText = await loginError.textContent(); throw new Error(`Login failed: ${errorText}`); } console.log('โ Login successful'); return true; } catch (error) { console.error('โ Login error:', error.message); return false; } } async function verifyEnhancedTemplate(page) { console.log('๐ Verifying enhanced template features...'); const checks = { enhanced_indicator: '.hvac-success-indicator', excerpt_field: '#hvac_post_excerpt', categories_section: '#hvac-categories-section', featured_image_section: '#hvac-featured-image-section', tags_section: '#hvac-tags-section', enhanced_styles: '#hvac-tec-enhanced-styles' }; const results = {}; for (let [name, selector] of Object.entries(checks)) { const element = await page.locator(selector).first(); const exists = await element.count() > 0; results[name] = exists; if (exists) { console.log(`โ ${name}: Found`); } else { console.log(`โ ${name}: Not found (${selector})`); } } const totalChecks = Object.keys(checks).length; const passedChecks = Object.values(results).filter(Boolean).length; const successRate = Math.round((passedChecks / totalChecks) * 100); console.log(`๐ Template verification: ${passedChecks}/${totalChecks} features found (${successRate}%)`); if (successRate < 80) { throw new Error(`Enhanced template verification failed: ${successRate}% < 80%`); } return results; } async function testEnhancedFieldPopulation(page, eventData) { console.log('๐ฏ Testing enhanced field population system...'); // Inject test data and run population const populationResult = await page.evaluate((testData) => { // Check if enhanced system is available if (!window.HVACEnhancedFieldPopulation) { return { error: 'Enhanced field population system not found' }; } // Run field access test first const accessTest = window.HVACEnhancedFieldPopulation.testFieldAccess(); // Run field population const populationTest = window.HVACEnhancedFieldPopulation.populateAllFields(testData); return { access_test: accessTest, population_test: populationTest, debug_info: window.HVACEnhancedFieldPopulation.debug || {} }; }, eventData); console.log('๐ Field Population Results:'); if (populationResult.error) { console.error(`โ ${populationResult.error}`); return populationResult; } const accessRate = populationResult.access_test?.access_rate || 0; const populationRate = populationResult.population_test?.successRate || 0; console.log(`๐ Field Access: ${accessRate}% (${populationResult.access_test?.found_fields}/${populationResult.access_test?.total_fields})`); console.log(`๐ฏ Population Success: ${populationRate}% (${populationResult.population_test?.populated_fields}/${populationResult.population_test?.total_fields})`); // Take screenshot of populated form await page.screenshot({ path: 'test-results/enhanced-template-populated.png', fullPage: true }); return populationResult; } async function testIndividualFields(page) { console.log('๐งช Testing individual enhanced field functionality...'); const fieldTests = {}; // Test excerpt field fieldTests.excerpt = await testExcerptField(page); // Test categories field fieldTests.categories = await testCategoriesField(page); // Test featured image field fieldTests.featured_image = await testFeaturedImageField(page); // Test tags field fieldTests.tags = await testTagsField(page); const totalTests = Object.keys(fieldTests).length; const passedTests = Object.values(fieldTests).filter(Boolean).length; const testSuccessRate = Math.round((passedTests / totalTests) * 100); console.log(`๐ Individual field tests: ${passedTests}/${totalTests} passed (${testSuccessRate}%)`); return fieldTests; } async function testExcerptField(page) { try { const excerptField = page.locator('#hvac_post_excerpt'); if (await excerptField.count() === 0) return false; // Test typing and character counter await excerptField.clear(); await excerptField.fill('Test excerpt content for enhanced template verification'); // Check character counter const counter = page.locator('#excerpt-counter .current-count'); if (await counter.count() > 0) { const count = await counter.textContent(); console.log(`โ Excerpt field: Character counter shows ${count}`); } return true; } catch (error) { console.log(`โ Excerpt field test failed: ${error.message}`); return false; } } async function testCategoriesField(page) { try { const categoriesSection = page.locator('#hvac-categories-section'); if (await categoriesSection.count() === 0) return false; // Test category selection const firstCategory = page.locator('.hvac-category-checkbox').first(); if (await firstCategory.count() > 0) { await firstCategory.check(); console.log('โ Categories field: Can select categories'); } // Test search functionality const searchInput = page.locator('#hvac_categories_search'); if (await searchInput.count() > 0) { await searchInput.fill('test'); await page.waitForTimeout(500); console.log('โ Categories field: Search functionality working'); } return true; } catch (error) { console.log(`โ Categories field test failed: ${error.message}`); return false; } } async function testFeaturedImageField(page) { try { const imageSection = page.locator('#hvac-featured-image-section'); if (await imageSection.count() === 0) return false; // Check if upload button exists and is clickable const uploadButton = page.locator('#hvac-upload-image-btn'); if (await uploadButton.count() > 0) { console.log('โ Featured image field: Upload button present'); } return true; } catch (error) { console.log(`โ Featured image field test failed: ${error.message}`); return false; } } async function testTagsField(page) { try { const tagsSection = page.locator('#hvac-tags-section'); if (await tagsSection.count() === 0) return false; // Test adding tags const tagsInput = page.locator('#hvac_tags_input'); if (await tagsInput.count() > 0) { await tagsInput.fill('test-tag'); await tagsInput.press('Enter'); // Check if tag was added const addedTag = page.locator('.hvac-tag-item'); if (await addedTag.count() > 0) { console.log('โ Tags field: Can add tags'); } } return true; } catch (error) { console.log(`โ Tags field test failed: ${error.message}`); return false; } } async function testFormSubmission(page) { try { // Look for submit button const submitButton = page.locator('input[type="submit"], button[type="submit"], .tribe-events-c-nav__list-item--publish').last(); if (await submitButton.count() === 0) { console.log('โ ๏ธ Form submission test skipped: Submit button not found'); return false; } console.log('โน๏ธ Form submission test: Submit button found but not clicking (test mode)'); return true; } catch (error) { console.log(`โ Form submission test failed: ${error.message}`); return false; } } function generateTestReport(results) { const { template_verification, field_population, individual_fields, form_submission, page_errors } = results; let report = '\n๐ ENHANCED TEC TEMPLATE TEST REPORT\n'; report += '='.repeat(50) + '\n\n'; // Template verification report += '๐ TEMPLATE VERIFICATION: '; report += template_verification ? 'โ PASSED\n' : 'โ FAILED\n'; // Field population results if (field_population?.population_test) { const pop = field_population.population_test; report += `๐ฏ FIELD POPULATION: ${pop.successRate}% (${pop.populated_fields}/${pop.total_fields})\n`; if (pop.successRate === 100) { report += ' ๐ TARGET ACHIEVED: 100% field population success!\n'; } } // Field access results if (field_population?.access_test) { const access = field_population.access_test; report += `๐ FIELD ACCESS: ${access.access_rate}% (${access.found_fields}/${access.total_fields})\n`; } // Individual field tests const fieldTestsCount = Object.keys(individual_fields).length; const fieldTestsPassed = Object.values(individual_fields).filter(Boolean).length; report += `๐งช INDIVIDUAL TESTS: ${fieldTestsPassed}/${fieldTestsCount} passed\n`; // Form submission report += '๐ค FORM SUBMISSION: '; report += form_submission ? 'โ READY\n' : 'โ ๏ธ NEEDS CHECK\n'; // Errors if (page_errors.length > 0) { report += `\nโ PAGE ERRORS (${page_errors.length}):\n`; page_errors.forEach((error, index) => { report += ` ${index + 1}. ${error}\n`; }); } else { report += '\nโ NO PAGE ERRORS DETECTED\n'; } // Overall assessment const overallSuccess = template_verification && (field_population?.population_test?.successRate || 0) >= 90 && fieldTestsPassed >= fieldTestsCount * 0.8; report += '\n๐ OVERALL RESULT: '; report += overallSuccess ? 'โ SUCCESS' : 'โ NEEDS IMPROVEMENT'; return report; } // Run the enhanced template test if (require.main === module) { runEnhancedTemplateTest() .then(report => { console.log('\nโ Enhanced TEC Template Test completed successfully'); process.exit(0); }) .catch(error => { console.error('\nโ Enhanced TEC Template Test failed:', error.message); process.exit(1); }); } module.exports = { runEnhancedTemplateTest };