-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Align label, strict locator, and URL wait semantics #2895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
miguelg719
wants to merge
16
commits into
evals/consolidation-04-cdp-diagnostics
Choose a base branch
from
evals/consolidation-05-locator-wait-contract
base: evals/consolidation-04-cdp-diagnostics
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2603149
Align label, strict locator, and URL wait semantics
miguelg719 34be62d
Merge branch 'evals/consolidation-04-cdp-diagnostics' into evals/cons…
miguelg719 6f981aa
Fix locator filter semantics and shared action deadlines
miguelg719 b69db32
Merge updated parent into locator contract PR
miguelg719 ae49e55
Advertise URL waiting with the locator implementation
miguelg719 4ed949e
Merge branch 'evals/consolidation-04-cdp-diagnostics' into evals/cons…
miguelg719 12766b0
Give strict locator assertions a stable browser command budget
miguelg719 03ab493
Merge branch 'evals/consolidation-04-cdp-diagnostics' into evals/cons…
miguelg719 11b056a
Merge branch 'evals/consolidation-04-cdp-diagnostics' into evals/cons…
miguelg719 0613e13
Merge branch 'evals/consolidation-04-cdp-diagnostics' into evals/cons…
miguelg719 d5fc21f
Merge commit '05ca9da698f1a6b2823d1c1462e9ff08fdcccefa' into HEAD
miguelg719 49e1f5b
Resolve facade locators across closed shadow roots
miguelg719 66fee7d
Update embedded extension
miguelg719 307df9c
Rebuild extension archive with local dependencies
miguelg719 61e41a5
Harden internal shadow-root evaluation errors and cleanup
miguelg719 16717f6
Rebuild extension archive with local dependencies
miguelg719 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { evaluateWithShadowRoots } from "../understudy/shadowRootEvaluation.js"; | ||
|
|
||
| describe("main-world shadow-root evaluation", () => { | ||
| it("uses ordinary main-world evaluation when there are no author closed roots", async () => { | ||
| const send = vi.fn().mockResolvedValue({ | ||
| root: { backendNodeId: 1, shadowRoots: [{ backendNodeId: 2, shadowRootType: "user-agent" }] }, | ||
| }); | ||
| const evaluate = vi.fn().mockResolvedValue(42); | ||
| await expect( | ||
| evaluateWithShadowRoots({ send } as never, evaluate, "roots => roots.length"), | ||
| ).resolves.toBe(42); | ||
| expect(evaluate).toHaveBeenCalledWith("(roots => roots.length)([])"); | ||
| expect(send).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it.each(["DOM.resolveNode", "Runtime.callFunctionOn"])( | ||
| "releases references after %s fails", | ||
| async (failureMethod) => { | ||
| let resolved = 0; | ||
| const send = vi.fn(async (method: string) => { | ||
| if (method === "DOM.getDocument") | ||
| return { | ||
| root: { | ||
| backendNodeId: 1, | ||
| shadowRoots: [ | ||
| { backendNodeId: 2, shadowRootType: "closed" }, | ||
| { backendNodeId: 3, shadowRootType: "closed" }, | ||
| ], | ||
| }, | ||
| }; | ||
| if (method === "DOM.resolveNode") { | ||
| resolved += 1; | ||
| if (method === failureMethod && resolved === 2) throw new Error("detached"); | ||
| return { object: { objectId: `root-${resolved}` } }; | ||
| } | ||
| if (method === failureMethod) throw new Error("detached"); | ||
| return {}; | ||
| }); | ||
| await expect( | ||
| evaluateWithShadowRoots({ send } as never, vi.fn(), "roots => roots.length"), | ||
| ).rejects.toThrow("detached"); | ||
| expect(send.mock.calls.at(-1)?.[0]).toBe("Runtime.releaseObjectGroup"); | ||
| }, | ||
| ); | ||
| it.each([false, true])( | ||
| "preserves query outcome when cleanup fails (callback throws: %s)", | ||
| async (throws) => { | ||
| const send = vi.fn(async (method: string) => { | ||
| if (method === "DOM.getDocument") | ||
| return { | ||
| root: { | ||
| backendNodeId: 1, | ||
| shadowRoots: [{ backendNodeId: 2, shadowRootType: "closed" }], | ||
| }, | ||
| }; | ||
| if (method === "DOM.resolveNode") return { object: { objectId: "root" } }; | ||
| if (method === "Runtime.releaseObjectGroup") throw new Error("context destroyed"); | ||
| return throws | ||
| ? { exceptionDetails: { text: "sensitive page data" } } | ||
| : { result: { value: 42 } }; | ||
| }); | ||
| const result = evaluateWithShadowRoots({ send } as never, vi.fn(), "roots => roots.length"); | ||
| if (throws) | ||
| await expect(result).rejects.toMatchObject({ | ||
| name: "ShadowRootEvaluationError", | ||
| message: "Shadow-root evaluation failed", | ||
| }); | ||
| else await expect(result).resolves.toBe(42); | ||
| expect(send.mock.calls.at(-1)?.[0]).toBe("Runtime.releaseObjectGroup"); | ||
| }, | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { ShadowRootEvaluationError } from "../errors.js"; | ||
| import type { Protocol } from "devtools-protocol"; | ||
| import type { CDPSessionLike } from "./cdp.js"; | ||
|
|
||
| /** Evaluate in the main world with temporary references to author closed roots. */ | ||
| export async function evaluateWithShadowRoots<Result>( | ||
| session: CDPSessionLike, | ||
| evaluate: (expression: string) => Promise<unknown>, | ||
| functionSource: string, | ||
| ): Promise<Result> { | ||
| const { root } = await session.send<Protocol.DOM.GetDocumentResponse>("DOM.getDocument", { | ||
| depth: -1, | ||
| pierce: true, | ||
| }); | ||
| const closedIds: number[] = []; | ||
| const visit = (node: Protocol.DOM.Node): void => { | ||
| if (node.shadowRootType === "closed") closedIds.push(node.backendNodeId); | ||
| for (const child of node.children ?? []) visit(child); | ||
| for (const shadow of node.shadowRoots ?? []) { | ||
| if (shadow.shadowRootType !== "user-agent") visit(shadow); | ||
| } | ||
| // Frame documents have separate main worlds and are resolved by frame locators. | ||
| }; | ||
| visit(root); | ||
| if (!closedIds.length) return (await evaluate(`(${functionSource})([])`)) as Result; | ||
| const objectGroup = `stagehand-shadow-query-${crypto.randomUUID()}`; | ||
| try { | ||
| const objects: Array<{ objectId: string }> = []; | ||
| for (const backendNodeId of closedIds) { | ||
| const { object } = await session.send<Protocol.DOM.ResolveNodeResponse>("DOM.resolveNode", { | ||
| backendNodeId, | ||
| objectGroup, | ||
| }); | ||
| if (!object.objectId) throw new ShadowRootEvaluationError(); | ||
| objects.push({ objectId: object.objectId }); | ||
| } | ||
| const response = await session.send<Protocol.Runtime.CallFunctionOnResponse>( | ||
| "Runtime.callFunctionOn", | ||
| { | ||
| objectId: objects[0]!.objectId, | ||
| functionDeclaration: `function(...roots) { return (${functionSource})(roots); }`, | ||
| arguments: objects, | ||
| awaitPromise: true, | ||
| returnByValue: true, | ||
| }, | ||
| ); | ||
| if (response.exceptionDetails) throw new ShadowRootEvaluationError(); | ||
| return response.result.value as Result; | ||
| } finally { | ||
| // Navigation can destroy the group before cleanup; preserve the query outcome. | ||
| await session.send("Runtime.releaseObjectGroup", { objectGroup }).catch(() => {}); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.