|
| 1 | +import path from 'path'; |
| 2 | +import { fileURLToPath } from 'url'; |
| 3 | + |
| 4 | +import { chromium } from '@playwright/test'; |
| 5 | + |
| 6 | +const __filename = fileURLToPath(import.meta.url); |
| 7 | +const __dirname = path.dirname(__filename); |
| 8 | + |
| 9 | +const VIEWPORTS = [ |
| 10 | + { name: 'mobile', width: 375, height: 812 }, |
| 11 | + { name: 'tablet', width: 768, height: 1024 }, |
| 12 | +]; |
| 13 | + |
| 14 | +const COLOR_SCHEMES = ['light', 'dark']; |
| 15 | + |
| 16 | +const BASE_URL = process.env.BASE_URL || 'http://localhost:5173'; |
| 17 | +const OUTPUT_DIR = path.join(__dirname, '../mock-screenshots'); |
| 18 | + |
| 19 | +async function generateScreenshots() { |
| 20 | + const browser = await chromium.launch(); |
| 21 | + |
| 22 | + try { |
| 23 | + for (const colorScheme of COLOR_SCHEMES) { |
| 24 | + for (const viewport of VIEWPORTS) { |
| 25 | + console.log( |
| 26 | + `Generating ${viewport.name} ${colorScheme} screenshot (${viewport.width}x${viewport.height})...`, |
| 27 | + ); |
| 28 | + |
| 29 | + const context = await browser.newContext({ |
| 30 | + viewport: { width: viewport.width, height: viewport.height }, |
| 31 | + deviceScaleFactor: 2, |
| 32 | + colorScheme, |
| 33 | + }); |
| 34 | + |
| 35 | + const page = await context.newPage(); |
| 36 | + |
| 37 | + // Listen to console messages |
| 38 | + page.on('console', (msg) => { |
| 39 | + const type = msg.type(); |
| 40 | + if (type === 'error' || type === 'warning') { |
| 41 | + console.log(` [Browser ${type}]:`, msg.text()); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + // Listen to page errors |
| 46 | + page.on('pageerror', (error) => { |
| 47 | + console.error(' [Browser error]:', error.message); |
| 48 | + }); |
| 49 | + |
| 50 | + // Navigate to the mock home page |
| 51 | + const url = `${BASE_URL}/#/_mock/home`; |
| 52 | + console.log(` Navigating to ${url}...`); |
| 53 | + await page.goto(url, { waitUntil: 'load', timeout: 60000 }); |
| 54 | + |
| 55 | + // Wait for React app to mount |
| 56 | + await page.waitForSelector('#app', { timeout: 10000 }); |
| 57 | + console.log(' ✓ App mounted'); |
| 58 | + |
| 59 | + // Wait for React to hydrate and route to render |
| 60 | + await page.waitForTimeout(3000); |
| 61 | + |
| 62 | + // Wait for the timeline to be rendered |
| 63 | + try { |
| 64 | + await page.waitForSelector('.timeline', { timeout: 30000 }); |
| 65 | + console.log(' ✓ Timeline found'); |
| 66 | + } catch (e) { |
| 67 | + console.error(' ✗ Timeline not found.'); |
| 68 | + console.error(' Current URL:', page.url()); |
| 69 | + const bodyHTML = await page.evaluate(() => document.body.innerHTML); |
| 70 | + console.error(' Body HTML:', bodyHTML.substring(0, 1000)); |
| 71 | + const errors = await page.evaluate(() => { |
| 72 | + return window.__errors || []; |
| 73 | + }); |
| 74 | + console.error(' JS Errors:', errors); |
| 75 | + throw e; |
| 76 | + } |
| 77 | + |
| 78 | + // Wait for timeline items to be rendered |
| 79 | + try { |
| 80 | + await page.waitForSelector('.timeline-item', { timeout: 30000 }); |
| 81 | + console.log(' ✓ Timeline items found'); |
| 82 | + } catch (e) { |
| 83 | + console.error(' ✗ Timeline items not found'); |
| 84 | + throw e; |
| 85 | + } |
| 86 | + |
| 87 | + // Wait for images and fonts to load |
| 88 | + await page.waitForTimeout(3000); |
| 89 | + |
| 90 | + const screenshotPath = path.join( |
| 91 | + OUTPUT_DIR, |
| 92 | + `mock-home-${viewport.name}-${colorScheme}@2x.png`, |
| 93 | + ); |
| 94 | + await page.screenshot({ |
| 95 | + path: screenshotPath, |
| 96 | + fullPage: true, |
| 97 | + }); |
| 98 | + |
| 99 | + console.log(` ✓ Screenshot saved: ${screenshotPath}`); |
| 100 | + await context.close(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + await browser.close(); |
| 105 | + console.log('\n✨ All screenshots generated successfully!'); |
| 106 | + console.log( |
| 107 | + 'Generated 4 screenshots: mobile & desktop in both light & dark modes', |
| 108 | + ); |
| 109 | + } catch (error) { |
| 110 | + await browser.close(); |
| 111 | + throw error; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +generateScreenshots().catch((error) => { |
| 116 | + console.error('Error generating screenshots:', error); |
| 117 | + process.exit(1); |
| 118 | +}); |
0 commit comments