const fs = require('fs'); const path = require('path'); // 页面配置 const PAGES = { onsite: { baseUrl: 'http://localhost:8000/emergency-app/onsite', pages: [ { page: 'workbench/index.html', screenshot: 'screenshots/index.jpg' }, { page: 'workbench/work-order-detail-ongoing.html', screenshot: 'screenshots/work-order-detail-ongoing.jpg' }, { page: 'workbench/work-order-detail.html', screenshot: 'screenshots/work-order-detail.jpg' }, { page: 'workbench/serious-illness-list.html', screenshot: 'screenshots/serious-illness-list.jpg' }, { page: 'workbench/serious-illness-detail-pending.html', screenshot: 'screenshots/serious-illness-detail-pending.jpg' }, { page: 'workbench/serious-illness-detail-done.html', screenshot: 'screenshots/serious-illness-detail-done.jpg' }, { page: 'knowledge/index.html', screenshot: 'screenshots/knowledge-index.jpg' }, { page: 'knowledge/article-detail.html', screenshot: 'screenshots/knowledge-article-detail.jpg' }, { page: 'profile/index.html', screenshot: 'screenshots/profile-index.jpg' }, { page: 'profile/edit-profile.html', screenshot: 'screenshots/edit-profile.jpg' }, { page: 'profile/change-pwd.html', screenshot: 'screenshots/change-pwd.jpg' }, ] }, operator: { baseUrl: 'http://localhost:8000/emergency-app/operator', pages: [ { page: 'workbench/index.html', screenshot: 'screenshots/index.jpg' }, { page: 'workbench/emergency-chat-ongoing.html', screenshot: 'screenshots/emergency-chat-ongoing.jpg' }, { page: 'workbench/emergency-chat-done.html', screenshot: 'screenshots/emergency-chat-done.jpg' }, { page: 'workbench/work-order-detail.html', screenshot: 'screenshots/work-order-detail.jpg' }, { page: 'workbench/serious-illness-detail-pending.html', screenshot: 'screenshots/serious-illness-detail-pending.jpg' }, { page: 'knowledge/index.html', screenshot: 'screenshots/knowledge-index.jpg' }, { page: 'knowledge/article-detail.html', screenshot: 'screenshots/knowledge-article-detail.jpg' }, { page: 'profile/index.html', screenshot: 'screenshots/profile-index.jpg' }, { page: 'profile/edit-profile.html', screenshot: 'screenshots/edit-profile.jpg' }, { page: 'profile/change-pwd.html', screenshot: 'screenshots/change-pwd.jpg' }, ] }, professional: { baseUrl: 'http://localhost:8000/emergency-app/professional', pages: [ { page: 'workbench/index.html', screenshot: 'screenshots/index.jpg' }, { page: 'workbench/emergency-chat-ongoing.html', screenshot: 'screenshots/emergency-chat-ongoing.jpg' }, { page: 'workbench/emergency-chat-done.html', screenshot: 'screenshots/emergency-chat-done.jpg' }, { page: 'workbench/work-order-detail-ongoing.html', screenshot: 'screenshots/work-order-detail-ongoing.jpg' }, { page: 'workbench/work-order-detail.html', screenshot: 'screenshots/work-order-detail.jpg' }, { page: 'knowledge/index.html', screenshot: 'screenshots/knowledge-index.jpg' }, { page: 'knowledge/article-detail.html', screenshot: 'screenshots/knowledge-article-detail.jpg' }, { page: 'profile/index.html', screenshot: 'screenshots/profile-index.jpg' }, { page: 'profile/edit-profile.html', screenshot: 'screenshots/edit-profile.jpg' }, { page: 'profile/change-pwd.html', screenshot: 'screenshots/change-pwd.jpg' }, ] } }; async function takeScreenshots() { const playwright = require('playwright'); const browser = await playwright.chromium.launch(); // 仅跑指定页面:ONLY_PAGES="professional:workbench/patient-location.html,professional:workbench/rescue-opinion.html" const onlyPages = (process.env.ONLY_PAGES || '').split(',').map(s => s.trim()).filter(Boolean); if (onlyPages.length > 0) console.log(`[Filter] Only run: ${onlyPages.join(', ')}`); for (const [role, config] of Object.entries(PAGES)) { console.log(`\nScreenshotting ${role}...`); for (const item of config.pages) { if (onlyPages.length > 0) { const key = `${role}:${item.page}`; if (!onlyPages.includes(key)) continue; } const url = `${config.baseUrl}/${item.page}`; const outputPath = path.join(__dirname, 'emergency-app', role, 'doc', item.screenshot); // Create directory if not exists const dir = path.dirname(outputPath); if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } try { const page = await browser.newPage(); await page.setViewportSize({ width: 375, height: 812 }); console.log(` Loading: ${url}`); await page.goto(url, { waitUntil: 'networkidle' }); // 修复:部分页面 fullPage 计算高度异常,改为截取手机壳容器 if (role === 'professional' && (item.page === 'workbench/patient-location.html' || item.page === 'workbench/rescue-opinion.html')) { console.log(` [FIX] Using .phone-shell selector for ${item.page}`); const shell = await page.$('.phone-shell'); if (!shell) throw new Error('Cannot find .phone-shell'); await shell.screenshot({ path: outputPath }); } else { await page.screenshot({ path: outputPath, fullPage: true }); } await page.close(); console.log(` OK: ${item.screenshot}`); } catch (e) { console.log(` FAIL: ${item.screenshot} - ${e.message}`); } } } await browser.close(); } takeScreenshots().catch(console.error);