ui-shots.mjs 6.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * 真实浏览器驱动测试(图形化环境用):桌面 + 手机关键流程截图、0 报错检查、横向溢出检查、移动端点击升级。
  3. * 用法:
  4. * python3 -m http.server 8801
  5. * BASE=http://127.0.0.1:8801/index.html OUT=./tests/shots node tests/ui-shots.mjs
  6. * 依赖:本地 playwright(`npm i -D playwright && npx playwright install chromium`),无则跳过并提示。
  7. */
  8. import { createRequire } from 'node:module';
  9. import fs from 'node:fs';
  10. const require = createRequire(import.meta.url);
  11. let chromium;
  12. try { ({ chromium } = require('playwright')); }
  13. catch (e) { console.error('未安装 playwright,跳过 UI 自动化(游戏本体无需此依赖)。'); process.exit(0); }
  14. const BASE = process.env.BASE || 'http://127.0.0.1:8801/index.html';
  15. const OUT = process.env.OUT || './tests/shots';
  16. fs.mkdirSync(OUT, { recursive: true });
  17. const browser = await chromium.launch({ executablePath: process.env.CHROME || undefined, args: ['--no-sandbox'] });
  18. const errors = [];
  19. const hook = (p) => {
  20. p.on('pageerror', (e) => errors.push('pageerror: ' + e.message));
  21. p.on('console', (m) => { if (m.type() === 'error') errors.push('console: ' + m.text()); });
  22. p.on('requestfailed', (r) => errors.push('requestfailed: ' + r.url()));
  23. };
  24. // 桌面 1440×900
  25. const page = await browser.newPage({ viewport: { width: 1440, height: 900 } });
  26. hook(page);
  27. await page.goto(BASE); await page.waitForTimeout(1000);
  28. await page.screenshot({ path: OUT + '/desktop-1-start.png' });
  29. await page.evaluate(() => { const w = window.__wt, s = w.s; s.essence = 3e4; s.playSec = 200; s.upgrades = { root: 14, canopy: 5 }; s.lives = { sprout: 22, firefly: 9 }; s.layer = 26; s.maxLayerRound = 26; w.renderAll(); });
  30. await page.waitForTimeout(800);
  31. await page.screenshot({ path: OUT + '/desktop-2-progress.png' });
  32. await page.click('[data-act="rebirth"]'); await page.waitForTimeout(500);
  33. await page.screenshot({ path: OUT + '/desktop-3-first-rebirth.png' });
  34. await page.evaluate(() => document.querySelector('[data-mact="close"]')?.click());
  35. // 里世界
  36. await page.evaluate(() => { const w = window.__wt; w.s.playSec = 100; w.startRift(); w.riftRun.budget = 12; w.riftPanelKey = null; w.renderRiftPanel(); document.querySelector('[data-r="auto"]')?.click(); });
  37. await page.waitForTimeout(300);
  38. await page.evaluate(() => document.querySelector('[data-r="build"]')?.click()); await page.waitForTimeout(300);
  39. await page.evaluate(() => window.__wt.rollRift(5)); await page.waitForTimeout(400);
  40. await page.screenshot({ path: OUT + '/desktop-4-rift-fight.png' });
  41. await page.evaluate(() => { window.__wt.riftRun.state = 'result'; window.__wt.finishRift(); }); await page.waitForTimeout(500);
  42. await page.screenshot({ path: OUT + '/desktop-5-rift-result.png' });
  43. await page.evaluate(() => document.querySelector('[data-mact="close"]')?.click());
  44. // 危机 / 文明
  45. await page.evaluate(() => { window.__wt.s.crisis.next = 0.1; }); await page.waitForTimeout(700);
  46. await page.screenshot({ path: OUT + '/desktop-6-crisis.png' });
  47. await page.evaluate(() => document.querySelector('[data-mact="crisis"]')?.click());
  48. await page.evaluate(() => { window.__wt.s.nextCivSec = 0.1; }); await page.waitForTimeout(600);
  49. await page.screenshot({ path: OUT + '/desktop-7-civilization.png' });
  50. await page.evaluate(() => document.querySelector('[data-mact="civ"]')?.click());
  51. await page.click('.top-save'); await page.waitForTimeout(400);
  52. await page.screenshot({ path: OUT + '/desktop-8-save.png' });
  53. await page.evaluate(() => window.__wt.closeModal());
  54. // 后期
  55. await page.evaluate(() => { const w = window.__wt, s = w.s; s.layer = 812; s.maxLayerRound = 812; s.essence = 1e42; s.upgrades = { root: 160, canopy: 40 }; s.lives = { sprout: 300, firefly: 250, rootwalker: 120, arbor: 90 }; s.treeLv = 24; s.crystals = 1e9; s.rebirths = 88; s.chapterSeen = 5; s.escaped = true; s.escapeCount = 1; s.planes = [{ explore: 100 }, { explore: 100 }, { explore: 73 }, { explore: 12 }]; s.exploreSum = 285; s.currentPlane = 3; s.relics = ['seed', 'star', 'gear']; w.renderLeft(); w.renderRight(); w.renderAll(); });
  56. await page.waitForTimeout(900);
  57. await page.screenshot({ path: OUT + '/desktop-9-late.png' });
  58. // 手机 375×667
  59. const ctx = await browser.newContext({ viewport: { width: 375, height: 667 }, isMobile: true, hasTouch: true, deviceScaleFactor: 2 });
  60. const mp = await ctx.newPage(); hook(mp);
  61. await mp.goto(BASE); await mp.waitForTimeout(900);
  62. await mp.screenshot({ path: OUT + '/mobile-1-growth.png' });
  63. const of1 = await mp.evaluate(() => ({ sw: document.documentElement.scrollWidth, cw: document.documentElement.clientWidth }));
  64. await mp.click('#tabbar button[data-tab="center"]'); await mp.waitForTimeout(800);
  65. await mp.screenshot({ path: OUT + '/mobile-2-march.png' });
  66. await mp.click('#tabbar button[data-tab="right"]'); await mp.waitForTimeout(500);
  67. await mp.screenshot({ path: OUT + '/mobile-3-rebirth.png' });
  68. await mp.evaluate(() => { const w = window.__wt; w.s.playSec = 100; w.s.riftCdSec = 0; w.startRift(); w.riftRun.budget = 12; w.riftPanelKey = null; w.renderRiftPanel(); document.querySelector('[data-r="auto"]')?.click(); });
  69. await mp.waitForTimeout(300); await mp.screenshot({ path: OUT + '/mobile-4-rift-build.png' });
  70. await mp.evaluate(() => document.querySelector('[data-r="build"]')?.click()); await mp.waitForTimeout(500);
  71. await mp.screenshot({ path: OUT + '/mobile-5-rift-fight.png' });
  72. const of2 = await mp.evaluate(() => ({ sw: document.documentElement.scrollWidth, cw: document.documentElement.clientWidth }));
  73. await mp.evaluate(() => { const a = window.__wt; a.riftRun.state = 'result'; a.finishRift(); }); await mp.waitForTimeout(400);
  74. await mp.screenshot({ path: OUT + '/mobile-6-rift-result.png' });
  75. await mp.evaluate(() => document.querySelector('[data-mact="close"]')?.click());
  76. await mp.evaluate(() => { window.__wt.s.riftCdSec = 0; window.__wt.s.essence = 1000; window.__wt.s.playSec = 200; window.__wt.renderAll(); });
  77. await mp.evaluate(() => window.__wt.showTab('left')); await mp.waitForTimeout(200);
  78. const before = await mp.evaluate(() => window.__wt.s.upgrades.root);
  79. await mp.tap('#buy-root').catch(() => {});
  80. await mp.waitForTimeout(300);
  81. const after = await mp.evaluate(() => window.__wt.s.upgrades.root);
  82. console.log('mobile tap upgrade: root', before, '→', after);
  83. console.log('overflow', JSON.stringify({ mobile: of1, mobileRift: of2 }));
  84. console.log('errors', JSON.stringify(errors.slice(0, 10)));
  85. await browser.close();