_shot.mjs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { chromium } from 'playwright';
  2. const OUT = process.env.SHOT_OUT || 'test-results';
  3. const browser = await chromium.launch({ executablePath: process.env.DEMO_CHROME || 'C:/Program Files/Google/Chrome/Application/chrome.exe' });
  4. async function grab(name, w, h, path, pre) {
  5. const ctx = await browser.newContext({ viewport: { width: w, height: h } });
  6. const p = await ctx.newPage();
  7. const errs = [];
  8. p.on('console', m => { if (m.type() === 'error') errs.push(m.text().slice(0, 160)); });
  9. p.on('pageerror', e => errs.push('PAGEERROR: ' + e.message.slice(0, 160)));
  10. await p.goto('http://localhost:4200' + path, { waitUntil: 'networkidle' });
  11. await p.waitForTimeout(1200);
  12. if (pre) await pre(p);
  13. await p.waitForTimeout(1000);
  14. await p.screenshot({ path: `${OUT}/${name}.png` });
  15. const dim = await p.evaluate(() => ({ sw: document.documentElement.scrollWidth, cw: document.documentElement.clientWidth }));
  16. console.log(`[${name}] ${w}x${h} url=${new URL(p.url()).pathname} scrollW=${dim.sw} clientW=${dim.cw}${dim.sw > dim.cw ? ' ⚠ 横向溢出' : ''}`);
  17. if (errs.length) console.log(` ⚠ 控制台错误 ${errs.length}: ${errs.slice(0,2).join(' || ')}`);
  18. await ctx.close();
  19. }
  20. // 登录流程(演示模式填手机号+验证码)
  21. async function login(p) {
  22. await p.fill('input[type="tel"]', '13800138000');
  23. await p.fill('input[type="text"][maxlength="6"]', '123456');
  24. await p.click('button[type="submit"]');
  25. await p.waitForTimeout(2500);
  26. }
  27. const A = process.argv[2] || 'login';
  28. if (A === 'login') {
  29. await grab('login', 1440, 900, '/login');
  30. await grab('login-m', 390, 844, '/login');
  31. } else if (A === 'desktop') {
  32. await grab('dk-workbench', 1440, 900, '/desktop/workbench', login);
  33. await grab('dk-workbench-wide', 1920, 1080, '/desktop/workbench', login);
  34. await grab('dk-workbench-narrow', 1100, 900, '/desktop/workbench', login);
  35. } else if (A === 'mobile') {
  36. await grab('mb-home', 390, 844, '/mobile/home', login);
  37. }
  38. await browser.close();