(
context: BrowserContext,
extensionId: string,
scriptFile: string,
targetUrl: string,
timeoutMs: number,
options?: { patchCode?: (code: string) => string }
)
| 227 | } |
| 228 | |
| 229 | async function runTestScript( |
| 230 | context: BrowserContext, |
| 231 | extensionId: string, |
| 232 | scriptFile: string, |
| 233 | targetUrl: string, |
| 234 | timeoutMs: number, |
| 235 | options?: { patchCode?: (code: string) => string } |
| 236 | ): Promise<{ passed: number; failed: number; logs: string[] }> { |
| 237 | let code = fs.readFileSync(path.join(__dirname, `../example/tests/${scriptFile}`), "utf-8"); |
| 238 | code = patchScriptCode(code); |
| 239 | code = patchTargetMatchCode(code, targetUrl); |
| 240 | code = options?.patchCode ? options.patchCode(code) : code; |
| 241 | |
| 242 | await installScriptByCode(context, extensionId, code); |
| 243 | autoApprovePermissions(context); |
| 244 | |
| 245 | const page = await context.newPage(); |
| 246 | const logs: string[] = []; |
| 247 | let passed = -1; |
| 248 | let failed = -1; |
| 249 | |
| 250 | page.on("console", (msg) => { |
| 251 | const text = msg.text(); |
| 252 | logs.push(text); |
| 253 | const passMatch = text.match(/(通过|Passed)[::]\s*(\d+)/); |
| 254 | const failMatch = text.match(/(失败|Failed)[::]\s*(\d+)/); |
| 255 | if (passMatch) passed = parseInt(passMatch[2], 10); |
| 256 | if (failMatch) failed = parseInt(failMatch[2], 10); |
| 257 | }); |
| 258 | |
| 259 | await page.goto(targetUrl, { waitUntil: "domcontentloaded" }); |
| 260 | await expect |
| 261 | .poll(() => passed >= 0 && failed >= 0, { timeout: timeoutMs, intervals: [100, 250, 500, 1_000] }) |
| 262 | .toBe(true) |
| 263 | .catch(() => undefined); |
| 264 | |
| 265 | await page.close(); |
| 266 | return { passed, failed, logs }; |
| 267 | } |
| 268 | |
| 269 | test.describe("GM API", () => { |
| 270 | let gmApiMockServer: GMApiMockServer; |
no test coverage detected