The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { PreviewAutomationEvaluateInput } from "@t3tools/contracts"; |
| 2 | import * as Schema from "effect/Schema"; |
| 3 | import { |
| 4 | chromium, |
| 5 | type Browser, |
| 6 | type BrowserContext, |
| 7 | type CDPSession, |
| 8 | type Page, |
| 9 | } from "playwright-core"; |
| 10 | import { |
| 11 | afterAll, |
| 12 | afterEach, |
| 13 | beforeAll, |
| 14 | beforeEach, |
| 15 | describe, |
| 16 | expect, |
| 17 | it, |
| 18 | vi, |
| 19 | } from "vite-plus/test"; |
| 20 | |
| 21 | import * as ServerBrowserPage from "./ServerBrowserPage.ts"; |
| 22 | import { SessionControl } from "./SessionControl.ts"; |
| 23 | |
| 24 | const decodeEvaluateInput = Schema.decodeUnknownSync(PreviewAutomationEvaluateInput); |
| 25 | |
| 26 | describe("JPEG screenshot dimensions", () => { |
| 27 | const encode = (bytes: number[]) => Buffer.from(bytes).toString("base64"); |
| 28 | const frame = [0xff, 0xd8, 0xff, 0xc0, 0, 7, 8, 0, 24, 0, 32]; |
| 29 | |
| 30 | it("reads valid dimensions and rejects invalid or incomplete frames", () => { |
| 31 | expect(ServerBrowserPage.jpegSize(encode(frame))).toEqual({ width: 32, height: 24 }); |
| 32 | for (const bytes of [ |
| 33 | [], |
| 34 | [0xff, 0xd8, 0xff, 0xe1, 0, 20, 1], |
| 35 | frame.slice(0, -1), |
| 36 | [...frame.slice(0, 7), 0, 0, 0, 32], |
| 37 | [...frame.slice(0, 9), 0, 0], |
| 38 | ]) { |
| 39 | expect(() => ServerBrowserPage.jpegSize(encode(bytes))).toThrow("Invalid JPEG screenshot."); |
| 40 | } |
| 41 | }); |
| 42 | }); |
| 43 | |
| 44 | describe("snapshot deadline diagnostics", () => { |
| 45 | const never = () => new Promise<never>(() => {}); |
| 46 | const stalledSnapshot = async (options: { |
| 47 | readonly stalled?: "metrics"; |
| 48 | readonly page?: Partial<Record<keyof Page, unknown>>; |
| 49 | readonly advanceMs: number; |
| 50 | }) => { |
| 51 | vi.useFakeTimers(); |
| 52 | try { |
| 53 | const frame = {}; |
| 54 | const page = { |
| 55 | viewportSize: () => ({ width: 1280, height: 800 }), |
| 56 | mainFrame: () => frame, |
| 57 | frames: () => [frame], |
| 58 | on: () => {}, |
| 59 | evaluate: async () => ({ |
| 60 | url: "about:blank", |
| 61 | title: "", |
| 62 | loading: false, |
| 63 | visibleText: "", |
| 64 | interactiveElements: [], |
| 65 | }), |
| 66 | ariaSnapshot: async () => "", |
| 67 | ...options.page, |
| 68 | } as unknown as Page; |
| 69 | const cdp = { |
| 70 | send: async (method: string) => { |
| 71 | if (options.stalled === "metrics") return never(); |
| 72 | if (method === "Page.captureScreenshot") return { data: "" }; |
| 73 | if (method === "Page.getFrameTree") return { frameTree: { frame: { id: "main" } } }; |
| 74 | if (method === "Page.createIsolatedWorld") return { executionContextId: 1 }; |
| 75 | if (method === "Runtime.evaluate") return { result: { value: [] } }; |
| 76 | throw new Error(`Unexpected command: ${method}`); |
| 77 | }, |
| 78 | } as unknown as CDPSession; |
| 79 | const captured = ServerBrowserPage.snapshot({ |
| 80 | page, |
| 81 | cdp, |
| 82 | renderScale: options.stalled === "metrics" ? "display" : 1, |
| 83 | consoleEntries: [], |
| 84 | networkEntries: [], |
| 85 | actionTimeline: [], |
| 86 | }).catch((error: unknown) => error); |
| 87 | await vi.advanceTimersByTimeAsync(options.advanceMs); |
| 88 | return await captured; |
| 89 | } finally { |
| 90 | vi.useRealTimers(); |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | it("identifies a stalled metrics read", async () => { |
| 95 | expect(await stalledSnapshot({ stalled: "metrics", advanceMs: 10_000 })).toMatchObject({ |
| 96 | tag: "PreviewAutomationTimeoutError", |
| 97 | detail: { timeoutMs: 10_000, pendingStages: ["metrics"] }, |
| 98 | }); |
| 99 | }); |
| 100 | |
| 101 | it("keeps an accessibility tree that hit its own timeout", async () => { |
| 102 | const timedOut = Object.assign(new Error("Timeout exceeded"), { name: "TimeoutError" }); |
| 103 | const captured = await stalledSnapshot({ |
| 104 | page: { ariaSnapshot: () => Promise.reject(timedOut) }, |
| 105 | advanceMs: 0, |
| 106 | }); |
| 107 | expect(captured).toMatchObject({ |
| 108 | tag: "PreviewAutomationTimeoutError", |
| 109 | detail: { timeoutMs: 10_000 }, |
| 110 | }); |
| 111 | expect((captured as ServerBrowserPage.ServerBrowserOperationError).detail).toMatchObject({ |
| 112 | pendingStages: expect.arrayContaining(["accessibility tree"]), |
| 113 | }); |
| 114 | }); |
| 115 | |
| 116 | it("omits a timed-out stage after it succeeds on retry", async () => { |
| 117 | let attempts = 0; |
| 118 | const timedOut = Object.assign(new Error("Timeout exceeded"), { name: "TimeoutError" }); |
| 119 | expect( |
| 120 | await stalledSnapshot({ |
| 121 | page: { |
| 122 | evaluate: never, |
| 123 | ariaSnapshot: async () => { |
| 124 | if (attempts++ === 0) throw timedOut; |
| 125 | return ""; |
| 126 | }, |
| 127 | }, |
| 128 | advanceMs: 10_000, |
| 129 | }), |
| 130 | ).toMatchObject({ detail: { timeoutMs: 10_000, pendingStages: ["page state"] } }); |
| 131 | }); |
| 132 | |
| 133 | it.each(["elementHandle", "contentFrame"] as const)( |
| 134 | "identifies stalled iframe %s lookup", |
| 135 | async (stage) => { |
| 136 | const main = {}; |
| 137 | const child = { evaluate: never, parentFrame: () => main }; |
| 138 | const captured = await stalledSnapshot({ |
| 139 | page: { |
| 140 | mainFrame: () => main, |
| 141 | frames: () => [main, child], |
| 142 | context: () => ({ |
| 143 | newCDPSession: () => |
| 144 | Promise.reject(new Error("Only this preview tab's page can be attached")), |
| 145 | }), |
| 146 | ariaSnapshot: async () => "- iframe [ref=f1]:\n - text: inside", |
| 147 | locator: () => ({ |
| 148 | elementHandle: |
| 149 | stage === "elementHandle" ? never : async () => ({ contentFrame: never }), |
| 150 | }), |
| 151 | }, |
| 152 | advanceMs: 10_000, |
| 153 | }); |
| 154 | expect(captured).toMatchObject({ |
| 155 | detail: { timeoutMs: 10_000, pendingStages: expect.arrayContaining(["frames"]) }, |
| 156 | }); |
| 157 | }, |
| 158 | ); |
| 159 | |
| 160 | it("reports a frame that didn't answer as the frame's own timeout", async () => { |
| 161 | const main = {}; |
| 162 | // A desktop tab refuses frame sessions, so the frame is read in its own world. |
| 163 | const child = { evaluate: never, parentFrame: () => main }; |
| 164 | const captured = await stalledSnapshot({ |
| 165 | page: { |
| 166 | mainFrame: () => main, |
| 167 | frames: () => [main, child], |
| 168 | context: () => ({ |
| 169 | newCDPSession: () => |
| 170 | Promise.reject(new Error("Only this preview tab's page can be attached")), |
| 171 | }), |
| 172 | ariaSnapshot: async () => "- iframe [ref=f1]:\n - text: inside", |
| 173 | locator: () => ({ |
| 174 | elementHandle: async () => ({ contentFrame: async () => child, dispose: async () => {} }), |
| 175 | }), |
| 176 | }, |
| 177 | // Both attempts wait out the frame; neither reaches the capture deadline. |
| 178 | advanceMs: 2_000, |
| 179 | }); |
| 180 | expect(captured).toMatchObject({ |
| 181 | tag: "PreviewAutomationTimeoutError", |
| 182 | detail: { timeoutMs: 1_000, pendingStages: ["frames"] }, |
| 183 | }); |
| 184 | }); |
| 185 | }); |
| 186 | |
| 187 | describe("server browser capture deadlines", () => { |
| 188 | it("releases control after an unanswered capture and ignores its late reply", async () => { |
| 189 | vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }); |
| 190 | try { |
| 191 | const captured = Promise.withResolvers<{ data: string }>(); |
| 192 | const send = vi.fn(async (method: string) => |
| 193 | method === "Page.captureScreenshot" |
| 194 | ? captured.promise |
| 195 | : { result: { value: "still responsive" } }, |
| 196 | ); |
| 197 | const cdp = { send } as unknown as CDPSession; |
| 198 | const control = new SessionControl("agent"); |
| 199 | let outcome: unknown; |
| 200 | const capture = control |
| 201 | .agent("agent", () => ServerBrowserPage.captureViewport(cdp, { format: "png" })) |
| 202 | .catch((cause: unknown) => { |
| 203 | outcome = cause; |
| 204 | }); |
| 205 | const evaluation = control.agent("agent", () => |
| 206 | ServerBrowserPage.evaluate(cdp, { expression: "document.title" }, 1_000), |
| 207 | ); |
| 208 | |
| 209 | await vi.advanceTimersByTimeAsync(3_000); |
| 210 | expect(outcome).toMatchObject({ |
| 211 | tag: "PreviewAutomationExecutionError", |
| 212 | message: expect.stringContaining("not producing screenshot frames"), |
| 213 | }); |
| 214 | await capture; |
| 215 | expect(await evaluation).toBe("still responsive"); |
| 216 | expect(vi.getTimerCount()).toBe(0); |
| 217 | |
| 218 | captured.resolve({ data: "late-frame" }); |
| 219 | await captured.promise; |
| 220 | expect(outcome).toBeInstanceOf(ServerBrowserPage.ServerBrowserOperationError); |
| 221 | expect(send.mock.calls.map(([method]) => method)).toEqual([ |
| 222 | "Page.captureScreenshot", |
| 223 | "Runtime.evaluate", |
| 224 | ]); |
| 225 | } finally { |
| 226 | vi.useRealTimers(); |
| 227 | } |
| 228 | }); |
| 229 | }); |
| 230 | |
| 231 | describe("server browser element refs", () => { |
| 232 | let browser: Browser; |
| 233 | let context: BrowserContext; |
| 234 | let page: Page; |
| 235 | let cdp: CDPSession; |
| 236 | |
| 237 | beforeAll(async () => { |
| 238 | browser = await chromium.launch({ headless: true }); |
| 239 | }); |
| 240 | afterAll(async () => { |
| 241 | await browser?.close(); |
| 242 | }); |
| 243 | beforeEach(async () => { |
| 244 | context = await browser.newContext(); |
| 245 | page = await context.newPage(); |
| 246 | cdp = await context.newCDPSession(page); |
| 247 | }); |
| 248 | afterEach(async () => { |
| 249 | await context.close(); |
| 250 | }); |
| 251 | |
| 252 | const takeSnapshot = () => |
| 253 | ServerBrowserPage.snapshot({ |
| 254 | page, |
| 255 | cdp, |
| 256 | renderScale: 1, |
| 257 | consoleEntries: [], |
| 258 | networkEntries: [], |
| 259 | actionTimeline: [], |
| 260 | }); |
| 261 | const locators = (tree: unknown) => { |
| 262 | expect(typeof tree).toBe("string"); |
| 263 | return Array.from( |
| 264 | String(tree).matchAll(/\[ref=([^\]]+)\]/g), |
| 265 | (match) => `aria-ref=${match[1]}`, |
| 266 | ); |
| 267 | }; |
| 268 | const buttonLocator = (tree: unknown, name: string) => { |
| 269 | const line = String(tree) |
| 270 | .split("\n") |
| 271 | .find((line) => line.includes(`button "${name}"`)); |
| 272 | const locator = locators(line)[0]; |
| 273 | expect(locator).toBeDefined(); |
| 274 | return locator!; |
| 275 | }; |
| 276 | const repeatedRows = `<ul>${Array.from({ length: 5 }, (_, i) => `<li>row ${i + 1}<button data-testid="delete-row" onclick="this.parentElement.remove()">delete</button></li>`).join("")}</ul>`; |
| 277 | |
| 278 | it("clicks the fifth repeated delete control without touching row one", async () => { |
| 279 | await page.setContent(repeatedRows); |
| 280 | const result = await takeSnapshot(); |
| 281 | const buttons = String(result.accessibilityTree) |
| 282 | .split("\n") |
| 283 | .filter((line) => line.includes('button "delete"')); |
| 284 | expect(buttons).toHaveLength(5); |
| 285 | await ServerBrowserPage.click(page, { locator: locators(buttons[4])[0]!, timeoutMs: 1_000 }); |
| 286 | expect(await page.locator("li").allTextContents()).toEqual([ |
| 287 | "row 1delete", |
| 288 | "row 2delete", |
| 289 | "row 3delete", |
| 290 | "row 4delete", |
| 291 | ]); |
| 292 | }); |
| 293 | |
| 294 | it("returns from a click once it opens a dialog", async () => { |
| 295 | await page.setContent( |
| 296 | `<button onclick="document.body.dataset.answer = String(confirm('sure?'))">Confirm</button>`, |
| 297 | ); |
| 298 | const dialog = new Promise<import("playwright-core").Dialog>((resolve) => |
| 299 | page.once("dialog", resolve), |
| 300 | ); |
| 301 | await ServerBrowserPage.click(page, { |
| 302 | locator: buttonLocator((await takeSnapshot()).accessibilityTree, "Confirm"), |
| 303 | timeoutMs: 1_000, |
| 304 | }); |
| 305 | await (await dialog).accept(); |
| 306 | await expect.poll(() => page.locator("body").getAttribute("data-answer")).toBe("true"); |
| 307 | }); |
| 308 | |
| 309 | it("rejects ambiguous CSS controls without clicking any row", async () => { |
| 310 | await page.setContent(repeatedRows); |
| 311 | await expect( |
| 312 | ServerBrowserPage.click(page, { |
| 313 | selector: 'button[data-testid="delete-row"]', |
| 314 | timeoutMs: 1_000, |
| 315 | }), |
| 316 | ).rejects.toThrow(/strict mode violation/); |
| 317 | expect(await page.locator("li").count()).toBe(5); |
| 318 | }); |
| 319 | |
| 320 | it("tells a click target that matches nothing from one that never becomes clickable", async () => { |
| 321 | await page.setContent(`<button hidden>hidden</button>`); |
| 322 | const failure = (selector: string) => |
| 323 | ServerBrowserPage.click(page, { selector, timeoutMs: 500 }).then( |
| 324 | () => expect.unreachable(), |
| 325 | ServerBrowserPage.toOperationError, |
| 326 | ); |
| 327 | |
| 328 | const absent = await failure(".definitely-no-such-t3-target"); |
| 329 | expect(absent.tag).toBe("PreviewAutomationTimeoutError"); |
| 330 | expect(absent.message).toBe( |
| 331 | 'The click may have happened and removed its target or navigated; check the page before retrying. No element matches ".definitely-no-such-t3-target" after the timeout; take a fresh snapshot and use its locator.', |
| 332 | ); |
| 333 | expect(absent.cause).toMatchObject({ name: "TimeoutError" }); |
| 334 | const hidden = await failure("button"); |
| 335 | expect(hidden.tag).toBe("PreviewAutomationTimeoutError"); |
| 336 | expect(hidden.message).toBe( |
| 337 | 'The click may have happened; check the page before retrying. "button" still matches an element after the timeout. Playwright last saw: element is not visible.', |
| 338 | ); |
| 339 | }); |
| 340 | |
| 341 | it("gives a late target only the click's remaining time, not a fresh timeout", async () => { |
| 342 | await page.setContent(`<script> |
| 343 | setTimeout(() => document.body.insertAdjacentHTML("beforeend", |
| 344 | '<button id="save" disabled onclick="document.body.dataset.clicked = 1">Save</button>'), 1000); |
| 345 | setTimeout(() => document.querySelector("#save").disabled = false, 2500); |
| 346 | </script>`); |
| 347 | await expect( |
| 348 | ServerBrowserPage.click(page, { selector: "#save", timeoutMs: 2_000 }), |
| 349 | ).rejects.toThrow(); |
| 350 | expect(await page.locator("body").getAttribute("data-clicked")).toBeNull(); |
| 351 | }); |
| 352 | |
| 353 | it("does not retarget a removed ref to a replacement node", async () => { |
| 354 | await page.setContent("<button onclick=\"this.textContent='clicked'\">original</button>"); |
| 355 | const locator = buttonLocator((await takeSnapshot()).accessibilityTree, "original"); |
| 356 | await page.setContent("<button onclick=\"this.textContent='clicked'\">replacement</button>"); |
| 357 | await expect(ServerBrowserPage.click(page, { locator, timeoutMs: 100 })).rejects.toThrow(); |
| 358 | expect(await page.locator("button").textContent()).toBe("replacement"); |
| 359 | }); |
| 360 | |
| 361 | it("targets iframe input refs and preserves the parent form", async () => { |
| 362 | await page.setContent( |
| 363 | '<input aria-label="parent"><iframe srcdoc="<input aria-label=child>"></iframe>', |
| 364 | ); |
| 365 | await page.frameLocator("iframe").getByRole("textbox").waitFor(); |
| 366 | const result = await takeSnapshot(); |
| 367 | const line = String(result.accessibilityTree) |
| 368 | .split("\n") |
| 369 | .find((line) => line.includes('textbox "child"')); |
| 370 | const locator = locators(line)[0]!; |
| 371 | await ServerBrowserPage.type(page, { locator, text: "inside iframe", clear: true }); |
| 372 | expect(await page.frameLocator("iframe").getByRole("textbox").inputValue()).toBe( |
| 373 | "inside iframe", |
| 374 | ); |
| 375 | expect(await page.getByRole("textbox", { name: "parent" }).inputValue()).toBe(""); |
| 376 | }); |
| 377 | |
| 378 | it("types after a click by coordinates into a shadow-root field, and refuses a field inside an editable frame", async () => { |
| 379 | await page.setContent( |
| 380 | `<div id="host"></div><div contenteditable="true"><iframe srcdoc="<input id=inner>"></iframe></div><script>document.getElementById("host").attachShadow({mode:"open"}).innerHTML = "<input id=field>";</script>`, |
| 381 | ); |
| 382 | const field = page.locator("#host #field"); |
| 383 | const box = (await field.boundingBox())!; |
| 384 | await page.mouse.click(box.x + box.width / 2, box.y + box.height / 2); |
| 385 | await ServerBrowserPage.type(page, { text: "shadow" }); |
| 386 | expect(await field.inputValue()).toBe("shadow"); |
| 387 | |
| 388 | // Refused for now: focus inside a frame cannot be checked from the top document. |
| 389 | const inner = page.frameLocator("iframe").locator("#inner"); |
| 390 | await inner.click(); |
| 391 | await expect(ServerBrowserPage.type(page, { text: "frame" })).rejects.toMatchObject({ |
| 392 | tag: "PreviewAutomationTargetNotEditableError", |
| 393 | }); |
| 394 | expect(await inner.inputValue()).toBe(""); |
| 395 | }); |
| 396 | |
| 397 | it("shows dotted fields as dots wherever their value appears, keeping text values and refs", async () => { |
| 398 | await page.setContent(` |
| 399 | <table><tr><td>password:</td><td><input id="pw" type="password" aria-label="Password"></td></tr></table> |
| 400 | <input aria-label="Username" value="maria"> |
| 401 | <input type="password" aria-label='saved" [ref=e2]: x' placeholder="hint" value="saved-pass"> |
| 402 | <input type="password" role="combobox" aria-label="Code" value="role-pass"> |
| 403 | <input aria-label="PIN" style="-webkit-text-security: disc" value="styled-pass"> |
| 404 | <div contenteditable style="-webkit-text-security: disc">editable-pass</div> |
| 405 | <iframe srcdoc="<input type=password aria-label=child value=frame-pass>"></iframe> |
| 406 | <iframe id="blank" src="javascript:false"></iframe> |
| 407 | <p>Order styled-pass-2</p> |
| 408 | <script> |
| 409 | // A frame that never commits a document, filled by its parent. |
| 410 | blank.contentDocument.body.innerHTML = "<input type=password aria-label=blank value=blank-pass>"; |
| 411 | // Page script can't change the value the snapshot sees. |
| 412 | Object.defineProperty(HTMLInputElement.prototype, "value", { get: () => "" }); |
| 413 | </script>`); |
| 414 | await page.frameLocator("iframe[srcdoc]").getByRole("textbox").waitFor(); |
| 415 | await ServerBrowserPage.type(page, { locator: "#pw", text: "typed-pass" }); |
| 416 | const result = await takeSnapshot(); |
| 417 | const tree = String(result.accessibilityTree); |
| 418 | expect(tree).toContain('textbox "blank"'); |
| 419 | expect(tree).not.toMatch( |
| 420 | /typed-pass|saved-pass|role-pass|styled-pass|editable-pass|frame-pass|blank-pass/, |
| 421 | ); |
| 422 | expect(result.visibleText).not.toContain("editable-pass"); |
| 423 | expect(result.visibleText).toContain("Order ••••••••-2"); |
| 424 | expect(tree).toMatch(/textbox "Username" [^\n]*: maria$/m); |
| 425 | const line = tree.split("\n").find((line) => line.includes('textbox "Password"')); |
| 426 | expect(line).toMatch(/: •+$/); |
| 427 | await ServerBrowserPage.type(page, { locator: locators(line)[0]!, text: "next", clear: true }); |
| 428 | expect(await page.locator("#pw").inputValue()).toBe("next"); |
| 429 | }); |
| 430 | |
| 431 | it("masks dotted values in page text next to punctuation, with odd spaces, and at the cut", async () => { |
| 432 | await page.setContent(` |
| 433 | <input type="password" aria-label="PIN" value="123"> |
| 434 | <input type="password" aria-label="Phrase" value="open sesame"> |
| 435 | <p>PIN: 123. Code (123) id 1234</p> |
| 436 | <p>open sesame</p>`); |
| 437 | const text = (await takeSnapshot()).visibleText; |
| 438 | expect(text).toContain("PIN: ••••••••. Code (••••••••) id 1234"); |
| 439 | expect(text).not.toMatch(/open|sesame/); |
| 440 | await page.setContent(` |
| 441 | <p>${"x".repeat(19_995)}long-secret</p> |
| 442 | <input type="password" aria-label="Long" value="long-secret">`); |
| 443 | const cut = (await takeSnapshot()).visibleText; |
| 444 | expect(cut.length).toBe(20_000); |
| 445 | expect(cut.slice(-8)).toBe("xxx•••••"); |
| 446 | }); |
| 447 | |
| 448 | it("shows a data: link or blocked data: navigation by media type only", async () => { |
| 449 | // Chromium keeps raw spaces and quotes in both, so a payload can't be cut at either. |
| 450 | await page.setContent(` |
| 451 | <a download href="data:text/plain;charset="a,b",head PAYLOAD'tail">Key</a> |
| 452 | <a href="https://example.com/data:report,2026">Report</a> |
| 453 | <a href="#">data:text/plain,label</a> |
| 454 | <a href='"data:text/plain,relative'>Relative</a> |
| 455 | <a href=" data:text/plain,PAYLOAD">Spaced</a> |
| 456 | <a href="da	ta:text/plain,PAYLOAD">Tabbed</a> |
| 457 | <a href="data:text/plain,head
PAYLOAD">Separated</a> |
| 458 | <a href="data:PAYLOAD,x">Typeless</a> |
| 459 | <a href="data:text/plain,PAYLOAD">Vertical tab</a> |
| 460 | <a href="data:text/plain,PAYLOAD">Backspace</a> |
| 461 | <a id="controlled">Controlled</a> |
| 462 | <a id="escaped">Escaped</a> |
| 463 | <script> |
| 464 | controlled.href = "\\x01data:text/plain,PAYLOAD"; |
| 465 | escaped.href = "\\x02da\\tta:text/plain,PAYLOAD"; |
| 466 | </script>`); |
| 467 | const tree = String((await takeSnapshot()).accessibilityTree); |
| 468 | expect(tree.match(/\/url: data:text\/plain,…$/gm)).toHaveLength(8); |
| 469 | expect(tree).toContain("/url: data:,…"); |
| 470 | expect(tree).toContain("/url: https://example.com/data:report,2026"); |
| 471 | expect(tree).toMatch(/link "data:text\/plain,label" \[ref=t3-/); |
| 472 | expect(tree).not.toMatch(/PAYLOAD|tail|charset|\u2028/); |
| 473 | expect(tree).toContain('/url: "\\"data:text/plain,relative"'); |
| 474 | }); |
| 475 | |
| 476 | it("redacts a data: link whose scheme a password value would mask", async () => { |
| 477 | await page.setContent(` |
| 478 | <input type="password" aria-label="Scheme" value="data"> |
| 479 | <a download href="data:text/plain,PAYLOAD">Key</a>`); |
| 480 | const tree = String((await takeSnapshot()).accessibilityTree); |
| 481 | expect(tree).toContain("/url: ••••••••:text/plain,…"); |
| 482 | expect(tree).not.toContain("PAYLOAD"); |
| 483 | }); |
| 484 | |
| 485 | it("shows a data: page's own URL by media type only", async () => { |
| 486 | await page.goto("data:text/html,<title>Key</title>PAYLOAD"); |
| 487 | expect((await takeSnapshot()).url).toBe("data:text/html,…"); |
| 488 | }); |
| 489 | |
| 490 | it("rejects refs from another tab", async () => { |
| 491 | await page.setContent("<button>same label</button>"); |
| 492 | const locator = buttonLocator((await takeSnapshot()).accessibilityTree, "same label"); |
| 493 | const other = await context.newPage(); |
| 494 | await other.setContent("<button>same label</button>"); |
| 495 | await expect(ServerBrowserPage.click(other, { locator })).rejects.toThrow(/another tab/); |
| 496 | }); |
| 497 | |
| 498 | it("revokes refs after navigation even when labels are identical", async () => { |
| 499 | await page.goto("data:text/html,<button>continue</button>"); |
| 500 | const locator = buttonLocator((await takeSnapshot()).accessibilityTree, "continue"); |
| 501 | await page.goto("data:text/html,<button>continue</button><p>new document</p>"); |
| 502 | await expect(ServerBrowserPage.click(page, { locator })).rejects.toThrow(/stale/); |
| 503 | }); |
| 504 | |
| 505 | it.each(["aria-ref=e1", " aria-ref=e1", "css=body >> aria-ref=e1"])( |
| 506 | "does not allow native refs to bypass generation validation (%s)", |
| 507 | async (locator) => { |
| 508 | await page.setContent("<button>continue</button>"); |
| 509 | await takeSnapshot(); |
| 510 | await expect(ServerBrowserPage.click(page, { locator })).rejects.toThrow(/stale/); |
| 511 | }, |
| 512 | ); |
| 513 | |
| 514 | it("preserves quoted attribute values containing ref engine text", async () => { |
| 515 | await page.setContent( |
| 516 | '<button data-example=" >> aria-ref=e1" onclick="this.textContent=\'saved\'">save</button>', |
| 517 | ); |
| 518 | await ServerBrowserPage.click(page, { selector: 'button[data-example=" >> aria-ref=e1"]' }); |
| 519 | expect(await page.locator("button").textContent()).toBe("saved"); |
| 520 | }); |
| 521 | |
| 522 | it("evaluates top-level await and returns the last expression's value", async () => { |
| 523 | await page.setContent("<title>Ready</title>"); |
| 524 | const result = await ServerBrowserPage.evaluate( |
| 525 | cdp, |
| 526 | { expression: "await Promise.resolve(); document.title" }, |
| 527 | 5_000, |
| 528 | ); |
| 529 | expect(result).toBe("Ready"); |
| 530 | }); |
| 531 | |
| 532 | it("returns object contents even when a legacy caller asks for a remote reference", async () => { |
| 533 | const input = decodeEvaluateInput({ |
| 534 | expression: "({ answer: 42, nested: { ready: true } })", |
| 535 | returnByValue: false, |
| 536 | }); |
| 537 | const result = await ServerBrowserPage.evaluate(cdp, input, 5_000); |
| 538 | expect(result).toEqual({ answer: 42, nested: { ready: true } }); |
| 539 | }); |
| 540 | |
| 541 | it("still awaits the promise an async function returns", async () => { |
| 542 | const result = await ServerBrowserPage.evaluate( |
| 543 | cdp, |
| 544 | { expression: "(async () => { await 0; return 1 })()" }, |
| 545 | 5_000, |
| 546 | ); |
| 547 | expect(result).toBe(1); |
| 548 | }); |
| 549 | |
| 550 | it("does not rerun an expression whose SyntaxError is thrown at runtime", async () => { |
| 551 | const evaluation = ServerBrowserPage.evaluate( |
| 552 | cdp, |
| 553 | { expression: "window.runs = (window.runs ?? 0) + 1; new Function('await 0')" }, |
| 554 | 5_000, |
| 555 | ); |
| 556 | await expect(evaluation).rejects.toThrow("await is only valid"); |
| 557 | expect(await page.evaluate("window.runs")).toBe(1); |
| 558 | }); |
| 559 | |
| 560 | it("does not rerun an expression whose awaited browser call rejects with a SyntaxError", async () => { |
| 561 | let posts = 0; |
| 562 | await page.route("http://preview.test/**", (route) => { |
| 563 | if (route.request().method() === "POST") posts++; |
| 564 | return route.fulfill({ contentType: "text/html", body: "<title>Ready</title>" }); |
| 565 | }); |
| 566 | await page.goto("http://preview.test/"); |
| 567 | const evaluation = ServerBrowserPage.evaluate( |
| 568 | cdp, |
| 569 | { |
| 570 | expression: |
| 571 | "(async () => { const r = await fetch('/api', { method: 'POST' }); return r.json() })()", |
| 572 | }, |
| 573 | 5_000, |
| 574 | ); |
| 575 | await expect(evaluation).rejects.toThrow("is not valid JSON"); |
| 576 | expect(posts).toBe(1); |
| 577 | }); |
| 578 | |
| 579 | it("returns the result of a top-level await expression that reloads the page", async () => { |
| 580 | await page.route("http://preview.test/", (route) => |
| 581 | route.fulfill({ contentType: "text/html", body: "<title>Ready</title>" }), |
| 582 | ); |
| 583 | await page.goto("http://preview.test/"); |
| 584 | const result = await ServerBrowserPage.evaluate( |
| 585 | cdp, |
| 586 | { expression: "await 0; location.reload(); ({ clicked: true })" }, |
| 587 | 5_000, |
| 588 | ); |
| 589 | expect(result).toEqual({ clicked: true }); |
| 590 | }); |
| 591 | }); |
| 592 | |
| 593 | describe("server browser snapshot screenshot", () => { |
| 594 | let browser: Browser; |
| 595 | let context: BrowserContext; |
| 596 | |
| 597 | beforeAll(async () => { |
| 598 | // ServerBrowserContexts launches with these, and the capture scale depends on them. |
| 599 | browser = await chromium.launch({ |
| 600 | headless: true, |
| 601 | args: ["--disable-gpu", "--force-device-scale-factor=2"], |
| 602 | }); |
| 603 | }); |
| 604 | afterAll(async () => { |
| 605 | await browser?.close(); |
| 606 | }); |
| 607 | beforeEach(async () => { |
| 608 | context = await browser.newContext({ |
| 609 | viewport: { width: 1280, height: 800 }, |
| 610 | deviceScaleFactor: 2, |
| 611 | }); |
| 612 | }); |
| 613 | afterEach(async () => { |
| 614 | await context.close(); |
| 615 | }); |
| 616 | |
| 617 | // Blue on the left half and red on the right, so a mirrored left half shows up on the right. |
| 618 | const halves = (overlay: string) => ` |
| 619 | <style> |
| 620 | body { margin: 0; background: white; } |
| 621 | .half { position: absolute; top: 0; width: 640px; height: 800px; } |
| 622 | .overlay { position: fixed; inset: 0; ${overlay} } |
| 623 | </style> |
| 624 | <div class="half" style="left: 0; background: #2563eb"></div> |
| 625 | <div class="half" style="left: 640px; background: #ef4444"></div> |
| 626 | <div class="overlay"></div>`; |
| 627 | |
| 628 | const screenshotOf = async (html: string) => { |
| 629 | const page = await context.newPage(); |
| 630 | await page.setContent(html); |
| 631 | const input = { |
| 632 | page, |
| 633 | cdp: await context.newCDPSession(page), |
| 634 | renderScale: 2, |
| 635 | fullScreenshot: true, |
| 636 | consoleEntries: [], |
| 637 | networkEntries: [], |
| 638 | actionTimeline: [], |
| 639 | }; |
| 640 | const { screenshot, fullScreenshot } = await ServerBrowserPage.snapshot(input); |
| 641 | // The server has no DOM types, so the decoder runs as page source. |
| 642 | const points = [ |
| 643 | [0.15, 0.75], |
| 644 | [0.85, 0.25], |
| 645 | [0.85, 0.75], |
| 646 | ]; |
| 647 | const pixels = (await page.evaluate(`(async () => { |
| 648 | const bytes = Uint8Array.from(atob(${JSON.stringify(screenshot.data)}), (char) => char.charCodeAt(0)); |
| 649 | const bitmap = await createImageBitmap(new Blob([bytes], { type: "image/png" })); |
| 650 | const canvas = document.createElement("canvas"); |
| 651 | canvas.width = bitmap.width; |
| 652 | canvas.height = bitmap.height; |
| 653 | const context = canvas.getContext("2d"); |
| 654 | context.drawImage(bitmap, 0, 0); |
| 655 | return ${JSON.stringify(points)}.map(([x, y]) => |
| 656 | Array.from(context.getImageData(x * bitmap.width, y * bitmap.height, 1, 1).data), |
| 657 | ); |
| 658 | })()`)) as Array<[number, number, number, number]>; |
| 659 | return { screenshot, fullScreenshot, pixels, input }; |
| 660 | }; |
| 661 | const colorOf = ([red, green, blue]: readonly [number, number, number, number]) => |
| 662 | blue - red > 100 ? "blue" : red - blue > 100 ? "red" : `rgb(${red}, ${green}, ${blue})`; |
| 663 | |
| 664 | it("captures a blurred backdrop-filter page as rendered, not mirrored", async () => { |
| 665 | const { screenshot, pixels } = await screenshotOf(halves("backdrop-filter: blur(4px);")); |
| 666 | |
| 667 | expect({ |
| 668 | size: [screenshot.width, screenshot.height], |
| 669 | colors: pixels.map(colorOf), |
| 670 | }).toEqual({ size: [1280, 800], colors: ["blue", "red", "red"] }); |
| 671 | }); |
| 672 | |
| 673 | it("captures a plain page at the shrunk size with its content in place", async () => { |
| 674 | const { screenshot, fullScreenshot, pixels, input } = await screenshotOf(halves("")); |
| 675 | const metadata = await ServerBrowserPage.snapshotMetadata(input); |
| 676 | |
| 677 | expect({ |
| 678 | size: [screenshot.width, screenshot.height], |
| 679 | fullSize: [fullScreenshot?.width, fullScreenshot?.height], |
| 680 | metadataSize: metadata.screenshot, |
| 681 | colors: pixels.map(colorOf), |
| 682 | }).toEqual({ |
| 683 | size: [1280, 800], |
| 684 | fullSize: [2560, 1600], |
| 685 | metadataSize: { width: 1280, height: 800 }, |
| 686 | colors: ["blue", "red", "red"], |
| 687 | }); |
| 688 | }); |
| 689 | |
| 690 | it("records the destination of a finite entrance animation without waiting on infinite ones", async () => { |
| 691 | const page = await context.newPage(); |
| 692 | await page.setContent(` |
| 693 | <style> |
| 694 | body { margin: 0; background: #2563eb; } |
| 695 | @keyframes enter { from { opacity: 0; } to { opacity: 1; } } |
| 696 | @keyframes pulse { from { opacity: 0.5; } to { opacity: 1; } } |
| 697 | button { animation: pulse 2s infinite; } |
| 698 | #paused { animation: enter 2s paused; } |
| 699 | #overlay { display: none; position: fixed; inset: 0; background: #ef4444; } |
| 700 | #overlay.open { display: block; animation: enter 200ms both; } |
| 701 | </style> |
| 702 | <button onclick="document.querySelector('#overlay').classList.add('open')">Open</button> |
| 703 | <div id="paused"></div> |
| 704 | <div id="overlay"></div> |
| 705 | `); |
| 706 | await page.getByRole("button", { name: "Open" }).click(); |
| 707 | const frame = await ServerBrowserPage.captureFinalRecordingFrame( |
| 708 | await context.newCDPSession(page), |
| 709 | 80, |
| 710 | ); |
| 711 | expect(frame).toBeDefined(); |
| 712 | const pixel = (await page.evaluate(`(async () => { |
| 713 | const bytes = Uint8Array.from(atob(${JSON.stringify(frame)}), char => char.charCodeAt(0)); |
| 714 | const bitmap = await createImageBitmap(new Blob([bytes], { type: "image/jpeg" })); |
| 715 | const canvas = document.createElement("canvas"); |
| 716 | canvas.width = bitmap.width; |
| 717 | canvas.height = bitmap.height; |
| 718 | const context = canvas.getContext("2d"); |
| 719 | context.drawImage(bitmap, 0, 0); |
| 720 | return Array.from(context.getImageData(bitmap.width / 2, bitmap.height / 2, 1, 1).data); |
| 721 | })()`)) as [number, number, number, number]; |
| 722 | expect(colorOf(pixel)).toBe("red"); |
| 723 | }); |
| 724 | |
| 725 | it("shrinks the screenshot of an XML document, whose createElement makes no canvas", async () => { |
| 726 | const page = await context.newPage(); |
| 727 | await page.goto("data:text/xml,<a/>"); |
| 728 | const { screenshot } = await ServerBrowserPage.snapshot({ |
| 729 | page, |
| 730 | cdp: await context.newCDPSession(page), |
| 731 | renderScale: 2, |
| 732 | consoleEntries: [], |
| 733 | networkEntries: [], |
| 734 | actionTimeline: [], |
| 735 | }); |
| 736 | |
| 737 | expect([screenshot.width, screenshot.height]).toEqual([1280, 800]); |
| 738 | }); |
| 739 | }); |
| 740 | |
| 741 | describe("server browser drag", () => { |
| 742 | it("fails the drag, not the server, when it times out while the cursor is still moving", async () => { |
| 743 | const timedOut = new Error("locator.dragTo: Timeout 30000ms exceeded."); |
| 744 | const box = { x: 0, y: 0, width: 20, height: 20 }; |
| 745 | const targetBox = Promise.withResolvers<typeof box>(); |
| 746 | const page = { |
| 747 | locator: (selector: string) => |
| 748 | selector === "#card" |
| 749 | ? { |
| 750 | scrollIntoViewIfNeeded: async () => {}, |
| 751 | boundingBox: async () => box, |
| 752 | dragTo: () => Promise.reject(timedOut), |
| 753 | } |
| 754 | : { boundingBox: () => targetBox.promise }, |
| 755 | } as unknown as Page; |
| 756 | const unobserved: Array<unknown> = []; |
| 757 | const onUnhandled = (reason: unknown) => unobserved.push(reason); |
| 758 | process.on("unhandledRejection", onUnhandled); |
| 759 | try { |
| 760 | let finished = false; |
| 761 | const settled = ServerBrowserPage.drag(page, { source: "#card", target: "#lane" }).then( |
| 762 | () => null, |
| 763 | (error: unknown) => error, |
| 764 | ); |
| 765 | void settled.then(() => (finished = true)); |
| 766 | // Two macrotask turns: Node reports a rejection nobody observed after the first. |
| 767 | for (let turn = 0; turn < 2; turn++) { |
| 768 | // oxlint-disable-next-line t3code/no-real-time-wait-in-tests -- Node emits unhandledRejection only between macrotasks; no promise can mark that point. |
| 769 | await new Promise((resolve) => setImmediate(resolve)); |
| 770 | } |
| 771 | expect(finished).toBe(false); |
| 772 | targetBox.resolve(box); |
| 773 | |
| 774 | expect(await settled).toBe(timedOut); |
| 775 | expect(unobserved).toEqual([]); |
| 776 | } finally { |
| 777 | process.off("unhandledRejection", onUnhandled); |
| 778 | } |
| 779 | }); |
| 780 | |
| 781 | it("moves the mouse after dragstart, so pages that track a drag with mousemove see it", async () => { |
| 782 | const browser = await chromium.launch({ headless: true }); |
| 783 | try { |
| 784 | const page = await browser.newPage(); |
| 785 | await page.setContent(` |
| 786 | <div style="display:flex;gap:40px"> |
| 787 | <div id="source" draggable="true" style="width:200px;height:120px">source</div> |
| 788 | <div id="target" style="width:200px;height:120px">target</div> |
| 789 | </div> |
| 790 | <script> |
| 791 | window.events = []; |
| 792 | let dragging = false; |
| 793 | source.addEventListener("dragstart", (event) => { |
| 794 | event.preventDefault(); |
| 795 | dragging = true; |
| 796 | events.push("dragstart"); |
| 797 | }); |
| 798 | addEventListener("mousemove", () => dragging && events.push("mousemove")); |
| 799 | addEventListener("mouseup", () => events.push("mouseup")); |
| 800 | </script>`); |
| 801 | |
| 802 | await ServerBrowserPage.drag(page, { source: "#source", target: "#target" }); |
| 803 | |
| 804 | const events = (await page.evaluate("window.events")) as Array<string>; |
| 805 | expect(events.slice(0, 2)).toEqual(["dragstart", "mousemove"]); |
| 806 | expect(events.at(-1)).toBe("mouseup"); |
| 807 | } finally { |
| 808 | await browser.close(); |
| 809 | } |
| 810 | }); |
| 811 | }); |
| 812 | |
| 813 | describe("server browser action timeout", () => { |
| 814 | type StepOptions = { readonly timeout?: number }; |
| 815 | it.each([ |
| 816 | { |
| 817 | action: "hover", |
| 818 | act: (page: Page) => ServerBrowserPage.hover(page, { selector: "#a", timeoutMs: 9_000 }), |
| 819 | last: ["#a.hover 1"], |
| 820 | }, |
| 821 | { |
| 822 | action: "drag", |
| 823 | act: (page: Page) => |
| 824 | ServerBrowserPage.drag(page, { source: "#a", target: "#b", timeoutMs: 9_000 }), |
| 825 | last: ["#a.dragTo 1", "#b.boundingBox 1"], |
| 826 | }, |
| 827 | ])("gives the $action's steps one shared timeout", async ({ act, last }) => { |
| 828 | let now = 0; |
| 829 | using _now = vi.spyOn(Date, "now").mockImplementation(() => now); |
| 830 | const steps: Array<string> = []; |
| 831 | const box = { x: 0, y: 0, width: 20, height: 20 }; |
| 832 | const page = { |
| 833 | locator: (selector: string) => { |
| 834 | const record = (step: string, options: StepOptions) => |
| 835 | steps.push(`${selector}.${step} ${options.timeout}`); |
| 836 | return { |
| 837 | // Each step that finds the target takes 5 of the 9 seconds. |
| 838 | scrollIntoViewIfNeeded: async (options: StepOptions) => { |
| 839 | record("scrollIntoViewIfNeeded", options); |
| 840 | now += 5_000; |
| 841 | }, |
| 842 | boundingBox: async (options: StepOptions) => { |
| 843 | record("boundingBox", options); |
| 844 | now += 5_000; |
| 845 | return box; |
| 846 | }, |
| 847 | hover: async (options: StepOptions) => record("hover", options), |
| 848 | dragTo: async (_target: unknown, options: StepOptions) => record("dragTo", options), |
| 849 | }; |
| 850 | }, |
| 851 | } as unknown as Page; |
| 852 | |
| 853 | await act(page); |
| 854 | |
| 855 | expect(steps).toEqual(["#a.scrollIntoViewIfNeeded 9000", "#a.boundingBox 4000", ...last]); |
| 856 | }); |
| 857 | }); |
| 858 | |
| 859 | describe("server browser click", () => { |
| 860 | it("clicks a target the page replaced while it was being scrolled into view", async () => { |
| 861 | let scrolls = 0; |
| 862 | const clicks: Array<unknown> = []; |
| 863 | const page = { |
| 864 | locator: () => ({ |
| 865 | // Playwright resolves the node once; a page re-rendering it detaches that node. |
| 866 | scrollIntoViewIfNeeded: async () => { |
| 867 | if (scrolls++ === 0) { |
| 868 | throw new Error("locator.scrollIntoViewIfNeeded: Element is not attached to the DOM"); |
| 869 | } |
| 870 | }, |
| 871 | boundingBox: async () => ({ x: 0, y: 0, width: 20, height: 20 }), |
| 872 | click: async (options: unknown) => { |
| 873 | clicks.push(options); |
| 874 | }, |
| 875 | count: async () => 1, |
| 876 | }), |
| 877 | once: () => {}, |
| 878 | off: () => {}, |
| 879 | } as unknown as Page; |
| 880 | |
| 881 | await expect(ServerBrowserPage.click(page, { selector: "#save" })).resolves.toEqual({ |
| 882 | x: 10, |
| 883 | y: 10, |
| 884 | }); |
| 885 | expect(clicks).toHaveLength(1); |
| 886 | }); |
| 887 | |
| 888 | it("retries a replaced target only within the action's timeout", async () => { |
| 889 | vi.useFakeTimers(); |
| 890 | try { |
| 891 | const timeouts: Array<number | undefined> = []; |
| 892 | const clicks: Array<unknown> = []; |
| 893 | const detached = new Error( |
| 894 | "locator.scrollIntoViewIfNeeded: Element is not attached to the DOM", |
| 895 | ); |
| 896 | const page = { |
| 897 | locator: () => ({ |
| 898 | scrollIntoViewIfNeeded: async (options?: { timeout?: number }) => { |
| 899 | timeouts.push(options?.timeout); |
| 900 | vi.advanceTimersByTime(60); |
| 901 | throw detached; |
| 902 | }, |
| 903 | click: async (options: unknown) => { |
| 904 | clicks.push(options); |
| 905 | }, |
| 906 | }), |
| 907 | } as unknown as Page; |
| 908 | |
| 909 | await expect( |
| 910 | ServerBrowserPage.click(page, { selector: "#save", timeoutMs: 100 }), |
| 911 | ).rejects.toBe(detached); |
| 912 | expect(timeouts).toEqual([100, 40]); |
| 913 | expect(clicks).toHaveLength(0); |
| 914 | } finally { |
| 915 | vi.useRealTimers(); |
| 916 | } |
| 917 | }); |
| 918 | }); |
| 919 | |
| 920 | describe("server browser evaluate", () => { |
| 921 | it("does not retry a parse error that arrives after the timeout", async () => { |
| 922 | vi.useFakeTimers(); |
| 923 | try { |
| 924 | const classic = Promise.withResolvers<unknown>(); |
| 925 | const send = vi.fn((method: string) => |
| 926 | method === "Runtime.evaluate" ? classic.promise : Promise.resolve({}), |
| 927 | ); |
| 928 | const cdp = { send } as unknown as CDPSession; |
| 929 | const evaluation = ServerBrowserPage.evaluate(cdp, { expression: "await 0; 1" }, 10); |
| 930 | const rejected = expect(evaluation).rejects.toMatchObject({ |
| 931 | tag: "PreviewAutomationTimeoutError", |
| 932 | }); |
| 933 | await vi.advanceTimersByTimeAsync(10); |
| 934 | await rejected; |
| 935 | classic.resolve({ |
| 936 | result: { type: "object" }, |
| 937 | exceptionDetails: { |
| 938 | text: "Uncaught", |
| 939 | scriptId: "4", |
| 940 | exception: { type: "object", className: "SyntaxError" }, |
| 941 | }, |
| 942 | }); |
| 943 | await classic.promise; |
| 944 | expect(send.mock.calls.map(([method]) => method)).toEqual([ |
| 945 | "Runtime.evaluate", |
| 946 | "Runtime.terminateExecution", |
| 947 | ]); |
| 948 | } finally { |
| 949 | vi.useRealTimers(); |
| 950 | } |
| 951 | }); |
| 952 | }); |
| 953 | |
| 954 | describe("server browser type", () => { |
| 955 | // As on a desktop tab whose Playwright utility world never answers. |
| 956 | const stalledPage = (focusAnswer: () => Promise<unknown>, typed: string[]) => |
| 957 | ({ |
| 958 | locator: () => ({ last: () => ({ count: () => new Promise<number>(() => {}) }) }), |
| 959 | evaluate: focusAnswer, |
| 960 | keyboard: { insertText: async (text: string) => void typed.push(text) }, |
| 961 | }) as unknown as Page; |
| 962 | |
| 963 | it("types into the focused field when Playwright's utility world never answers", async () => { |
| 964 | vi.useFakeTimers(); |
| 965 | try { |
| 966 | const typed: string[] = []; |
| 967 | let outcome: unknown = "pending"; |
| 968 | const page = stalledPage(async () => true, typed); |
| 969 | void ServerBrowserPage.type(page, { text: "hello", timeoutMs: 300 }).then( |
| 970 | () => (outcome = "typed"), |
| 971 | (error: unknown) => (outcome = error), |
| 972 | ); |
| 973 | await vi.advanceTimersByTimeAsync(300); |
| 974 | expect(outcome).toBe("typed"); |
| 975 | expect(typed).toEqual(["hello"]); |
| 976 | } finally { |
| 977 | vi.useRealTimers(); |
| 978 | } |
| 979 | }); |
| 980 | |
| 981 | it("stops at its timeout without typing when the page never answers", async () => { |
| 982 | vi.useFakeTimers(); |
| 983 | try { |
| 984 | const typed: string[] = []; |
| 985 | let outcome: unknown = "pending"; |
| 986 | const page = stalledPage(() => new Promise(() => {}), typed); |
| 987 | void ServerBrowserPage.type(page, { text: "hello", timeoutMs: 300 }).then( |
| 988 | () => (outcome = "typed"), |
| 989 | (error: unknown) => (outcome = error), |
| 990 | ); |
| 991 | await vi.advanceTimersByTimeAsync(300); |
| 992 | expect(outcome).toMatchObject({ |
| 993 | tag: "PreviewAutomationTimeoutError", |
| 994 | detail: { timeoutMs: 300 }, |
| 995 | }); |
| 996 | expect(typed).toEqual([]); |
| 997 | } finally { |
| 998 | vi.useRealTimers(); |
| 999 | } |
| 1000 | }); |
| 1001 | }); |
| 1002 | |
| 1003 | describe("server browser unresponsive script", () => { |
| 1004 | it("leaves a page that answers alone", async () => { |
| 1005 | const send = vi.fn(async (_method: string) => ({})); |
| 1006 | await ServerBrowserPage.stopUnresponsiveScript({ send } as unknown as CDPSession); |
| 1007 | expect(send.mock.calls.map(([method]) => method)).toEqual(["Runtime.evaluate"]); |
| 1008 | }); |
| 1009 | }); |
| 1010 | |
| 1011 | describe("server browser wait", () => { |
| 1012 | it("ends at its timeout when a check never answers", async () => { |
| 1013 | // As on a desktop tab whose Playwright utility world never answers. |
| 1014 | const page = { |
| 1015 | locator: () => ({ count: () => new Promise<number>(() => {}) }), |
| 1016 | } as unknown as Page; |
| 1017 | vi.useFakeTimers(); |
| 1018 | try { |
| 1019 | let failure: unknown; |
| 1020 | void ServerBrowserPage.waitFor(page, { selector: "button", timeoutMs: 300 }).catch( |
| 1021 | (error: unknown) => (failure = error), |
| 1022 | ); |
| 1023 | await vi.advanceTimersByTimeAsync(300); |
| 1024 | expect(failure).toMatchObject({ tag: "PreviewAutomationTimeoutError" }); |
| 1025 | } finally { |
| 1026 | vi.useRealTimers(); |
| 1027 | } |
| 1028 | }); |
| 1029 | }); |
| 1030 | |
| 1031 | describe("server browser net errors", () => { |
| 1032 | it.each([ |
| 1033 | ["net::ERR_CONNECTION_REFUSED", "ERR_CONNECTION_REFUSED", -102], |
| 1034 | [ |
| 1035 | "page.goto: net::ERR_HTTP2_PROTOCOL_ERROR at https://localhost:5173/", |
| 1036 | "ERR_HTTP2_PROTOCOL_ERROR", |
| 1037 | -2, |
| 1038 | ], |
| 1039 | ["net::ERR_TLS13_DOWNGRADE_DETECTED", "ERR_TLS13_DOWNGRADE_DETECTED", -2], |
| 1040 | ])("reads the whole error name from %j", (errorText, description, code) => { |
| 1041 | expect(ServerBrowserPage.parseNetError(errorText)).toEqual({ description, code }); |
| 1042 | }); |
| 1043 | }); |