From 58951fe4e174cbdfea6eca893bf976c1dde8b23b Mon Sep 17 00:00:00 2001 From: Arian Hanifi Date: Thu, 7 Nov 2024 15:52:40 +0100 Subject: [PATCH] fix browser to use active page for get_download --- dendrite/async_api/_core/dendrite_browser.py | 18 ++++++++++++++++-- dendrite/async_api/_core/dendrite_element.py | 5 +++-- dendrite/async_api/_core/dendrite_page.py | 2 +- dendrite/sync_api/_core/dendrite_browser.py | 18 ++++++++++++++++-- dendrite/sync_api/_core/dendrite_element.py | 3 +-- dendrite/sync_api/_core/dendrite_page.py | 2 +- 6 files changed, 38 insertions(+), 10 deletions(-) diff --git a/dendrite/async_api/_core/dendrite_browser.py b/dendrite/async_api/_core/dendrite_browser.py index 594ed22..2b4418a 100644 --- a/dendrite/async_api/_core/dendrite_browser.py +++ b/dendrite/async_api/_core/dendrite_browser.py @@ -376,7 +376,7 @@ async def _get_active_page_manager(self) -> PageManager: return self._active_page_manager - async def get_download(self, pw_page: PlaywrightPage, timeout: float) -> Download: + async def get_download(self, timeout: float) -> Download: """ Retrieves the download event from the browser. @@ -386,7 +386,21 @@ async def get_download(self, pw_page: PlaywrightPage, timeout: float) -> Downloa Raises: Exception: If there is an issue retrieving the download event. """ - return await self._impl.get_download(self, pw_page, timeout) + active_page = await self.get_active_page() + pw_page = active_page.playwright_page + return await self._get_download(pw_page, timeout) + + async def _get_download(self, pw_page: PlaywrightPage, timeout: float) -> Download: + """ + Retrieves the download event from the browser. + + Returns: + Download: The download event. + + Raises: + Exception: If there is an issue retrieving the download event. + """ + return await self._download_handler.get_data(pw_page, timeout=timeout) async def upload_files( self, diff --git a/dendrite/async_api/_core/dendrite_element.py b/dendrite/async_api/_core/dendrite_element.py index c1dd291..e4e4fed 100644 --- a/dendrite/async_api/_core/dendrite_element.py +++ b/dendrite/async_api/_core/dendrite_element.py @@ -183,9 +183,10 @@ async def click( start_time = time.time() await page.playwright_page.wait_for_load_state("load", timeout=2000) wait_duration = time.time() - start_time - print(f"Waited {wait_duration:.2f} seconds for load state") + # print(f"Waited {wait_duration:.2f} seconds for load state") except Exception as e: - print(f"Page navigated but failed to wait for load state: {e}") + pass + # print(f"Page navigated but failed to wait for load state: {e}") return InteractionResponse(status="success", message="") diff --git a/dendrite/async_api/_core/dendrite_page.py b/dendrite/async_api/_core/dendrite_page.py index 54c3606..7d45eeb 100644 --- a/dendrite/async_api/_core/dendrite_page.py +++ b/dendrite/async_api/_core/dendrite_page.py @@ -153,7 +153,7 @@ async def get_download(self, timeout: float = 30000) -> Download: Returns: The downloaded file data. """ - return await self.dendrite_browser.get_download(self.playwright_page, timeout) + return await self.dendrite_browser._get_download(self.playwright_page, timeout) def _get_context(self, element: Any) -> Union[PlaywrightPage, FrameLocator]: """ diff --git a/dendrite/sync_api/_core/dendrite_browser.py b/dendrite/sync_api/_core/dendrite_browser.py index b500f0c..351fa56 100644 --- a/dendrite/sync_api/_core/dendrite_browser.py +++ b/dendrite/sync_api/_core/dendrite_browser.py @@ -339,7 +339,7 @@ def _get_active_page_manager(self) -> PageManager: return active_page_manager return self._active_page_manager - def get_download(self, pw_page: PlaywrightPage, timeout: float) -> Download: + def get_download(self, timeout: float) -> Download: """ Retrieves the download event from the browser. @@ -349,7 +349,21 @@ def get_download(self, pw_page: PlaywrightPage, timeout: float) -> Download: Raises: Exception: If there is an issue retrieving the download event. """ - return self._impl.get_download(self, pw_page, timeout) + active_page = self.get_active_page() + pw_page = active_page.playwright_page + return self._get_download(pw_page, timeout) + + def _get_download(self, pw_page: PlaywrightPage, timeout: float) -> Download: + """ + Retrieves the download event from the browser. + + Returns: + Download: The download event. + + Raises: + Exception: If there is an issue retrieving the download event. + """ + return self._download_handler.get_data(pw_page, timeout=timeout) def upload_files( self, diff --git a/dendrite/sync_api/_core/dendrite_element.py b/dendrite/sync_api/_core/dendrite_element.py index 9cebfba..d73e788 100644 --- a/dendrite/sync_api/_core/dendrite_element.py +++ b/dendrite/sync_api/_core/dendrite_element.py @@ -153,9 +153,8 @@ def click( start_time = time.time() page.playwright_page.wait_for_load_state("load", timeout=2000) wait_duration = time.time() - start_time - print(f"Waited {wait_duration:.2f} seconds for load state") except Exception as e: - print(f"Page navigated but failed to wait for load state: {e}") + pass return InteractionResponse(status="success", message="") @perform_action("fill") diff --git a/dendrite/sync_api/_core/dendrite_page.py b/dendrite/sync_api/_core/dendrite_page.py index 1639c46..b9cd048 100644 --- a/dendrite/sync_api/_core/dendrite_page.py +++ b/dendrite/sync_api/_core/dendrite_page.py @@ -123,7 +123,7 @@ def get_download(self, timeout: float = 30000) -> Download: Returns: The downloaded file data. """ - return self.dendrite_browser.get_download(self.playwright_page, timeout) + return self.dendrite_browser._get_download(self.playwright_page, timeout) def _get_context(self, element: Any) -> Union[PlaywrightPage, FrameLocator]: """