Repository navigation
fix: wait for the locator world when a deep XPath crosses an iframe - #2342
Conversation
🦋 Changeset detectedLatest commit: c4e799f The changes in this PR will be included in the next version bump. This PR includes changesets to release 11 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
This PR is from an external contributor and must be approved by a stagehand team member with write access before CI can run. |
There was a problem hiding this comment.
All reported issues were addressed across 7 files
Confidence score: 5/5
- Safe to merge after the addressed issues were fixed.
Architecture diagram
sequenceDiagram
participant Client as "Test / User"
participant FrameLocator as FrameLocator
participant Page as Page
participant CDPSession as "Child CDPSession"
participant ExecutionContextRegistry as ExecutionContextRegistry
Note over Client,ExecutionContextRegistry: NEW: Event-driven wait for cross-origin iframe main world
Client->>FrameLocator: deepLocator().click()
FrameLocator->>FrameLocator: ensureChildFrameReady(frameId)
loop Retry on OOPIF session change
FrameLocator->>Page: getSessionForFrame(frameId)
Page-->>FrameLocator: session (current owner)
FrameLocator->>CDPSession: Runtime.enable (best-effort)
FrameLocator->>CDPSession: Page.setLifecycleEventsEnabled (best-effort)
FrameLocator->>ExecutionContextRegistry: waitForMainWorld(session, frameId, remaining)
alt Main world already cached
ExecutionContextRegistry-->>FrameLocator: executionContextId (immediate)
else Event received before timeout
par Runtime.executionContextCreated fires
CDPSession-->>ExecutionContextRegistry: event {auxData.frameId, isDefault, context.id}
and Page.lifecycleEvent fires
CDPSession-->>ExecutionContextRegistry: event {frameId, name}
end
ExecutionContextRegistry-->>FrameLocator: executionContextId
else Timeout expires (DEFAULT_MAIN_WORLD_TIMEOUT_MS = 15s via shared constant)
ExecutionContextRegistry->>ExecutionContextRegistry: final cache check
alt Found
ExecutionContextRegistry-->>FrameLocator: executionContextId
else Still missing
ExecutionContextRegistry-->>FrameLocator: reject (main world not ready)
FrameLocator->>FrameLocator: catch() -> wait 50ms, retry loop
end
end
end
FrameLocator-->>Client: resolved
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
Keep trailing iframe XPaths in the parent frame, and block genuine frame transitions on extension/locator-world readiness with OOPIF session rechecks.
Keep OOPIF readiness tied to the current owning session and preserve the actionable locator-world error after matching a child frame.
a56940f to
c4e799f
Compare
f03fde9
into
browserbase:evals/trippyogi-fix-iframe-main-world-loading
…2342) Fixes #2324. > Rebuilt against current v4 `main`. The original implementation in this PR targeted `packages/core/lib/v3/understudy/`, which no longer exists, and waited on the **main world**. v4 fails on the **Stagehand extension/locator world** instead, so this branch was rewritten rather than rebased. ## Why Clicking through a still-loading iframe fails deterministically on v4: ``` Stagehand extension world not ready for frame <id>; checked contexts: none ``` The same click succeeds immediately once the child document commits. The reproduction in #2324 sharpens the original report in two ways: the trigger is **frame descent** rather than the target element, and it is **not cross-origin specific** — a same-site, in-process delayed iframe fails identically. That surfaces two distinct defects: | Case | Selector | Expected | Before | | --- | --- | --- | --- | | A. Selector ends at the iframe | `/html/body/iframe[1]` | Stay in the parent document and target the `<iframe>` element | `resolveDeepXPathTarget` flushed the trailing iframe into a `FrameLocator`, entered the child with `xpath=/`, and needed a child execution context it never required | | B. Selector crosses the iframe | `/html/body/iframe[1]/html/body/button` | Descend, then wait for the child's locator world | `ensureChildFrameReady` waited ~1200ms best-effort for the **main** world and swallowed failures, so evaluation reached the child before the extension world existed | A plain CSS `iframe` click already worked while loading, which is the semantic target for case A. ## What changed **`packages/extension/understudy/deepLocator.ts`** — an iframe step becomes a frame hop only when further selector steps follow it. A trailing `iframe[n]` stays an element target in the parent frame. Hop planning moved into `planDeepXPathTarget()` so the semantics are unit-testable without CDP. **`packages/extension/understudy/frameLocator.ts`** — `ensureChildFrameReady()` now blocks on `executionContexts.waitForLocatorWorld()` instead of the main world: - one bounded `15s` deadline, applied only when crossing a frame; - session ownership is re-resolved per attempt so OOPIF adoption cannot pin the wait to a stale session, and re-verified after a successful wait; - readiness failure throws instead of returning best-effort; - the per-child `try/catch` in `resolveFrame()` was narrowed to the `DOM.getFrameOwner` lookup, so a readiness failure surfaces its real message rather than the generic `Unable to obtain a content frame for selector: ...`. `executionContextRegistry.ts` is untouched, and the generic `1s` `waitForLocatorWorld` call sites are unchanged — after a gated transition that budget is only a short recovery window for context churn, not the loading timeout. ## Test plan New unit coverage: - `packages/extension/tests/deep-locator.test.ts` — trailing, crossing, nested-trailing, and nested-crossing XPath semantics, plus `frame` parity with `iframe`. - `packages/extension/tests/frame-locator.test.ts` — readiness errors propagate out of `resolveFrame()`, and readiness retries against the new session when OOPIF ownership changes mid-attempt. New browser regression, `packages/sdk-ts/tests/integration/iframeLocatorReadiness.test.ts`, registered in the `local/frames-shadow` group. The child document response is delayed while the `<iframe>` element is in the parent DOM immediately: | Case | Same-process | OOPIF (`--site-per-process` + host-resolver rules) | | --- | --- | --- | | Trailing iframe XPath | clicks without waiting for the child document | same | | Deep XPath into the child button | waits for readiness, then the button's server-side counter increments | waits across session adoption, then increments | Case B asserts a real side effect (the child button issues an HTTP request the fixture counts) rather than only elapsed time, so it works for OOPIF without cross-origin DOM inspection. Results locally: extension unit suite passes, all 4 integration cases pass, `oxfmt` and `oxlint` clean, and typecheck reports no errors in the touched files. ## Changeset Extension patch plus the SDKs that embed it; no protocol bump, since there is no wire or API change.
…2342) Fixes #2324. > Rebuilt against current v4 `main`. The original implementation in this PR targeted `packages/core/lib/v3/understudy/`, which no longer exists, and waited on the **main world**. v4 fails on the **Stagehand extension/locator world** instead, so this branch was rewritten rather than rebased. ## Why Clicking through a still-loading iframe fails deterministically on v4: ``` Stagehand extension world not ready for frame <id>; checked contexts: none ``` The same click succeeds immediately once the child document commits. The reproduction in #2324 sharpens the original report in two ways: the trigger is **frame descent** rather than the target element, and it is **not cross-origin specific** — a same-site, in-process delayed iframe fails identically. That surfaces two distinct defects: | Case | Selector | Expected | Before | | --- | --- | --- | --- | | A. Selector ends at the iframe | `/html/body/iframe[1]` | Stay in the parent document and target the `<iframe>` element | `resolveDeepXPathTarget` flushed the trailing iframe into a `FrameLocator`, entered the child with `xpath=/`, and needed a child execution context it never required | | B. Selector crosses the iframe | `/html/body/iframe[1]/html/body/button` | Descend, then wait for the child's locator world | `ensureChildFrameReady` waited ~1200ms best-effort for the **main** world and swallowed failures, so evaluation reached the child before the extension world existed | A plain CSS `iframe` click already worked while loading, which is the semantic target for case A. ## What changed **`packages/extension/understudy/deepLocator.ts`** — an iframe step becomes a frame hop only when further selector steps follow it. A trailing `iframe[n]` stays an element target in the parent frame. Hop planning moved into `planDeepXPathTarget()` so the semantics are unit-testable without CDP. **`packages/extension/understudy/frameLocator.ts`** — `ensureChildFrameReady()` now blocks on `executionContexts.waitForLocatorWorld()` instead of the main world: - one bounded `15s` deadline, applied only when crossing a frame; - session ownership is re-resolved per attempt so OOPIF adoption cannot pin the wait to a stale session, and re-verified after a successful wait; - readiness failure throws instead of returning best-effort; - the per-child `try/catch` in `resolveFrame()` was narrowed to the `DOM.getFrameOwner` lookup, so a readiness failure surfaces its real message rather than the generic `Unable to obtain a content frame for selector: ...`. `executionContextRegistry.ts` is untouched, and the generic `1s` `waitForLocatorWorld` call sites are unchanged — after a gated transition that budget is only a short recovery window for context churn, not the loading timeout. ## Test plan New unit coverage: - `packages/extension/tests/deep-locator.test.ts` — trailing, crossing, nested-trailing, and nested-crossing XPath semantics, plus `frame` parity with `iframe`. - `packages/extension/tests/frame-locator.test.ts` — readiness errors propagate out of `resolveFrame()`, and readiness retries against the new session when OOPIF ownership changes mid-attempt. New browser regression, `packages/sdk-ts/tests/integration/iframeLocatorReadiness.test.ts`, registered in the `local/frames-shadow` group. The child document response is delayed while the `<iframe>` element is in the parent DOM immediately: | Case | Same-process | OOPIF (`--site-per-process` + host-resolver rules) | | --- | --- | --- | | Trailing iframe XPath | clicks without waiting for the child document | same | | Deep XPath into the child button | waits for readiness, then the button's server-side counter increments | waits across session adoption, then increments | Case B asserts a real side effect (the child button issues an HTTP request the fixture counts) rather than only elapsed time, so it works for OOPIF without cross-origin DOM inspection. Results locally: extension unit suite passes, all 4 integration cases pass, `oxfmt` and `oxlint` clean, and typecheck reports no errors in the touched files. ## Changeset Extension patch plus the SDKs that embed it; no protocol bump, since there is no wire or API change.
Fixes #2324.
Why
Clicking through a still-loading iframe fails deterministically on v4:
The same click succeeds immediately once the child document commits. The reproduction in #2324 sharpens the original report in two ways: the trigger is frame descent rather than the target element, and it is not cross-origin specific — a same-site, in-process delayed iframe fails identically.
That surfaces two distinct defects:
/html/body/iframe[1]<iframe>elementresolveDeepXPathTargetflushed the trailing iframe into aFrameLocator, entered the child withxpath=/, and needed a child execution context it never required/html/body/iframe[1]/html/body/buttonensureChildFrameReadywaited ~1200ms best-effort for the main world and swallowed failures, so evaluation reached the child before the extension world existedA plain CSS
iframeclick already worked while loading, which is the semantic target for case A.What changed
packages/extension/understudy/deepLocator.ts— an iframe step becomes a frame hop only when further selector steps follow it. A trailingiframe[n]stays an element target in the parent frame. Hop planning moved intoplanDeepXPathTarget()so the semantics are unit-testable without CDP.packages/extension/understudy/frameLocator.ts—ensureChildFrameReady()now blocks onexecutionContexts.waitForLocatorWorld()instead of the main world:15sdeadline, applied only when crossing a frame;try/catchinresolveFrame()was narrowed to theDOM.getFrameOwnerlookup, so a readiness failure surfaces its real message rather than the genericUnable to obtain a content frame for selector: ....executionContextRegistry.tsis untouched, and the generic1swaitForLocatorWorldcall sites are unchanged — after a gated transition that budget is only a short recovery window for context churn, not the loading timeout.Test plan
New unit coverage:
packages/extension/tests/deep-locator.test.ts— trailing, crossing, nested-trailing, and nested-crossing XPath semantics, plusframeparity withiframe.packages/extension/tests/frame-locator.test.ts— readiness errors propagate out ofresolveFrame(), and readiness retries against the new session when OOPIF ownership changes mid-attempt.New browser regression,
packages/sdk-ts/tests/integration/iframeLocatorReadiness.test.ts, registered in thelocal/frames-shadowgroup. The child document response is delayed while the<iframe>element is in the parent DOM immediately:--site-per-process+ host-resolver rules)Case B asserts a real side effect (the child button issues an HTTP request the fixture counts) rather than only elapsed time, so it works for OOPIF without cross-origin DOM inspection.
Results locally: extension unit suite passes, all 4 integration cases pass,
oxfmtandoxlintclean, and typecheck reports no errors in the touched files.Changeset
Extension patch plus the SDKs that embed it; no protocol bump, since there is no wire or API change.