|
| 1 | +/** |
| 2 | + * WebGAL E2E Test Utilities |
| 3 | + * |
| 4 | + * Encapsulates browser interaction patterns for WebGAL testing. |
| 5 | + * Extracted from TDD verification scripts. |
| 6 | + */ |
| 7 | +import type { Page } from 'playwright-chromium' |
| 8 | + |
| 9 | +/** |
| 10 | + * Navigate through WebGAL title screen to start the game. |
| 11 | + * |
| 12 | + * Steps: |
| 13 | + * 1. Click the overlay `.html-body__title-enter` |
| 14 | + * 2. Select language (click matching button) |
| 15 | + * 3. Click "开始游戏" (must target inner leaf element) |
| 16 | + */ |
| 17 | +export async function startGame(page: Page, lang = '简体中文'): Promise<void> { |
| 18 | + // Click title overlay |
| 19 | + await page.evaluate(() => { |
| 20 | + const overlay = document.querySelector('.html-body__title-enter') as HTMLElement |
| 21 | + overlay?.click() |
| 22 | + }) |
| 23 | + await page.waitForSelector('div[class*="langSelectButton"]', { state: 'visible' }) |
| 24 | + |
| 25 | + // Select language |
| 26 | + await page.evaluate((lang) => { |
| 27 | + const buttons = document.querySelectorAll('div[class*="langSelectButton"]') |
| 28 | + for (const btn of buttons) { |
| 29 | + if (btn.textContent?.includes(lang)) { |
| 30 | + (btn as HTMLElement).click() |
| 31 | + return |
| 32 | + } |
| 33 | + } |
| 34 | + }, lang) |
| 35 | + await page.waitForFunction(() => |
| 36 | + Array.from(document.querySelectorAll('*')).some( |
| 37 | + (el) => el.textContent?.trim() === '开始游戏' && el.children.length === 0, |
| 38 | + ), |
| 39 | + ) |
| 40 | + |
| 41 | + // Click "开始游戏" — must target leaf element |
| 42 | + await page.evaluate(() => { |
| 43 | + const all = document.querySelectorAll('*') |
| 44 | + for (const el of all) { |
| 45 | + if (el.textContent?.trim() === '开始游戏' && el.children.length === 0) { |
| 46 | + (el as HTMLElement).click() |
| 47 | + return |
| 48 | + } |
| 49 | + } |
| 50 | + }) |
| 51 | + await page.waitForSelector('div[class*="_text_"]', { state: 'visible' }) |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Get current dialog text from the TextBox. |
| 56 | + * WebGAL renders text in 3 layers (original + outer stroke + inner fill), |
| 57 | + * so "Line 1" appears as "LineLineLine 111" in textContent. |
| 58 | + */ |
| 59 | +export async function getText(page: Page): Promise<string> { |
| 60 | + return page.evaluate(() => { |
| 61 | + const el = document.querySelector('div[class*="_text_"]') |
| 62 | + return el?.textContent ?? '' |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Access Redux store via React fiber tree. |
| 68 | + * Store is not on window — it's in module scope, accessible through |
| 69 | + * React's internal fiber tree at depth ~3. |
| 70 | + */ |
| 71 | +export async function getStore(page: Page): Promise<any> { |
| 72 | + return page.evaluate(() => { |
| 73 | + const rootEl = document.getElementById('root') |
| 74 | + if (!rootEl) return null |
| 75 | + |
| 76 | + const fiberKey = Object.keys(rootEl).find((k) => k.startsWith('__react')) |
| 77 | + if (!fiberKey) return null |
| 78 | + |
| 79 | + // BFS through fiber tree to find store |
| 80 | + const queue: any[] = [(rootEl as any)[fiberKey]] |
| 81 | + for (let depth = 0; depth < 10 && queue.length > 0; depth++) { |
| 82 | + const size = queue.length |
| 83 | + for (let i = 0; i < size; i++) { |
| 84 | + const node = queue.shift() |
| 85 | + if (!node) continue |
| 86 | + if (node.memoizedProps?.store) { |
| 87 | + return node.memoizedProps.store.getState() |
| 88 | + } |
| 89 | + if (node.child) queue.push(node.child) |
| 90 | + if (node.sibling) queue.push(node.sibling) |
| 91 | + } |
| 92 | + } |
| 93 | + return null |
| 94 | + }) |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Click to advance dialog. |
| 99 | + */ |
| 100 | +export async function clickToAdvance(page: Page): Promise<void> { |
| 101 | + const before = await getText(page) |
| 102 | + await page.evaluate(() => { |
| 103 | + const textbox = document.querySelector('div[class*="textBox_"]') as HTMLElement |
| 104 | + textbox?.click() |
| 105 | + }) |
| 106 | + await page.waitForFunction( |
| 107 | + (prev) => { |
| 108 | + const el = document.querySelector('div[class*="_text_"]') |
| 109 | + return el?.textContent !== prev |
| 110 | + }, |
| 111 | + before, |
| 112 | + { timeout: 5000 }, |
| 113 | + ).catch(() => {}) |
| 114 | +} |
0 commit comments