Skip to content

Commit

Permalink
fix(playwright): attach event listeners when new page created
Browse files Browse the repository at this point in the history
  • Loading branch information
idiotWu committed Jan 25, 2025
1 parent c606213 commit 59ad6d4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
21 changes: 13 additions & 8 deletions npiai/core/browser/_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,17 @@ def block_route(route):
)

self.page = await self.context.new_page()
self.page.on("dialog", self.on_dialog)
self.page.on("download", self.on_download)
self.page.on("filechooser", self.on_filechooser)
self.page.on("popup", self.on_popup)
self.context.on("close", self.on_close)
self.attach_events(self.page)

self.ready = True

def attach_events(self, page: Page):
page.on("dialog", self.on_dialog)
page.on("download", self.on_download)
page.on("filechooser", self.on_filechooser)
page.on("popup", self.on_popup)
page.on("close", self.on_close)

async def on_dialog(self, dialog: Dialog):
"""
Callback function invoked when a dialog is opened
Expand Down Expand Up @@ -158,14 +161,16 @@ async def on_popup(self, popup: Page):
Args:
popup: Page instance
"""
print(f"popup {popup}")
self.page = popup
self.attach_events(popup)

async def on_close(self, ctx: BrowserContext):
async def on_close(self, _):
"""
Callback function invoked when the page is closed
"""
self.page = ctx.pages[-1] if ctx.pages else None
if self.context.pages:
self.page = self.context.pages[-1]
self.attach_events(self.page)

async def stop(self):
"""
Expand Down
12 changes: 6 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 59ad6d4

Please sign in to comment.