Skip to content
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

fix: handle navigation timing in page.evaluate calls #335

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/shortest/src/browser/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,20 @@ export async function click(page: Page, x: number, y: number): Promise<void> {
const scaledY = Math.round(y * scaleRatio.y);

await mouseMove(page, x, y);

// Start navigation promise before clicking
const navigationPromise = page
.waitForNavigation({
waitUntil: "domcontentloaded",
timeout: 5000,
})
.catch(() => {}); // Ignore timeout

await page.mouse.click(scaledX, scaledY);
await showClickAnimation(page, "left");

// Wait for navigation if it started
await navigationPromise;
}

export async function dragMouse(
Expand Down
43 changes: 17 additions & 26 deletions packages/shortest/src/browser/core/browser-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,27 +227,8 @@ export class BrowserTool extends BaseBrowserTool {
await this.clickAtCoordinates(clickCoords[0], clickCoords[1]);
output = `${input.action} at (${clickCoords[0]}, ${clickCoords[1]})`;

// Get initial metadata before potential navigation
// Get metadata after navigation is complete
metadata = await this.getMetadata();

// Wait briefly for navigation to start
await this.page.waitForTimeout(100);

// If navigation started, get updated metadata
if (
await this.page
.evaluate(() => document.readyState !== "complete")
.catch(() => true)
) {
try {
await this.page.waitForLoadState("domcontentloaded", {
timeout: 5000,
});
metadata = await this.getMetadata();
} catch {
// Keep the initial metadata if navigation timeout
}
}
break;
}

Expand Down Expand Up @@ -642,13 +623,23 @@ export class BrowserTool extends BaseBrowserTool {
},
};

// Only try to get cursor position if page is stable
// Only try to get cursor position if page is stable and not navigating
if (await this.isPageStable()) {
const position = await actions.getCursorPosition(this.page);
metadata.cursor_info = {
position,
visible: this.cursorVisible,
};
try {
const position = await actions.getCursorPosition(this.page);
metadata.cursor_info = {
position,
visible: this.cursorVisible,
};
} catch (error: unknown) {
// Ignore cursor errors during navigation
if (
error instanceof Error &&
!error.message.includes("context was destroyed")
) {
throw error;
}
}
}

return metadata;
Expand Down