Skip to content
Open
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
63 changes: 54 additions & 9 deletions lib/StagehandContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,24 +179,69 @@ export class StagehandContext {
}

private async handleNewPlaywrightPage(pwPage: PlaywrightPage): Promise<void> {
let stagehandPage = this.pageMap.get(pwPage);
if (!stagehandPage) {
stagehandPage = await this.createStagehandPage(pwPage);
if (pwPage.isClosed()) return;

// Only register close handler once per page
if (!this.pageMap.has(pwPage)) {
pwPage.once("close", () => {
const shPage = this.pageMap.get(pwPage);
if (shPage) {
if (shPage.frameId) this.unregisterFrameId(shPage.frameId);
if (this.activeStagehandPage === shPage) {
for (const p of this.intContext.pages()) {
const sp = this.pageMap.get(p);
if (sp && sp !== shPage) {
this.setActivePage(sp);
break;
}
}
Comment on lines +191 to +197
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: check that candidate page p is not closed before setting as active

Suggested change
for (const p of this.intContext.pages()) {
const sp = this.pageMap.get(p);
if (sp && sp !== shPage) {
this.setActivePage(sp);
break;
}
}
for (const p of this.intContext.pages()) {
if (!p.isClosed()) {
const sp = this.pageMap.get(p);
if (sp && sp !== shPage) {
this.setActivePage(sp);
break;
}
}
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: lib/StagehandContext.ts
Line: 191:197

Comment:
**style:** check that candidate page `p` is not closed before setting as active

```suggestion
            for (const p of this.intContext.pages()) {
              if (!p.isClosed()) {
                const sp = this.pageMap.get(p);
                if (sp && sp !== shPage) {
                  this.setActivePage(sp);
                  break;
                }
              }
            }
```

How can I resolve this? If you propose a fix, please make it concise.

}
}
});
}

try {
let stagehandPage = this.pageMap.get(pwPage);
if (!stagehandPage) {
stagehandPage = await this.createStagehandPage(pwPage);
}
this.setActivePage(stagehandPage);
} catch (err) {
const msg = (err as Error).message ?? "";
if (
msg.includes("No target with given id") ||
msg.includes("Target closed") ||
msg.includes("Target page, context or browser has been closed")
) {
return;
}
throw err;
}
this.setActivePage(stagehandPage);
}

private async attachFrameNavigatedListener(
pwPage: PlaywrightPage,
): Promise<void> {
const shPage = this.pageMap.get(pwPage);
if (!shPage) return;
const session: CDPSession = await this.intContext.newCDPSession(pwPage);
await session.send("Page.enable");

pwPage.once("close", () => {
if (shPage.frameId) this.unregisterFrameId(shPage.frameId);
});
if (pwPage.isClosed()) return;

let session: CDPSession;
try {
session = await this.intContext.newCDPSession(pwPage);
await session.send("Page.enable");
} catch (err) {
const msg = (err as Error).message ?? "";
if (
msg.includes("No target with given id") ||
msg.includes("Target closed") ||
msg.includes("Target page, context or browser has been closed")
) {
return;
}
throw err;
}

session.on(
"Page.frameNavigated",
Expand Down
Loading