( context: BrowserContext, extensionId: string, code: string, targetUrl: string, timeoutMs: number )
| 53 | |
| 54 | /** Run inline script code on the target page and collect console results */ |
| 55 | export async function runInlineTestScript( |
| 56 | context: BrowserContext, |
| 57 | extensionId: string, |
| 58 | code: string, |
| 59 | targetUrl: string, |
| 60 | timeoutMs: number |
| 61 | ): Promise<{ passed: number; failed: number; logs: string[] }> { |
| 62 | await installScriptByCode(context, extensionId, code); |
| 63 | autoApprovePermissions(context); |
| 64 | |
| 65 | const page = await context.newPage(); |
| 66 | const logs: string[] = []; |
| 67 | let passed = -1; |
| 68 | let failed = -1; |
| 69 | |
| 70 | page.on("console", (msg) => { |
| 71 | const text = msg.text(); |
| 72 | logs.push(text); |
| 73 | const passMatch = text.match(/通过[::]\s*(\d+)/); |
| 74 | const failMatch = text.match(/失败[::]\s*(\d+)/); |
| 75 | if (passMatch) passed = parseInt(passMatch[1], 10); |
| 76 | if (failMatch) failed = parseInt(failMatch[1], 10); |
| 77 | }); |
| 78 | |
| 79 | await page.goto(targetUrl, { waitUntil: "domcontentloaded" }); |
| 80 | await expect |
| 81 | .poll(() => passed >= 0 && failed >= 0, { timeout: timeoutMs, intervals: [100, 250, 500, 1_000] }) |
| 82 | .toBe(true) |
| 83 | .catch(() => undefined); |
| 84 | |
| 85 | await page.close(); |
| 86 | return { passed, failed, logs }; |
| 87 | } |
| 88 | |
| 89 | /** Open the options page and wait for it to load */ |
| 90 | export async function openOptionsPage(context: BrowserContext, extensionId: string): Promise<Page> { |
no test coverage detected