const path = require('path'); const fs = require('fs'); const { chromium } = require('playwright'); const root = path.resolve(__dirname, '..'); const outputDir = path.join(root, 'artifacts', 'screenshots'); const edgePath = 'C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe'; const pages = [ { name: 'map', url: 'http://127.0.0.1:98/', settle: 1600, closeVersion: true }, { name: 'map-authenticated', url: 'http://127.0.0.1:98/', settle: 1800, closeVersion: true, authenticatedUser: true }, { name: 'analysis', url: 'http://127.0.0.1:98/analysis.html', settle: 1200 }, { name: 'ring-progress', url: 'http://127.0.0.1:98/ring_progress.html?task_id=preview&total=24', settle: 700 }, { name: 'admin-login', url: 'http://127.0.0.1:98/admin/login', settle: 700 }, { name: 'admin-dashboard', url: 'http://127.0.0.1:98/admin', settle: 1200, authenticated: true }, ]; function readAdminCredentials() { const config = fs.readFileSync(path.join(root, 'admin_config.txt'), 'utf8'); const values = {}; for (const line of config.split(/\r?\n/)) { const separator = line.indexOf('='); if (separator > 0) values[line.slice(0, separator).trim()] = line.slice(separator + 1).trim(); } if (!values.admin_username || !values.admin_password) { throw new Error('admin_config.txt 中缺少管理员账号或密码'); } return { username: values.admin_username, password: values.admin_password }; } (async () => { fs.mkdirSync(outputDir, { recursive: true }); const browser = await chromium.launch({ executablePath: edgePath, headless: true }); const context = await browser.newContext({ viewport: { width: 1440, height: 900 }, colorScheme: 'dark', deviceScaleFactor: 1, }); for (const item of pages) { const page = await context.newPage(); page.on('dialog', dialog => dialog.dismiss()); if (item.authenticatedUser) { const token = process.env.XBNIU_CAPTURE_TOKEN; if (!token) throw new Error('缺少 XBNIU_CAPTURE_TOKEN'); await page.addInitScript(value => localStorage.setItem('auth_token', value), token); } if (item.authenticated) { const credentials = readAdminCredentials(); await page.goto('http://127.0.0.1:98/admin/login', { waitUntil: 'domcontentloaded' }); await page.locator('#username').fill(credentials.username); await page.locator('#password').fill(credentials.password); await Promise.all([ page.waitForURL('**/admin'), page.getByRole('button', { name: '登录', exact: true }).click(), ]); } await page.goto(item.url, { waitUntil: 'domcontentloaded' }); await page.waitForTimeout(item.settle); if (item.authenticated) { // 只修改截图页面中的 DOM,避免将真实邮箱与凭据编号写入图片。 await page.locator('.tab-content.active tbody tr').evaluateAll(rows => { for (const row of rows) { const cells = row.querySelectorAll('td'); if (cells[1]) cells[1].textContent = '••••••@•••'; if (cells[2]) cells[2].textContent = '••••••'; } }); } if (item.authenticatedUser) { // 截图只展示登录后的主界面,忽略测试账号的过期凭据提示。 await page.addStyleTag({ content: '#token-expired-modal, .login-gate, .toast, #loading { display: none !important; }', }); } if (item.closeVersion) { const close = page.locator('#version-modal .auth-close'); if (await close.isVisible()) { await close.click(); await page.waitForTimeout(300); } // 本地截图环境不在百度地图 AK 的 Referer 白名单中,隐藏该环境提示。 await page.locator('.toast').evaluateAll(elements => { for (const element of elements) { if (element.textContent.includes('地图初始化失败')) element.classList.remove('show'); } }); await page.addStyleTag({ content: '.toast { display: none !important; }' }); } await page.screenshot({ path: path.join(outputDir, `${item.name}.png`) }); await page.close(); } await browser.close(); console.log(outputDir); })();