Skip to content

Commit

Permalink
Refactor capture_url method to include timeout parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedmamdouh22 committed Jan 17, 2025
1 parent 2493331 commit c15125f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
8 changes: 6 additions & 2 deletions sdk/harambe/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ async def paginate(
return

async def capture_url(
self, clickable: ElementHandle, resource_type: ResourceType = "document"
self,
clickable: ElementHandle,
resource_type: ResourceType = "document",
timeout: Optional[int] = 10,
) -> URL | None:
"""
Capture the url of a click event. This will click the element and return the url
Expand All @@ -224,11 +227,12 @@ async def capture_url(
:param clickable: the element to click
:param resource_type: the type of resource to capture
:param timeout: the time to wait for the new page to open (in seconds)
:return url: the url of the captured resource or None if no match was found
:raises ValueError: if more than one request matches
"""
async with ResourceRequestHandler(
self.page, resource_type=resource_type
self.page, resource_type=resource_type, timeout=timeout
) as handler:
await clickable.click()

Expand Down
11 changes: 7 additions & 4 deletions sdk/harambe/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ def __init__(
self,
page: Page,
resource_type: ResourceType,
timeout: int,
url_pattern: str = "**/*",
):
self.page = page
self.url_pattern = url_pattern
self.resource_type = resource_type

self.timeout = timeout
self._initial_pages = [p.url for p in page.context.pages]
self._new_pages: list[str] = []

Expand All @@ -61,11 +62,13 @@ async def __aexit__(self, *_: Any, **__: Any) -> None:
self._new_pages.append(page.url)
await page.close()
except TimeoutError:
raise TimeoutError("No new page opened within the timeout.")
raise TimeoutError(
f"No new page opened within the {self.timeout} seconds timeout."
)

async def _wait_for_new_page(self, timeout: int = 10) -> Page | None:
async def _wait_for_new_page(self) -> Page | None:
start_time = time.monotonic()
while time.monotonic() - start_time < timeout:
while time.monotonic() - start_time < self.timeout:
current_pages = self.page.context.pages
for page in current_pages:
if page.url not in self._initial_pages:
Expand Down

0 comments on commit c15125f

Please sign in to comment.