feat(browser sessions): Collect XHR requests done while loading the page

Solves #159
This commit is contained in:
Karim shoair
2026-03-29 22:53:54 +02:00
parent 0f6dcccf5f
commit 68f7c5c36f
7 changed files with 135 additions and 36 deletions
+33 -4
View File
@@ -1,4 +1,5 @@
from time import time
from re import search as re_search
from asyncio import sleep as asyncio_sleep, Lock
from contextlib import contextmanager, asynccontextmanager
@@ -146,11 +147,18 @@ class SyncSession:
self._wait_for_networkidle(page)
@staticmethod
def _create_response_handler(page_info: PageInfo[Page], response_container: List) -> Callable:
"""Create a response handler that captures the final navigation response.
def _create_response_handler(
page_info: PageInfo[Page],
response_container: List,
xhr_pattern: Optional[str] = None,
xhr_container: Optional[List] = None,
) -> Callable:
"""Create a response handler that captures the final navigation response and optionally XHR/fetch responses.
:param page_info: The PageInfo object containing the page
:param response_container: A list to store the final response (mutable container)
:param xhr_pattern: Optional regex pattern to match XHR/fetch response URLs
:param xhr_container: Optional list to store captured XHR/fetch responses
:return: A callback function for page.on("response", ...)
"""
@@ -161,6 +169,13 @@ class SyncSession:
and finished_response.request.frame == page_info.page.main_frame
):
response_container[0] = finished_response
elif (
xhr_pattern
and xhr_container is not None
and finished_response.request.resource_type in ("xhr", "fetch")
and re_search(xhr_pattern, finished_response.url)
):
xhr_container.append(finished_response)
return handle_response
@@ -317,11 +332,18 @@ class AsyncSession:
await self._wait_for_networkidle(page)
@staticmethod
def _create_response_handler(page_info: PageInfo[AsyncPage], response_container: List) -> Callable:
"""Create an async response handler that captures the final navigation response.
def _create_response_handler(
page_info: PageInfo[AsyncPage],
response_container: List,
xhr_pattern: Optional[str] = None,
xhr_container: Optional[List] = None,
) -> Callable:
"""Create an async response handler that captures the final navigation response and optionally XHR/fetch responses.
:param page_info: The PageInfo object containing the page
:param response_container: A list to store the final response (mutable container)
:param xhr_pattern: Optional regex pattern to match XHR/fetch response URLs
:param xhr_container: Optional list to store captured XHR/fetch responses
:return: A callback function for page.on("response", ...)
"""
@@ -332,6 +354,13 @@ class AsyncSession:
and finished_response.request.frame == page_info.page.main_frame
):
response_container[0] = finished_response
elif (
xhr_pattern
and xhr_container is not None
and finished_response.request.resource_type in ("xhr", "fetch")
and re_search(xhr_pattern, finished_response.url)
):
xhr_container.append(finished_response)
return handle_response