MCPcopy Index your code
hub / github.com/scriptscat/scriptcat / runAndCapture

Function runAndCapture

e2e/resource-update.spec.ts:35–72  ·  view source on GitHub ↗

* 打开目标页面并等待脚本输出哨兵 JSON 行。脚本注入相对安装存在异步窗口, * 因此在拿不到结果时重新加载页面重试。

(
  context: BrowserContext,
  targetUrl: string,
  timeoutMs = 20_000
)

Source from the content-addressed store, hash-verified

33 * 因此在拿不到结果时重新加载页面重试。
34 */
35async function runAndCapture(
36 context: BrowserContext,
37 targetUrl: string,
38 timeoutMs = 20_000
39): Promise<{ data: any; logs: string[] }> {
40 const page: Page = await context.newPage();
41 const logs: string[] = [];
42 let resolved: any = null;
43 page.on("console", (msg) => {
44 const text = msg.text();
45 logs.push(text);
46 const idx = text.indexOf(SENTINEL);
47 if (idx >= 0) {
48 try {
49 resolved = JSON.parse(text.slice(idx + SENTINEL.length));
50 } catch {
51 /* ignore partial */
52 }
53 }
54 });
55
56 await page.goto(targetUrl, { waitUntil: "domcontentloaded" });
57 await expect
58 .poll(
59 async () => {
60 if (resolved) return true;
61 // 脚本可能尚未注册完成,重载重试
62 await page.reload({ waitUntil: "domcontentloaded" }).catch(() => {});
63 return !!resolved;
64 },
65 { timeout: timeoutMs, intervals: [500, 1_000, 2_000] }
66 )
67 .toBe(true)
68 .catch(() => undefined);
69 await page.close();
70 if (!resolved) throw new Error(`no sentinel captured from ${targetUrl}\nlogs:\n${logs.join("\n")}`);
71 return { data: resolved, logs };
72}
73
74function selfTestScript(opts: {
75 name: string;

Callers 1

Calls 6

pushMethod · 0.80
closeMethod · 0.65
onMethod · 0.45
textMethod · 0.45
parseMethod · 0.45
sliceMethod · 0.45

Tested by

no test coverage detected