From e9f82930f7316a136f554d9dd6aaeca8f9206191 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 23 Nov 2026 11:40:08 +0000 Subject: [PATCH 1/5] test(server): preview type sends key events to keydown-driven fields Fails on main: type uses fill/insertText, which dispatch no keydown (#41389). Co-Authored-By: Claude Opus 5.5 --- apps/server/src/preview/ServerBrowserPage.test.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/apps/server/src/preview/ServerBrowserPage.test.ts b/apps/server/src/preview/ServerBrowserPage.test.ts index 9afe924dabc4c..65c0c6afc475f 100644 --- a/apps/server/src/preview/ServerBrowserPage.test.ts +++ b/apps/server/src/preview/ServerBrowserPage.test.ts @@ -410,6 +410,18 @@ describe("server browser element refs", () => { expect(await inner.inputValue()).toBe(""); }); + it("types into a field that builds its value from keydown, with and without clear", async () => { + await page.setContent( + ``, + ); + const keys = (selector: string) => + page.locator(selector).evaluate((field) => (field as unknown as { keys: string }).keys); + await ServerBrowserPage.type(page, { locator: "#a", text: "girona42" }); + expect(await keys("#a")).toBe("girona42"); + await ServerBrowserPage.type(page, { locator: "#b", text: "girona42", clear: true }); + expect(await keys("#b")).toBe("girona42"); + }); + it("shows dotted fields as dots wherever their value appears, keeping text values and refs", async () => { await page.setContent(`
password:
From f5d93f6c6ed67edc1730ced30e1720a0af56a24a Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 23 Nov 2026 11:46:33 +0000 Subject: [PATCH 2/5] fix(server): preview type sends key events to the page preview_act type entered text with insertText, or fill when clearing a target, and neither dispatches keydown or keyup. Pages that build their value from key events saw nothing while the action reported success. Text is now typed as key presses at every length. Line breaks stay inserted text so Enter cannot submit a form. Clearing a target empties it with fill, which follows a label to its control, then types keys; date, time, colour and range inputs keep fill for the value because keys cannot enter it. Co-Authored-By: Claude Fable 5.2 --- .../src/preview/ServerBrowserPage.test.ts | 16 +++++++++- apps/server/src/preview/ServerBrowserPage.ts | 31 +++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/apps/server/src/preview/ServerBrowserPage.test.ts b/apps/server/src/preview/ServerBrowserPage.test.ts index 65c0c6afc475f..14eb8a90d4efe 100644 --- a/apps/server/src/preview/ServerBrowserPage.test.ts +++ b/apps/server/src/preview/ServerBrowserPage.test.ts @@ -420,6 +420,20 @@ describe("server browser element refs", () => { expect(await keys("#a")).toBe("girona42"); await ServerBrowserPage.type(page, { locator: "#b", text: "girona42", clear: true }); expect(await keys("#b")).toBe("girona42"); + await ServerBrowserPage.type(page, { locator: "#b", text: "a".repeat(501), clear: true }); + expect(await keys("#b")).toBe(`girona42${"a".repeat(501)}`); + + await page.setContent( + `
`, + ); + await ServerBrowserPage.type(page, { locator: "text=Email", text: "a@b.c", clear: true }); + expect(await page.locator("#e").inputValue()).toBe("a@b.c"); + await ServerBrowserPage.type(page, { locator: "#d", text: "2026-01-31", clear: true }); + expect(await page.locator("#d").inputValue()).toBe("2026-01-31"); + await ServerBrowserPage.type(page, { locator: "#e", text: "x\ny" }); + await ServerBrowserPage.type(page, { locator: "#t", text: "one\r\ntwo" }); + expect(await page.locator("#t").inputValue()).toBe("one\ntwo"); + expect(await page.locator("#sent").inputValue()).toBe(""); }); it("shows dotted fields as dots wherever their value appears, keeping text values and refs", async () => { @@ -985,7 +999,7 @@ describe("server browser type", () => { ({ locator: () => ({ last: () => ({ count: () => new Promise(() => {}) }) }), evaluate: focusAnswer, - keyboard: { insertText: async (text: string) => void typed.push(text) }, + keyboard: { type: async (text: string) => void typed.push(text) }, }) as unknown as Page; it("types into the focused field when Playwright's utility world never answers", async () => { diff --git a/apps/server/src/preview/ServerBrowserPage.ts b/apps/server/src/preview/ServerBrowserPage.ts index f5eed496da5fe..c803d1a344e53 100644 --- a/apps/server/src/preview/ServerBrowserPage.ts +++ b/apps/server/src/preview/ServerBrowserPage.ts @@ -962,11 +962,38 @@ const focusedTextField = async (page: Page, timeout: number) => { ); }; +// Serialized into the page's main world, so it must not reference module scope. +const focusedValueOnlyInput = (target: FocusCandidate) => { + let element = target.getRootNode().activeElement; + while (element?.shadowRoot?.activeElement) element = element.shadowRoot.activeElement; + return ( + element?.tagName === "INPUT" && + ["color", "date", "datetime-local", "month", "range", "time", "week"].includes( + element.type ?? "", + ) + ); +}; + +// Keys reach pages that build their value from keydown. Line breaks stay text: Enter would submit the form. +const typeKeys = async (page: Page, text: string) => { + for (const [index, part] of text.split(/([\r\n]+)/u).entries()) { + if (index % 2 === 1) await page.keyboard.insertText(part); + else if (part !== "") await page.keyboard.type(part); + } +}; + export const type = async (page: Page, input: PreviewAutomationTypeInput) => { const timeout = input.timeoutMs ?? DEFAULT_TIMEOUT_MS; const locator = targetLocator(page, input); if (locator !== null && input.clear) { - await locator.fill(input.text, { timeout }); + // fill follows a label to its control and leaves that control focused for the keys. + await locator.fill("", { timeout }); + // Pickers take a value such as 2026-01-31 only from fill; keys would enter their segments. + if (await locator.evaluate(focusedValueOnlyInput, undefined, { timeout })) { + await locator.fill(input.text, { timeout }); + return; + } + await typeKeys(page, input.text); return; } const focused = @@ -983,7 +1010,7 @@ export const type = async (page: Page, input: PreviewAutomationTypeInput) => { await page.keyboard.press("ControlOrMeta+A"); await page.keyboard.press("Delete"); } - await page.keyboard.insertText(input.text); + await typeKeys(page, input.text); }; export const hover = async ( From 01cf2d6683da0bb961a305b89857c7bef6b9bdcf Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 23 Nov 2026 11:59:13 +0000 Subject: [PATCH 3/5] fix(server): typing stops at the request deadline Key presses cost about a millisecond each, so long text could run past the request's budget and keep typing after the caller got a timeout. Type now works against one deadline, handed down from the request as for click, sends keys in pieces of 64 characters, and stops with a timeout that says how many characters the field holds. Co-Authored-By: Claude Fable 5.2 --- apps/server/src/preview/ServerBrowser.test.ts | 28 +++++++++++++ apps/server/src/preview/ServerBrowser.ts | 12 ++++-- apps/server/src/preview/ServerBrowserPage.ts | 39 +++++++++++++------ 3 files changed, 64 insertions(+), 15 deletions(-) diff --git a/apps/server/src/preview/ServerBrowser.test.ts b/apps/server/src/preview/ServerBrowser.test.ts index d8e5f0622241c..f0841ca2556d3 100644 --- a/apps/server/src/preview/ServerBrowser.test.ts +++ b/apps/server/src/preview/ServerBrowser.test.ts @@ -1735,6 +1735,34 @@ it.effect( ).pipe(Effect.provide(layer)), ); +it.effect("stops typing long text at the request's deadline and says how far it got", () => + Effect.scoped( + Effect.gen(function* () { + const { broker, tabId } = yield* ready; + const page = contexts[0]!.page; + page.evaluate.mockImplementation(async () => true as never); + let now = 1_700_000_000_000; + const clock = vi.spyOn(Date, "now").mockImplementation(() => now); + yield* Effect.addFinalizer(() => Effect.sync(() => clock.mockRestore())); + // Each piece of keys takes 10 seconds here: over the request's 5, under the 15-second default. + const type = vi.fn(async (_text: string) => void (now += 10_000)); + Object.assign(page, { keyboard: { type } }); + const failure = yield* broker + .invoke({ + scope, + tabId, + operation: "type", + input: { text: "a".repeat(200) }, + timeoutMs: 5_000, + }) + .pipe(Effect.flip); + expect(failure._tag).toBe("PreviewAutomationTimeoutError"); + expect(failure.message).toMatch(/after 64 of 200 characters/); + expect(type.mock.calls).toEqual([["a".repeat(64)]]); + }), + ).pipe(Effect.provide(layer)), +); + it.effect("reports a pending dialog without evaluating the page and resolves it explicitly", () => Effect.scoped( Effect.gen(function* () { diff --git a/apps/server/src/preview/ServerBrowser.ts b/apps/server/src/preview/ServerBrowser.ts index 9fd94232a5e23..8adba6ba3e478 100644 --- a/apps/server/src/preview/ServerBrowser.ts +++ b/apps/server/src/preview/ServerBrowser.ts @@ -2945,10 +2945,14 @@ const make = Effect.gen(function* () { return recordAction(tab, "upload", () => uploadFiles(tab, input as PreviewAutomationUploadInput), ); - case "type": - return recordAction(tab, "type", () => - ServerBrowserPage.type(tab.page, input as PreviewAutomationTypeInput), - ); + case "type": { + // As for click: typing stops at the broker's deadline instead of running on after the reply. + const typeInput = { + ...(input as PreviewAutomationTypeInput), + timeoutMs: Math.max(1, Math.floor(deadlineMs - performance.now())), + }; + return recordAction(tab, "type", () => ServerBrowserPage.type(tab.page, typeInput)); + } case "press": return recordAction(tab, "press", () => ServerBrowserPage.press(tab.page, input as PreviewAutomationPressInput), diff --git a/apps/server/src/preview/ServerBrowserPage.ts b/apps/server/src/preview/ServerBrowserPage.ts index c803d1a344e53..4cf0230b50ad6 100644 --- a/apps/server/src/preview/ServerBrowserPage.ts +++ b/apps/server/src/preview/ServerBrowserPage.ts @@ -975,31 +975,48 @@ const focusedValueOnlyInput = (target: FocusCandidate) => { }; // Keys reach pages that build their value from keydown. Line breaks stay text: Enter would submit the form. -const typeKeys = async (page: Page, text: string) => { +const typeKeys = async (page: Page, text: string, timeoutMs: number, deadline: number) => { + let typed = 0; for (const [index, part] of text.split(/([\r\n]+)/u).entries()) { - if (index % 2 === 1) await page.keyboard.insertText(part); - else if (part !== "") await page.keyboard.type(part); + // Keys cost about a millisecond each, so long text is sent in pieces that stop at the deadline. + const pieces = index % 2 === 1 ? [part] : (part.match(/[^]{1,64}/gu) ?? []); + for (const piece of pieces) { + if (Date.now() >= deadline) { + const reason = `Typing stopped after ${typed} of ${text.length} characters, which the field now holds. Type the rest in another call or raise timeoutMs.`; + throw new ServerBrowserOperationError({ + tag: "PreviewAutomationTimeoutError", + message: reason, + detail: { timeoutMs, reason }, + }); + } + if (index % 2 === 1) await page.keyboard.insertText(piece); + else await page.keyboard.type(piece); + typed += piece.length; + } } }; export const type = async (page: Page, input: PreviewAutomationTypeInput) => { - const timeout = input.timeoutMs ?? DEFAULT_TIMEOUT_MS; + // One deadline across steps keeps the reply inside the broker's grace; Playwright reads 0 as none. + const timeoutMs = input.timeoutMs ?? DEFAULT_TIMEOUT_MS; + const deadline = Date.now() + timeoutMs; + const remaining = () => Math.max(1, deadline - Date.now()); const locator = targetLocator(page, input); if (locator !== null && input.clear) { // fill follows a label to its control and leaves that control focused for the keys. - await locator.fill("", { timeout }); + await locator.fill("", { timeout: remaining() }); // Pickers take a value such as 2026-01-31 only from fill; keys would enter their segments. - if (await locator.evaluate(focusedValueOnlyInput, undefined, { timeout })) { - await locator.fill(input.text, { timeout }); + if (await locator.evaluate(focusedValueOnlyInput, undefined, { timeout: remaining() })) { + await locator.fill(input.text, { timeout: remaining() }); return; } - await typeKeys(page, input.text); + await typeKeys(page, input.text, timeoutMs, deadline); return; } const focused = locator === null - ? await focusedTextField(page, timeout) - : await locator.evaluate(focusTextTarget, undefined, { timeout }); + ? await focusedTextField(page, remaining()) + : await locator.evaluate(focusTextTarget, undefined, { timeout: remaining() }); if (focused !== true) { throw new ServerBrowserOperationError({ tag: "PreviewAutomationTargetNotEditableError", @@ -1010,7 +1027,7 @@ export const type = async (page: Page, input: PreviewAutomationTypeInput) => { await page.keyboard.press("ControlOrMeta+A"); await page.keyboard.press("Delete"); } - await typeKeys(page, input.text); + await typeKeys(page, input.text, timeoutMs, deadline); }; export const hover = async ( From 9e2f76014f3db0e21600a231ce4223e375c977c1 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 23 Nov 2026 12:16:55 +0000 Subject: [PATCH 4/5] fix(server): colour and range inputs keep their value when typed with clear Emptying the target first made Playwright reject colour and range inputs with "Malformed value" before the value was filled. The control is now classified before it is cleared, so pickers and sliders are filled directly as before. Co-Authored-By: Claude Fable 5.2 --- .../src/preview/ServerBrowserPage.test.ts | 6 +++++- apps/server/src/preview/ServerBrowserPage.ts | 18 +++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/apps/server/src/preview/ServerBrowserPage.test.ts b/apps/server/src/preview/ServerBrowserPage.test.ts index 14eb8a90d4efe..67633a63cf70b 100644 --- a/apps/server/src/preview/ServerBrowserPage.test.ts +++ b/apps/server/src/preview/ServerBrowserPage.test.ts @@ -424,12 +424,16 @@ describe("server browser element refs", () => { expect(await keys("#b")).toBe(`girona42${"a".repeat(501)}`); await page.setContent( - `
`, + `
`, ); await ServerBrowserPage.type(page, { locator: "text=Email", text: "a@b.c", clear: true }); expect(await page.locator("#e").inputValue()).toBe("a@b.c"); await ServerBrowserPage.type(page, { locator: "#d", text: "2026-01-31", clear: true }); expect(await page.locator("#d").inputValue()).toBe("2026-01-31"); + await ServerBrowserPage.type(page, { locator: "text=Tint", text: "#ff0000", clear: true }); + expect(await page.locator("#c").inputValue()).toBe("#ff0000"); + await ServerBrowserPage.type(page, { locator: "#r", text: "70", clear: true }); + expect(await page.locator("#r").inputValue()).toBe("70"); await ServerBrowserPage.type(page, { locator: "#e", text: "x\ny" }); await ServerBrowserPage.type(page, { locator: "#t", text: "one\r\ntwo" }); expect(await page.locator("#t").inputValue()).toBe("one\ntwo"); diff --git a/apps/server/src/preview/ServerBrowserPage.ts b/apps/server/src/preview/ServerBrowserPage.ts index 4cf0230b50ad6..0e84152588ca4 100644 --- a/apps/server/src/preview/ServerBrowserPage.ts +++ b/apps/server/src/preview/ServerBrowserPage.ts @@ -963,9 +963,13 @@ const focusedTextField = async (page: Page, timeout: number) => { }; // Serialized into the page's main world, so it must not reference module scope. -const focusedValueOnlyInput = (target: FocusCandidate) => { - let element = target.getRootNode().activeElement; - while (element?.shadowRoot?.activeElement) element = element.shadowRoot.activeElement; +const valueOnlyInput = ( + target: FocusCandidate & { + closest(selector: string): { readonly control: FocusCandidate | null } | null; + }, +) => { + // As fill does, a target that is not a control stands for its label's control. + const element = target.tagName === "INPUT" ? target : target.closest("label")?.control; return ( element?.tagName === "INPUT" && ["color", "date", "datetime-local", "month", "range", "time", "week"].includes( @@ -1003,13 +1007,13 @@ export const type = async (page: Page, input: PreviewAutomationTypeInput) => { const remaining = () => Math.max(1, deadline - Date.now()); const locator = targetLocator(page, input); if (locator !== null && input.clear) { - // fill follows a label to its control and leaves that control focused for the keys. - await locator.fill("", { timeout: remaining() }); - // Pickers take a value such as 2026-01-31 only from fill; keys would enter their segments. - if (await locator.evaluate(focusedValueOnlyInput, undefined, { timeout: remaining() })) { + // Pickers and sliders take a value such as 2026-01-31 only from fill, and some reject an empty one. + if (await locator.evaluate(valueOnlyInput, undefined, { timeout: remaining() })) { await locator.fill(input.text, { timeout: remaining() }); return; } + // fill follows a label to its control and leaves that control focused for the keys. + await locator.fill("", { timeout: remaining() }); await typeKeys(page, input.text, timeoutMs, deadline); return; } From e6f4380a14bf30d99f0a0d8a3cf26e9e5b4d0772 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 23 Nov 2026 12:25:56 +0000 Subject: [PATCH 5/5] fix(server): typing with clear times out when the page stops answering Playwright's timeout on locator.evaluate covers finding the element only, so the check that picks fill or keys could wait past the request deadline on a page that stopped answering. It now races the deadline, with the helper the focused-field check already used. Co-Authored-By: Claude Fable 5.2 --- .../src/preview/ServerBrowserPage.test.ts | 24 +++++++++++++++++++ apps/server/src/preview/ServerBrowserPage.ts | 16 ++++++------- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/apps/server/src/preview/ServerBrowserPage.test.ts b/apps/server/src/preview/ServerBrowserPage.test.ts index 67633a63cf70b..544e4d81d01ab 100644 --- a/apps/server/src/preview/ServerBrowserPage.test.ts +++ b/apps/server/src/preview/ServerBrowserPage.test.ts @@ -1040,6 +1040,30 @@ describe("server browser type", () => { detail: { timeoutMs: 300 }, }); expect(typed).toEqual([]); + + // A target whose element is found but whose page then stops answering. + const fill = vi.fn(async () => {}); + const hung = { + locator: () => ({ evaluate: () => new Promise(() => {}), fill }), + keyboard: { type: async (text: string) => void typed.push(text) }, + } as unknown as Page; + void ServerBrowserPage.type(hung, { + locator: "#field", + text: "hello", + clear: true, + timeoutMs: 300, + }).then( + () => (outcome = "typed"), + (error: unknown) => (outcome = error), + ); + outcome = "pending"; + await vi.advanceTimersByTimeAsync(300); + expect(outcome).toMatchObject({ + tag: "PreviewAutomationTimeoutError", + detail: { timeoutMs: 300 }, + }); + expect(fill).not.toHaveBeenCalled(); + expect(typed).toEqual([]); } finally { vi.useRealTimers(); } diff --git a/apps/server/src/preview/ServerBrowserPage.ts b/apps/server/src/preview/ServerBrowserPage.ts index 0e84152588ca4..a633f4328d163 100644 --- a/apps/server/src/preview/ServerBrowserPage.ts +++ b/apps/server/src/preview/ServerBrowserPage.ts @@ -941,8 +941,8 @@ const focusTextTarget = (target: FocusCandidate | null) => { return active != null && (active === element || element.contains(active)); }; -// A `*:focus` locator needs Playwright's utility world, which some desktop tabs never get. -const focusedTextField = async (page: Page, timeout: number) => { +// Playwright's own timeout covers finding the element, not a page that stops answering the evaluation. +const pageAnswer = async (answer: Promise, timeout: number) => { let timer: ReturnType | undefined; const expired = new Promise((_, reject) => { timer = setTimeout( @@ -950,16 +950,14 @@ const focusedTextField = async (page: Page, timeout: number) => { reject( new ServerBrowserOperationError({ tag: "PreviewAutomationTimeoutError", - message: `The page did not report its focused element within ${timeout}ms, so no text was typed.`, + message: `The page did not answer within ${timeout}ms, so no text was typed.`, detail: { timeoutMs: timeout }, }), ), timeout, ); }); - return await Promise.race([page.evaluate(focusTextTarget, null), expired]).finally(() => - clearTimeout(timer), - ); + return await Promise.race([answer, expired]).finally(() => clearTimeout(timer)); }; // Serialized into the page's main world, so it must not reference module scope. @@ -1008,7 +1006,8 @@ export const type = async (page: Page, input: PreviewAutomationTypeInput) => { const locator = targetLocator(page, input); if (locator !== null && input.clear) { // Pickers and sliders take a value such as 2026-01-31 only from fill, and some reject an empty one. - if (await locator.evaluate(valueOnlyInput, undefined, { timeout: remaining() })) { + const timeout = remaining(); + if (await pageAnswer(locator.evaluate(valueOnlyInput, undefined, { timeout }), timeout)) { await locator.fill(input.text, { timeout: remaining() }); return; } @@ -1019,7 +1018,8 @@ export const type = async (page: Page, input: PreviewAutomationTypeInput) => { } const focused = locator === null - ? await focusedTextField(page, remaining()) + ? // A `*:focus` locator needs Playwright's utility world, which some desktop tabs never get. + await pageAnswer(page.evaluate(focusTextTarget, null), remaining()) : await locator.evaluate(focusTextTarget, undefined, { timeout: remaining() }); if (focused !== true) { throw new ServerBrowserOperationError({