pages-load.spec.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * L1 冒烟测试:核心前端页面加载
  3. * 使用 Playwright 浏览器打开关键页面,验证无控制台错误
  4. */
  5. import { test, expect } from '@playwright/test';
  6. const FRONTEND_URL = process.env.FRONTEND_URL || 'http://localhost:5173';
  7. /** 核心页面列表(TabBar 页面 + 关键业务页面) */
  8. const CORE_PAGES = [
  9. { name: '首页', path: '/#/pages/index/index' },
  10. { name: '生成页', path: '/#/pages/create/index' },
  11. { name: '书籍生成', path: '/#/pages/book-generator/index' },
  12. { name: '我的', path: '/#/pages/mine/index' },
  13. { name: '历史记录', path: '/#/pages/history/index' },
  14. { name: '播放器', path: '/#/pages/player/index' },
  15. { name: '会员中心', path: '/#/pages/member/index' },
  16. { name: '专辑列表', path: '/#/pages/albums/index' },
  17. ];
  18. test.describe('核心页面加载测试', () => {
  19. for (const pageInfo of CORE_PAGES) {
  20. test(`页面加载: ${pageInfo.name} (${pageInfo.path})`, async ({ page }) => {
  21. // 收集控制台错误
  22. const consoleErrors: string[] = [];
  23. page.on('console', msg => {
  24. if (msg.type() === 'error') {
  25. consoleErrors.push(msg.text());
  26. }
  27. });
  28. // 收集页面异常
  29. const pageErrors: string[] = [];
  30. page.on('pageerror', err => {
  31. pageErrors.push(err.message);
  32. });
  33. // 打开页面
  34. await page.goto(`${FRONTEND_URL}${pageInfo.path}`, {
  35. waitUntil: 'networkidle',
  36. timeout: 30000,
  37. });
  38. // 等待页面渲染
  39. await page.waitForTimeout(2000);
  40. // 验证页面标题存在(uni-app 页面至少有一个根元素)
  41. const appElement = await page.$('#app');
  42. expect(appElement).not.toBeNull();
  43. // 验证无严重 JS 错误
  44. // 注意:一些第三方库(如 uni-app)可能产生非关键警告,这里不视为失败
  45. const criticalErrors = consoleErrors.filter(
  46. e => !e.includes('favicon') && !e.includes('preload') && !e.includes('net::ERR')
  47. );
  48. const totalErrors = [...criticalErrors, ...pageErrors];
  49. if (totalErrors.length > 0) {
  50. console.warn(` ⚠ ${pageInfo.name} 存在 ${totalErrors.length} 个错误:`);
  51. totalErrors.forEach(e => console.warn(` - ${e}`));
  52. }
  53. // 截图保存
  54. await page.screenshot({
  55. path: `test-results/screenshots/smoke-${pageInfo.name}.png`,
  56. fullPage: true,
  57. }).catch(() => {});
  58. });
  59. }
  60. });