// UI 冒烟:用 Playwright 真渲染前端,验证页面能加载、能登录、能进详情 import { chromium } from 'playwright' const WEB = 'http://127.0.0.1:3100' const results = [] function rec(name, ok, detail = '') { results.push({ name, ok, detail }) console.log(`${ok ? ' \x1b[32m✓\x1b[0m' : ' \x1b[31m✗\x1b[0m'} ${name}${detail ? ' -> ' + detail : ''}`) } const browser = await chromium.launch({ executablePath: process.env.CHROME_PATH || 'C:/Users/12914/AppData/Local/ms-playwright/chromium-1234/chrome-win64/chrome.exe', headless: true, }) const page = await browser.newPage() const consoleErrors = [] page.on('console', (m) => { if (m.type() === 'error') consoleErrors.push(m.text()) }) page.on('pageerror', (e) => consoleErrors.push('pageerror: ' + e.message)) // 1. 首页 await page.goto(WEB, { waitUntil: 'networkidle' }) const title = await page.title() rec('首页加载', title.includes('文章平台'), title) const hasNav = await page.locator('.tabbar').count() rec('底部导航渲染', hasNav > 0) // 2. 文章列表 await page.waitForTimeout(800) const cards = await page.locator('.article-card').count() rec(`文章列表渲染 ${cards} 张卡片`, cards > 0) // 3. 登录页 await page.goto(`${WEB}/login`, { waitUntil: 'networkidle' }) const loginH = await page.locator('.login-card h2').textContent() rec('登录页渲染', loginH?.includes('登录'), loginH) // 4. 实际注册并登录 const suffix = Date.now().toString().slice(-6) const account = `uitest${suffix}` await page.fill('input[autocomplete="username"]', account) await page.fill('input[type="password"]', 'UiPass12345!') // 切到注册 await page.click('.switch .link') await page.waitForTimeout(200) await page.fill('input[autocomplete="username"]', account) await page.fill('input[type="password"]', 'UiPass12345!') await page.click('button[type="submit"]') await page.waitForTimeout(1500) const urlAfter = page.url() rec('注册并跳转', !urlAfter.includes('/login'), urlAfter) // 5. 登录后顶部显示昵称 await page.goto(WEB, { waitUntil: 'networkidle' }) await page.waitForTimeout(500) const userChip = await page.locator('.user-chip').count() rec('登录态持久化(顶部显示昵称)', userChip > 0) // 6. 进文章详情 const firstCard = page.locator('.article-card').first() if (await firstCard.count() > 0) { await firstCard.click() await page.waitForTimeout(1200) const detailTitle = await page.locator('article h1').count() rec('文章详情页渲染', detailTitle > 0) const commentSection = await page.locator('section h2').first().textContent().catch(() => '') rec('评论区渲染', commentSection?.includes('评论'), commentSection) } else { rec('文章详情页渲染', false, '没有可点击的文章') } // 7. 搜索页 await page.goto(`${WEB}/search`, { waitUntil: 'networkidle' }) const searchInput = await page.locator('input[type="search"]').count() rec('搜索页渲染', searchInput > 0) // 8. 我的页面 await page.goto(`${WEB}/mine`, { waitUntil: 'networkidle' }) await page.waitForTimeout(800) const profile = await page.locator('.profile').count() rec('我的页面渲染', profile > 0) // 9. 编辑器(普通用户应看到权限提示,不应崩溃) await page.goto(`${WEB}/editor`, { waitUntil: 'networkidle' }) await page.waitForTimeout(800) const editorBody = await page.locator('body').innerText() rec('编辑器页面可访问(不白屏)', editorBody.length > 50, `${editorBody.length} 字符`) // 10. 控制台错误 const realErrors = consoleErrors.filter((e) => !e.includes('favicon') && !e.includes('404') && !e.includes('403')) rec('无未预期控制台错误', realErrors.length === 0, realErrors.slice(0, 2).join(' | ')) await browser.close() const pass = results.filter((r) => r.ok).length const fail = results.length - pass console.log(`\n${'═'.repeat(44)}`) console.log(` 通过: \x1b[32m${pass}\x1b[0m 失败: \x1b[31m${fail}\x1b[0m`) console.log('═'.repeat(44)) process.exit(fail === 0 ? 0 : 1)