verify-auto-switch.mjs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /** 验证:按屏幕宽度自动进入移动端/桌面端,不再手动切换 */
  2. import { chromium } from 'playwright';
  3. import { mkdirSync } from 'node:fs';
  4. import { resolve } from 'node:path';
  5. import { loadDevLogin } from './dev-login.mjs';
  6. const OUT = resolve('test-results/fix-20261110'); mkdirSync(OUT, { recursive: true });
  7. const login = loadDevLogin();
  8. const b = await chromium.launch({ headless: true, channel: 'chromium' });
  9. async function testAt(name, viewport) {
  10. const ctx = await b.newContext({ viewport, isMobile: viewport.width < 768, hasTouch: viewport.width < 768 });
  11. const p = await ctx.newPage();
  12. await p.addInitScript((d) => {
  13. localStorage.setItem('fmode_session_token', d.token);
  14. localStorage.setItem('fmode_me', JSON.stringify(d.me));
  15. }, login.payload);
  16. // 访问根路径,看自动分流
  17. await p.goto('http://localhost:4200/', { waitUntil: 'domcontentloaded' });
  18. await p.waitForTimeout(5000);
  19. const url = p.url();
  20. const expect = viewport.width < 768 ? '/mobile/' : '/desktop/';
  21. const ok = url.includes(expect);
  22. console.log(`${name} (${viewport.width}px) → ${url.replace('http://localhost:4200', '')} ${ok ? '✅' : '❌ 应为 ' + expect}`);
  23. await p.screenshot({ path: `${OUT}/90-${name}.png` });
  24. await ctx.close();
  25. return ok;
  26. }
  27. const r1 = await testAt('phone', { width: 390, height: 844 });
  28. const r2 = await testAt('tablet', { width: 1024, height: 768 });
  29. const r3 = await testAt('desktop', { width: 1440, height: 900 });
  30. const r4 = await testAt('narrow-window', { width: 600, height: 900 });
  31. console.log('\n全部通过:', r1 && r2 && r3 && r4 ? '✅' : '❌');
  32. await b.close();