/** * 真实浏览器驱动测试(图形化环境用):桌面 + 手机关键流程截图、0 报错检查、横向溢出检查、移动端点击升级。 * 用法: * python3 -m http.server 8801 * BASE=http://127.0.0.1:8801/index.html OUT=./tests/shots node tests/ui-shots.mjs * 依赖:本地 playwright(`npm i -D playwright && npx playwright install chromium`),无则跳过并提示。 */ import { createRequire } from 'node:module'; import fs from 'node:fs'; const require = createRequire(import.meta.url); let chromium; try { ({ chromium } = require('playwright')); } catch (e) { console.error('未安装 playwright,跳过 UI 自动化(游戏本体无需此依赖)。'); process.exit(0); } const BASE = process.env.BASE || 'http://127.0.0.1:8801/index.html'; const OUT = process.env.OUT || './tests/shots'; fs.mkdirSync(OUT, { recursive: true }); const browser = await chromium.launch({ executablePath: process.env.CHROME || undefined, args: ['--no-sandbox'] }); const errors = []; const hook = (p) => { p.on('pageerror', (e) => errors.push('pageerror: ' + e.message)); p.on('console', (m) => { if (m.type() === 'error') errors.push('console: ' + m.text()); }); p.on('requestfailed', (r) => errors.push('requestfailed: ' + r.url())); }; // 桌面 1440×900 const page = await browser.newPage({ viewport: { width: 1440, height: 900 } }); hook(page); await page.goto(BASE); await page.waitForTimeout(1000); await page.screenshot({ path: OUT + '/desktop-1-start.png' }); 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(); }); await page.waitForTimeout(800); await page.screenshot({ path: OUT + '/desktop-2-progress.png' }); await page.click('[data-act="rebirth"]'); await page.waitForTimeout(500); await page.screenshot({ path: OUT + '/desktop-3-first-rebirth.png' }); await page.evaluate(() => document.querySelector('[data-mact="close"]')?.click()); // 里世界 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(); }); await page.waitForTimeout(300); await page.evaluate(() => document.querySelector('[data-r="build"]')?.click()); await page.waitForTimeout(300); await page.evaluate(() => window.__wt.rollRift(5)); await page.waitForTimeout(400); await page.screenshot({ path: OUT + '/desktop-4-rift-fight.png' }); await page.evaluate(() => { window.__wt.riftRun.state = 'result'; window.__wt.finishRift(); }); await page.waitForTimeout(500); await page.screenshot({ path: OUT + '/desktop-5-rift-result.png' }); await page.evaluate(() => document.querySelector('[data-mact="close"]')?.click()); // 危机 / 文明 await page.evaluate(() => { window.__wt.s.crisis.next = 0.1; }); await page.waitForTimeout(700); await page.screenshot({ path: OUT + '/desktop-6-crisis.png' }); await page.evaluate(() => document.querySelector('[data-mact="crisis"]')?.click()); await page.evaluate(() => { window.__wt.s.nextCivSec = 0.1; }); await page.waitForTimeout(600); await page.screenshot({ path: OUT + '/desktop-7-civilization.png' }); await page.evaluate(() => document.querySelector('[data-mact="civ"]')?.click()); await page.click('.top-save'); await page.waitForTimeout(400); await page.screenshot({ path: OUT + '/desktop-8-save.png' }); await page.evaluate(() => window.__wt.closeModal()); // 后期 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(); }); await page.waitForTimeout(900); await page.screenshot({ path: OUT + '/desktop-9-late.png' }); // 手机 375×667 const ctx = await browser.newContext({ viewport: { width: 375, height: 667 }, isMobile: true, hasTouch: true, deviceScaleFactor: 2 }); const mp = await ctx.newPage(); hook(mp); await mp.goto(BASE); await mp.waitForTimeout(900); await mp.screenshot({ path: OUT + '/mobile-1-growth.png' }); const of1 = await mp.evaluate(() => ({ sw: document.documentElement.scrollWidth, cw: document.documentElement.clientWidth })); await mp.click('#tabbar button[data-tab="center"]'); await mp.waitForTimeout(800); await mp.screenshot({ path: OUT + '/mobile-2-march.png' }); await mp.click('#tabbar button[data-tab="right"]'); await mp.waitForTimeout(500); await mp.screenshot({ path: OUT + '/mobile-3-rebirth.png' }); 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(); }); await mp.waitForTimeout(300); await mp.screenshot({ path: OUT + '/mobile-4-rift-build.png' }); await mp.evaluate(() => document.querySelector('[data-r="build"]')?.click()); await mp.waitForTimeout(500); await mp.screenshot({ path: OUT + '/mobile-5-rift-fight.png' }); const of2 = await mp.evaluate(() => ({ sw: document.documentElement.scrollWidth, cw: document.documentElement.clientWidth })); await mp.evaluate(() => { const a = window.__wt; a.riftRun.state = 'result'; a.finishRift(); }); await mp.waitForTimeout(400); await mp.screenshot({ path: OUT + '/mobile-6-rift-result.png' }); await mp.evaluate(() => document.querySelector('[data-mact="close"]')?.click()); await mp.evaluate(() => { window.__wt.s.riftCdSec = 0; window.__wt.s.essence = 1000; window.__wt.s.playSec = 200; window.__wt.renderAll(); }); await mp.evaluate(() => window.__wt.showTab('left')); await mp.waitForTimeout(200); const before = await mp.evaluate(() => window.__wt.s.upgrades.root); await mp.tap('#buy-root').catch(() => {}); await mp.waitForTimeout(300); const after = await mp.evaluate(() => window.__wt.s.upgrades.root); console.log('mobile tap upgrade: root', before, '→', after); console.log('overflow', JSON.stringify({ mobile: of1, mobileRift: of2 })); console.log('errors', JSON.stringify(errors.slice(0, 10))); await browser.close();