-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add fix for page.url() race when navigation happens as a result of act()
#1795
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
Draft
pirate
wants to merge
4
commits into
main
Choose a base branch
from
act-handler-race-test
base: main
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.
Draft
Changes from all commits
Commits
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
110 changes: 110 additions & 0 deletions
110
packages/core/tests/integration/perform-understudy-navigation-race.spec.ts
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,110 @@ | ||
| import { expect, test } from "@playwright/test"; | ||
| import { V3 } from "../../lib/v3/v3.js"; | ||
| import { performUnderstudyMethod } from "../../lib/v3/handlers/handlerUtils/actHandlerUtils.js"; | ||
| import { closeV3 } from "./testUtils.js"; | ||
| import { v3DynamicTestConfig } from "./v3.dynamic.config.js"; | ||
|
|
||
| async function loadDelayedDocumentNavigationFixture( | ||
| v3: V3, | ||
| delayMs: number, | ||
| ): Promise<{ | ||
| page: ReturnType<V3["context"]["pages"]>[number]; | ||
| sourceUrl: string; | ||
| targetUrl: string; | ||
| }> { | ||
| const page = v3.context.pages()[0]; | ||
| const sourceUrl = "https://example.com/"; | ||
| const targetUrl = `https://example.com/?nav-race=${delayMs}`; | ||
|
|
||
| await page.goto(sourceUrl, { waitUntil: "load" }); | ||
|
|
||
| await page.mainFrame().evaluate( | ||
| ({ delay, nextUrl }) => { | ||
| document.open(); | ||
| document.write(`<!DOCTYPE html> | ||
| <html> | ||
| <body> | ||
| <button id="go" type="button">Go</button> | ||
| </body> | ||
| </html>`); | ||
| document.close(); | ||
|
|
||
| (window as typeof window & { __navScheduled?: boolean }).__navScheduled = | ||
| false; | ||
| document.getElementById("go")?.addEventListener("click", () => { | ||
| ( | ||
| window as typeof window & { __navScheduled?: boolean } | ||
| ).__navScheduled = true; | ||
| setTimeout(() => { | ||
| window.location.assign(nextUrl); | ||
| }, delay); | ||
| }); | ||
| }, | ||
| { delay: delayMs, nextUrl: targetUrl }, | ||
| ); | ||
|
|
||
| return { | ||
| page, | ||
| sourceUrl, | ||
| targetUrl, | ||
| }; | ||
| } | ||
|
|
||
| test.describe("performUnderstudyMethod navigation race", () => { | ||
| let v3: V3; | ||
|
|
||
| test.beforeEach(async () => { | ||
| v3 = new V3(v3DynamicTestConfig); | ||
| await v3.init(); | ||
| }); | ||
|
|
||
| test.afterEach(async () => { | ||
| await closeV3(v3); | ||
| }); | ||
|
|
||
| test("waits for navigation that starts within 400ms of click", async () => { | ||
| const { page, targetUrl } = await loadDelayedDocumentNavigationFixture( | ||
| v3, | ||
| 250, | ||
| ); | ||
|
|
||
| await performUnderstudyMethod( | ||
| page, | ||
| page.mainFrame(), | ||
| "click", | ||
| "xpath=/html/body/button", | ||
| [], | ||
| 3_000, | ||
| ); | ||
|
|
||
| expect(page.url()).toBe(targetUrl); | ||
| }); | ||
|
|
||
| test("does not wait for navigation that starts after 400ms of click", async () => { | ||
| const { page, sourceUrl, targetUrl } = | ||
| await loadDelayedDocumentNavigationFixture(v3, 900); | ||
|
|
||
| await performUnderstudyMethod( | ||
| page, | ||
| page.mainFrame(), | ||
| "click", | ||
| "xpath=/html/body/button", | ||
| [], | ||
| 3_000, | ||
| ); | ||
|
|
||
| expect(page.url()).toBe(sourceUrl); | ||
| expect( | ||
| await page.evaluate(() => { | ||
| return (window as typeof window & { __navScheduled?: boolean }) | ||
| .__navScheduled; | ||
| }), | ||
| ).toBe(true); | ||
|
|
||
| await expect | ||
| .poll(() => page.url(), { | ||
| timeout: 3_000, | ||
| }) | ||
| .toBe(targetUrl); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unconditional 400ms latency on every click/doubleClick
navWatcher.wait(400)always blocks for up to 400ms after everyclickordoubleClickaction, even when the click does not trigger any navigation. The timer inwait()only resolves early if aPage.frameNavigatedorPage.navigatedWithinDocumentevent fires within the window — if neither fires, the 400ms timeout runs to completion before returningfalse.In agentic workflows that perform many clicks (selecting options, toggling checkboxes, closing modals, etc.) this adds up to 400ms of dead wait per non-navigating click. A task with 20 clicks would incur up to 8 seconds of pure idle time.
Consider whether the window should start from when the click handler is dispatched rather than when
wait()is called, or whether a shorter window (e.g. 150–200ms) would be enough to catch the typical "JS deferred with setTimeout" pattern while reducing the worst-case overhead on non-navigating clicks.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ALTERNATIVE IDEA (which I prefer) would be add an arg to
act({..., expectNavigation: true | false})and only wait for nav iftrueis passed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a much better approach. The explicit
expectNavigationflag avoids the entire detection problem — no 400ms timeout penalty on non-navigating clicks, and the caller already knows whether they expect navigation (e.g., clicking a submit button vs toggling a checkbox).The automatic detection forces a trade-off between latency (shorter window = faster but might miss slow navigations) and reliability (longer window = catches more cases but adds overhead everywhere). An explicit flag sidesteps this entirely and makes the behavior predictable.
The only downside is slightly more verbose call sites when navigation is expected, but that's a small price for eliminating 8+ seconds of idle time across a multi-click task.