Skip to content

Commit

Permalink
chg: Ease exceptions tracking during capture
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafiot committed Nov 9, 2024
1 parent 0edd695 commit 34c06e4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
27 changes: 17 additions & 10 deletions playwrightcapture/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ async def __frame_consent(self, frame: Frame) -> bool:
await frame.get_by_label(label).click(timeout=2000)
break
except (TimeoutError, asyncio.TimeoutError) as e:
self.logger.warning(f'Frame consent timeout: {e}')
self.logger.warning(f'Consent timeout (label {label}) : {e}')

try:
async with timeout(5):
Expand All @@ -742,9 +742,9 @@ async def __frame_consent(self, frame: Frame) -> bool:
await frame.get_by_role("button", name=label).click(timeout=2000)
break
except (TimeoutError, asyncio.TimeoutError) as e:
self.logger.warning(f'Frame consent timeout: {e}')
self.logger.warning(f'Frame consent timeout (button {label}): {e}')
except Exception as e:
self.logger.info(f'Issue with frame consent: {e}')
self.logger.info(f'Issue with consent validation: {e}')
return got_button

async def _move_time_forward(self, page: Page, time: int) -> None:
Expand Down Expand Up @@ -825,12 +825,18 @@ async def store_request(request: Request) -> None:
capturing_sub = False
try:
page = await self.context.new_page()
await page.clock.install()
except Error as e:
self.logger.warning(f'The context is in a broken state: {e}')
self.logger.warning(f'Unable to create new page, the context is in a broken state: {e}')
self.should_retry = True
return to_return

try:
await page.clock.install()
clock_set = True
except Error as e:
self.logger.warning(f'Unable to install the clock: {e}')
clock_set = False

if allow_tracking:
# Add authorization clickthroughs
await self.__dialog_didomi_clickthrough(page)
Expand Down Expand Up @@ -898,8 +904,7 @@ async def store_request(request: Request) -> None:
await page.bring_to_front()
self.logger.debug('Page moved to front.')
except Error as e:
self.logger.warning('Page in a broken state.')
raise e
self.logger.warning(f'Unable to bring the page to the front: {e}.')

# page instrumentation
await self._wait_for_random_timeout(page, 5) # Wait 5 sec after document loaded
Expand Down Expand Up @@ -969,7 +974,8 @@ async def store_request(request: Request) -> None:
self.logger.debug('Got button on main frame')
await self._wait_for_random_timeout(page, 10) # Wait 10 sec after click

await self._move_time_forward(page, 10)
if clock_set:
await self._move_time_forward(page, 10)

if parsed_url.fragment:
# We got a fragment, make sure we go to it and scroll only a little bit.
Expand Down Expand Up @@ -1037,8 +1043,9 @@ async def store_request(request: Request) -> None:
z.writestr(f'{i}_{filename}', file_content)
to_return["downloaded_file"] = mem_zip.getvalue()

# fast forward ~30s
await self._move_time_forward(page, 30)
if clock_set:
# fast forward ~30s
await self._move_time_forward(page, 30)

self.logger.debug('Done with instrumentation, waiting for network idle.')
await self._wait_for_random_timeout(page, 5) # Wait 5 sec after instrumentation
Expand Down
6 changes: 3 additions & 3 deletions poetry.lock

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

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "PlaywrightCapture"
version = "1.27.0"
version = "1.27.1"
description = "A simple library to capture websites using playwright"
authors = ["Raphaël Vinot <[email protected]>"]
license = "BSD-3-Clause"
Expand Down

0 comments on commit 34c06e4

Please sign in to comment.