Skip to content

Commit

Permalink
fix(playwright): add various event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
idiotWu committed Jan 25, 2025
1 parent 4134182 commit c606213
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions npiai/core/browser/_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
Page,
FileChooser,
StorageState,
Dialog,
Download,
)

__BROWSER_UTILS_VERSION__ = "0.0.15"
Expand Down Expand Up @@ -114,11 +116,32 @@ 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.ready = True

async def on_dialog(self, dialog: Dialog):
"""
Callback function invoked when a dialog is opened
Args:
dialog: Dialog instance
"""
await dialog.dismiss()

async def on_download(self, download: Download):
"""
Callback function invoked when a download is started
Args:
download: Download instance
"""
await download.cancel()

async def on_filechooser(self, chooser: FileChooser):
"""
Callback function invoked when an input:file is clicked
Expand All @@ -138,6 +161,12 @@ async def on_popup(self, popup: Page):
print(f"popup {popup}")
self.page = popup

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

async def stop(self):
"""
Dispose the chrome tools
Expand Down

0 comments on commit c606213

Please sign in to comment.