🕷️ The BEST Web Scraping framework that handles everything from a single request to a full-scale crawl! Don't be shy, join here: https://discord.gg/EMgGbDceNQ and follow here for daily tips and tricks: https://x.com/Scrapling_dev
Python aiai-scrapingautomationcrawlercrawlingcrawling-pythondatadata-extractionmcpmcp-serverplaywrightpythonscrapingselectorsstealthweb-scraperweb-scrapingweb-scraping-pythonwebscrapingxpath
fix(mcp)!: Make the screenshot tool doesn't reload the page
Karim shoair5bbc6842bbe8
diff --git a/agent-skill/Scrapling-Skill.zip b/agent-skill/Scrapling-Skill.zipindex c4b3a3a..71f72c8 100644Binary files a/agent-skill/Scrapling-Skill.zip and b/agent-skill/Scrapling-Skill.zip differdiff --git a/agent-skill/Scrapling-Skill/references/mcp-server.md b/agent-skill/Scrapling-Skill/references/mcp-server.mdindex 1a5205d..2322a9c 100644--- a/agent-skill/Scrapling-Skill/references/mcp-server.md+++ b/agent-skill/Scrapling-Skill/references/mcp-server.md@@ -2,7 +2,7 @@The Scrapling MCP server exposes fifteen tools over the MCP protocol. It supports CSS-selector-based content narrowing (reducing tokens by extracting only relevant elements before returning results), three levels of scraping capability (plain HTTP, browser-rendered, and stealth/anti-bot bypass), persistent browser session management, mouse actions, batch field filling, keyboard shortcuts, page waits, and page screenshots returned as real image content blocks. Fetch tools come in two modes: one-shot tools (`browser_fetch_once`, `browser_fetch_many_once`, `browser_stealth_fetch_once`, `browser_stealth_fetch_many_once`) each launch and close their own browser, while `browser_fetch` and `session_make_request` work through sessions opened with `browser_open`/`open_request_session`.-Fetch and HTTP request tools return a `ResponseModel` with fields: `status` (int), `content` (list of strings), `url` (str). Bulk tools return a list of these responses. The `browser_screenshot` tool returns a list of MCP content blocks: an `ImageContent` (the screenshot bytes) followed by a `TextContent` (the post-redirect URL). `browser_snapshot` and `browser_actions` return plain text.+Fetch and HTTP request tools return a `ResponseModel` with fields: `status` (int), `content` (list of strings), `url` (str). Bulk tools return a list of these responses. The `browser_screenshot` tool returns a list of MCP content blocks: an `ImageContent` (the screenshot bytes) followed by a `TextContent` (the current page URL). `browser_snapshot` and `browser_actions` return plain text.## Shadow DOM@@ -238,24 +238,21 @@ Returns a list of `SessionInfo` objects, each with `session_id`, `session_type`,No parameters.-### `browser_screenshot` -- Capture a page screenshot+### `browser_screenshot` -- Capture the current page-Navigates to a URL inside an existing browser session and returns the screenshot as an MCP `ImageContent` block (the bytes the model can see directly, not a base64 string in JSON) followed by a `TextContent` block carrying the post-redirect URL.+Captures the existing page without reloading or navigating, preserving filled inputs and open menus. Returns an MCP `ImageContent` block followed by a `TextContent` block with the current page URL. The tool uses `structured_output=False`, so the model receives real image content rather than image data duplicated in a structured JSON result.-Requires an open browser session. Call `browser_open` first, then pass the `session_id` here. Both `dynamic` and `stealthy` sessions are accepted.+Call `browser_open`, then `browser_fetch` to open a page first. Both `dynamic` and `stealthy` sessions are accepted. Use `browser_actions` for waits or interactions before capture; this tool does not accept a URL or readiness controls.-| Parameter | Type | Default | Description |-|-----------------------|-----------------------|--------------|--------------------------------------------------------------------------------------|-| `url` | str | required | URL to navigate to and capture |-| `session_id` | str | required | ID of an open browser session created with `browser_open` |-| `image_type` | `"png"` / `"jpeg"` | `"png"` | Image format. Use `"jpeg"` for smaller payloads |-| `full_page` | bool | false | Capture the full scrollable page instead of just the viewport |-| `quality` | int or null | null | JPEG quality 0-100. Raises if passed with `image_type="png"` |-| `wait` | number | 0 | Extra wait (ms) after page load before capture |-| `wait_selector` | str or null | null | CSS selector to wait for before capture |-| `wait_selector_state` | str | `"attached"` | State for `wait_selector`: `"attached"` / `"visible"` / `"hidden"` / `"detached"` |-| `network_idle` | bool | false | Wait until no network activity for 500ms |-| `timeout` | number | 30000 | Timeout in milliseconds |+| Parameter | Type | Default | Description |+|-----------|------|---------|-------------|+| `session_id` | str | required | ID of an open browser session with a page |+| `image_type` | `"png"` / `"jpeg"` | `"png"` | Image format; JPEG allows smaller payloads |+| `full_page` | bool | false | Capture the full scrollable page instead of the viewport |+| `quality` | int or null | null | JPEG quality 0-100; an error if supplied for PNG |+| `timeout` | number | 30000 | Finite nonnegative capture timeout in milliseconds; 0 disables it |++The page stays reserved during capture and is released after success, failure, or cancellation. Unknown or HTTP sessions and missing, closed, or busy pages return an error. Capture errors propagate.## Tool selection guide@@ -269,7 +266,7 @@ Requires an open browser session. Call `browser_open` first, then pass the `sess| Multiple protected pages | `browser_stealth_fetch_many_once` || Multiple pages from the same site | `browser_open` + `browser_fetch` per page || Multiple plain HTTP requests to one site | `open_request_session` + `session_make_request` per request |-| Need a screenshot of a page | `browser_open` + `browser_screenshot` with `session_id` |+| Capture the current page | `browser_screenshot` after `browser_open` + `browser_fetch` || Read the current page's AI ARIA snapshot | `browser_snapshot` with `session_id` || Chain mouse, field, keyboard, or wait actions | `browser_actions` with `session_id` |diff --git a/docs/ai/mcp-server.md b/docs/ai/mcp-server.mdindex 0d67f4f..e8e945f 100644--- a/docs/ai/mcp-server.md+++ b/docs/ai/mcp-server.md@@ -35,7 +35,8 @@ The Scrapling MCP Server provides fifteen tools for web scraping and browser int- **`session_make_request`**: Make an HTTP request with any method through a session opened with `open_request_session`, reusing its cookies, connections, and browser fingerprint. This is the session counterpart of `make_request`.#### 📸 Screenshots-- **`browser_screenshot`**: Capture a PNG or JPEG screenshot of a page using an open browser session, returned as an image content block the model can actually see (not a base64 string blob). Supports full-page captures, JPEG quality, and the usual readiness controls (`wait`, `wait_selector`, `network_idle`).++- **`browser_screenshot`**: Capture the current page as PNG or JPEG without reloading it, keeping filled inputs and open menus in place. Supports the visible viewport or the full page, with adjustable JPEG quality. The screenshot is returned as a real image content block the model can see directly.#### AI Snapshots@@ -457,11 +458,12 @@ This protection runs automatically for HTML, Markdown, and text extraction. Keep- Pass a custom `session_id` to the open tools to give sessions meaningful names (e.g. `"search"`, `"checkout"`) instead of the random hex default. They raise if the chosen ID is already in use, so you can detect collisions up front### 7. Capturing Screenshots-- `browser_screenshot` only works through an existing browser session, so call `browser_open` first (either `dynamic` or `stealthy` works)-- The image is returned as a real `ImageContent` block, not a base64 string in JSON, so the model sees the page directly-- Use `full_page=True` when you need everything below the fold; the default captures only the visible viewport-- Pick `image_type="jpeg"` with a `quality` value (0-100) for smaller payloads when pixel-perfect color isn't needed-- The same `wait`, `wait_selector`, `network_idle`, and `timeout` controls used by `browser_fetch_once` are available here too++- `browser_screenshot` captures the page already opened through `browser_fetch` in a dynamic or stealthy browser session. It does not reload the page, so the AI can inspect filled fields, open menus, and other results of its actions.+- The image is returned as a real `ImageContent` block, so the model sees the page directly.+- Capture the visible viewport or the full page, including content below the fold.+- Choose PNG for lossless images or JPEG with adjustable quality for smaller payloads.+- The AI can use `browser_actions` to wait for content or finish interactions before capture.## Legal and Ethical Considerationsdiff --git a/docs/api-reference/mcp-server.md b/docs/api-reference/mcp-server.mdindex 64ec8f9..5168a51 100644--- a/docs/api-reference/mcp-server.md+++ b/docs/api-reference/mcp-server.md@@ -30,7 +30,7 @@ server = ScraplingMCPServer(executable_path="/path/to/chromium")## Response Model-The standardized response structure returned by the fetch and HTTP request tools. Bulk tools return a list of these responses. Session management tools use the models below, `browser_screenshot` returns image and text content blocks, and `browser_snapshot` and `browser_actions` return plain text.+The standardized response structure returned by the fetch and HTTP request tools. Bulk tools return a list of these responses. Session management tools use the models below, `browser_screenshot` returns the current page image and URL as image and text content blocks, and `browser_snapshot` and `browser_actions` return plain text.## ::: scrapling.core.ai.ResponseModelhandler: pythondiff --git a/scrapling/core/ai.py b/scrapling/core/ai.pyindex 017c526..b98f74b 100644--- a/scrapling/core/ai.py+++ b/scrapling/core/ai.py@@ -626,65 +626,30 @@ class ScraplingMCPServer:async def browser_screenshot(self,- url: str,session_id: str,image_type: ScreenshotType = "png",full_page: bool = False,- quality: Optional[int] = None,- wait: int | float = 0,- wait_selector: Optional[str] = None,- wait_selector_state: SelectorWaitStates = "attached",- network_idle: bool = False,- timeout: int | float = 30000,+ quality: Optional[Annotated[int, Field(ge=0, le=100)]] = None,+ timeout: NonNegativeFiniteFloat = 30000,) -> List[ImageContent | TextContent]:- """Navigate to a URL in an open browser session; return the image and URL at capture.+ """Capture the current page without navigating; return the image and current URL.+ Use browser_actions for waits before capture.- :param url: URL to navigate to and capture.- :param session_id: ID from `browser_open`.+ :param session_id: ID from `browser_open`; call `browser_fetch` first.:param image_type: Image format.:param full_page: Capture the full scrollable page.:param quality: JPEG quality, 0-100; invalid for PNG.- :param wait: Extra milliseconds after capture.- :param wait_selector: Wait for the first CSS match after capture; continue if the wait fails.- :param wait_selector_state: Target state of `wait_selector`.- :param network_idle: Try to wait for 500 ms without network activity; continue if the wait fails.- :param timeout: Navigation and page-operation timeout in milliseconds.+ :param timeout: Capture limit in milliseconds; 0 disables it."""if quality is not None and image_type != "jpeg":raise ValueError("'quality' is only valid when 'image_type' is 'jpeg'.")- entry = self._get_session(session_id, expected_type=["dynamic", "stealthy"])-- screenshot_kwargs: Dict[str, Any] = {"type": image_type, "full_page": full_page}- if quality is not None:- screenshot_kwargs["quality"] = quality-- captured: Dict[str, Any] = {}-- async def _capture(page: Any) -> None:- try:- captured["bytes"] = await page.screenshot(**screenshot_kwargs)- captured["url"] = page.url- except Exception as exc:- captured["error"] = exc-- await entry.session.fetch(- url,- page_action=_capture,- wait=wait,- timeout=timeout,- network_idle=network_idle,- wait_selector=wait_selector,- wait_selector_state=wait_selector_state,- )-- if "error" in captured:- raise captured["error"]- if "bytes" not in captured:- raise RuntimeError(f"Failed to capture screenshot for {url}")-- image = Image(data=captured["bytes"], format=image_type).to_image_content()- return [image, TextContent(type="text", text=captured["url"])]+ with self._browser_page(session_id) as (_, page):+ image = Image(+ data=await page.screenshot(type=image_type, full_page=full_page, quality=quality, timeout=timeout),+ format=image_type,+ ).to_image_content()+ return [image, TextContent(type="text", text=page.url)]@staticmethodasync def make_request(diff --git a/tests/ai/test_ai_mcp.py b/tests/ai/test_ai_mcp.pyindex 8fba5a6..d308cd5 100644--- a/tests/ai/test_ai_mcp.py+++ b/tests/ai/test_ai_mcp.py@@ -159,7 +159,7 @@ class TestSessionTypeChecks:session = Mock(_is_alive=True)server._sessions["test"] = _SessionEntry(session, session_type)args = {"session_id": "test"}- if tool != "browser_snapshot":+ if tool not in ("browser_snapshot", "browser_screenshot"):args["url"] = "https://example.com"async with Client(server._build_server("127.0.0.1", 8000)) as client:result = await client.call_tool(tool, args)@@ -428,7 +428,7 @@ class TestStaticSessionManagement:with pytest.raises(ValueError, match="requires a 'dynamic' or 'stealthy' session"):await server.browser_fetch(url="https://example.com", session_id="st2")with pytest.raises(ValueError, match="requires a 'dynamic' or 'stealthy' session"):- await server.browser_screenshot(url="https://example.com", session_id="st2")+ await server.browser_screenshot(session_id="st2")await server.close_session("st2")@@ -768,10 +768,12 @@ class TestScreenshot:"""PNG screenshot via a dynamic session returns image and url content blocks"""opened = await server.browser_open(session_type="dynamic", headless=True)try:- result = await server.browser_screenshot(url=test_url, session_id=opened.session_id)+ await server.browser_fetch(url=test_url, session_id=opened.session_id)+ result = await server.browser_screenshot(session_id=opened.session_id)assert isinstance(result, list) and len(result) == 2assert isinstance(result[0], ImageContent)assert result[0].mime_type == "image/png"+ assert base64.b64decode(result[0].data).startswith(b"\x89PNG\r\n\x1a\n")assert isinstance(result[1], TextContent)assert result[1].text == test_urlfinally:@@ -782,11 +784,12 @@ class TestScreenshot:"""JPEG screenshot with quality parameter via a dynamic session"""opened = await server.browser_open(session_type="dynamic", headless=True)try:- result = await server.browser_screenshot(- url=test_url, session_id=opened.session_id, image_type="jpeg", quality=80- )+ await server.browser_fetch(url=test_url, session_id=opened.session_id)+ result = await server.browser_screenshot(session_id=opened.session_id, image_type="jpeg", quality=80)assert isinstance(result[0], ImageContent)assert result[0].mime_type == "image/jpeg"+ jpeg = base64.b64decode(result[0].data)+ assert jpeg.startswith(b"\xff\xd8") and jpeg.endswith(b"\xff\xd9")finally:await server.close_session(opened.session_id)@@ -795,9 +798,11 @@ class TestScreenshot:"""PNG screenshot via a stealthy session"""opened = await server.browser_open(session_type="stealthy", headless=True)try:- result = await server.browser_screenshot(url=test_url, session_id=opened.session_id)+ await server.browser_fetch(url=test_url, session_id=opened.session_id)+ result = await server.browser_screenshot(session_id=opened.session_id)assert isinstance(result[0], ImageContent)assert result[0].mime_type == "image/png"+ assert base64.b64decode(result[0].data).startswith(b"\x89PNG\r\n\x1a\n")finally:await server.close_session(opened.session_id)@@ -808,12 +813,9 @@ class TestScreenshot:with _serve_html(tall_html) as tall_url:opened = await server.browser_open(session_type="dynamic", headless=True)try:- viewport_result = await server.browser_screenshot(- url=tall_url, session_id=opened.session_id, full_page=False- )- full_result = await server.browser_screenshot(- url=tall_url, session_id=opened.session_id, full_page=True- )+ await server.browser_fetch(url=tall_url, session_id=opened.session_id)+ viewport_result = await server.browser_screenshot(session_id=opened.session_id, full_page=False)+ full_result = await server.browser_screenshot(session_id=opened.session_id, full_page=True)viewport_png = base64.b64decode(viewport_result[0].data)full_png = base64.b64decode(full_result[0].data)@@ -826,7 +828,7 @@ class TestScreenshot:async def test_screenshot_invalid_session_id_raises(self, server, test_url):"""Unknown session_id raises ValueError"""with pytest.raises(ValueError, match="not found"):- await server.browser_screenshot(url=test_url, session_id="does-not-exist")+ await server.browser_screenshot(session_id="does-not-exist")@pytest.mark.asyncioasync def test_screenshot_quality_with_png_raises(self, server, test_url):@@ -834,9 +836,7 @@ class TestScreenshot:opened = await server.browser_open(session_type="dynamic", headless=True)try:with pytest.raises(ValueError, match="quality"):- await server.browser_screenshot(- url=test_url, session_id=opened.session_id, image_type="png", quality=90- )+ await server.browser_screenshot(session_id=opened.session_id, image_type="png", quality=90)finally:await server.close_session(opened.session_id)diff --git a/tests/ai/test_browser_screenshot.py b/tests/ai/test_browser_screenshot.pynew file mode 100644index 0000000..e949c02--- /dev/null+++ b/tests/ai/test_browser_screenshot.py@@ -0,0 +1,341 @@+import asyncio+from base64 import b64decode+from contextlib import suppress+from os import getenv+from struct import unpack+from unittest.mock import AsyncMock, Mock, call++import pytest+from anyio import CancelScope+from mcp.client import Client+from mcp.types import ImageContent, TextContent+from patchright.async_api import Error as PatchrightError+from playwright.async_api import Error as PlaywrightError+from pydantic import ValidationError++from scrapling.core.ai import ScraplingMCPServer, SessionType, _SessionEntry+from scrapling.core._types import Any+from scrapling.engines._browsers._base import AsyncSession+++def _server(session_type: SessionType = "dynamic") -> tuple[ScraplingMCPServer, AsyncSession, Mock]:+ server = ScraplingMCPServer()+ session = AsyncSession()+ session._is_alive = True+ page = Mock()+ page.is_closed.return_value = False+ page.url = "https://screenshot.test/current"+ page.screenshot = AsyncMock(return_value=b"captured pixels")+ session.page_pool.add_page(page).mark_ready()+ server._sessions["browser"] = _SessionEntry(session, session_type)+ return server, session, page+++async def test_browser_screenshot_schema() -> None:+ async with Client(ScraplingMCPServer()._build_server("127.0.0.1", 8000)) as client:+ tool = next(tool for tool in (await client.list_tools()).tools if tool.name == "browser_screenshot")+ schema = tool.input_schema+ assert schema["required"] == ["session_id"]+ properties = schema["properties"]+ assert set(properties) == {"session_id", "image_type", "full_page", "quality", "timeout"}+ assert properties["image_type"]["default"] == "png"+ assert properties["image_type"]["enum"] == ["png", "jpeg"]+ assert properties["full_page"]["default"] is False+ assert properties["quality"]["default"] is None+ assert {"type": "integer", "minimum": 0, "maximum": 100} in properties["quality"]["anyOf"]+ assert {"type": "null"} in properties["quality"]["anyOf"]+ assert properties["timeout"]["default"] == 30000+ assert properties["timeout"]["minimum"] == 0+ assert tool.output_schema is None+ assert tool.annotations is not None+ assert tool.annotations.read_only_hint is True+ assert tool.annotations.open_world_hint is True++[email protected]("session_type", ["dynamic", "stealthy"])+ "options",+ [+ {},+ {"image_type": "jpeg"},+ {"image_type": "jpeg", "quality": 0, "full_page": True, "timeout": 0},+ {"image_type": "jpeg", "quality": 100, "timeout": 12.5},+ ],+)+async def test_browser_screenshot_returns_native_image_without_navigation(+ session_type: SessionType, options: dict[str, Any]+) -> None:+ server, session, page = _server(session_type)++ async def capture(**kwargs: Any) -> bytes:+ assert session.page_pool.pages[0].state == "busy"+ return b"captured pixels"++ page.screenshot.side_effect = capture+ async with Client(server._build_server("127.0.0.1", 8000)) as client:+ result = await client.call_tool("browser_screenshot", {"session_id": "browser", **options})+ assert not result.is_error and result.structured_content is None+ assert len(result.content) == 2+ image, url = result.content+ assert isinstance(image, ImageContent)+ assert b64decode(image.data) == b"captured pixels"+ assert image.mime_type == f"image/{options.get('image_type', 'png')}"+ assert url == TextContent(type="text", text=page.url)+ assert page.mock_calls == [+ call.is_closed(),+ call.screenshot(+ type=options.get("image_type", "png"),+ full_page=options.get("full_page", False),+ quality=options.get("quality"),+ timeout=options.get("timeout", 30000),+ ),+ call.is_closed(),+ ]+ assert session.page_pool.pages[0].state == "ready"+++ "options",+ [+ {"image_type": "gif"},+ {"quality": -1},+ {"quality": 101},+ {"quality": 1.5},+ {"quality": "invalid"},+ {"timeout": -1},+ {"timeout": "NaN"},+ {"timeout": "Infinity"},+ ],+)+async def test_browser_screenshot_invalid_input_does_not_reserve_page(options: dict[str, Any]) -> None:+ server, session, page = _server()+ async with Client(server._build_server("127.0.0.1", 8000)) as client:+ result = await client.call_tool(+ "browser_screenshot", {"session_id": "browser", "image_type": "jpeg", **options}+ )+ assert result.is_error+ page.is_closed.assert_not_called()+ page.screenshot.assert_not_awaited()+ assert session.page_pool.pages[0].state == "ready"++[email protected]("value", [float("nan"), float("inf"), float("-inf")])+def test_browser_screenshot_schema_rejects_nonfinite_timeout(value: float) -> None:+ tool = ScraplingMCPServer()._build_server("127.0.0.1", 8000)._tool_manager.get_tool("browser_screenshot")+ assert tool is not None+ with pytest.raises(ValidationError):+ tool.fn_metadata.arg_model.model_validate({"session_id": "browser", "timeout": value})++[email protected]("quality", [0, 90])+async def test_browser_screenshot_png_quality_does_not_reserve_page(quality: int) -> None:+ server, session, page = _server()+ with pytest.raises(ValueError, match="quality"):+ await server.browser_screenshot("browser", quality=quality)+ async with Client(server._build_server("127.0.0.1", 8000)) as client:+ result = await client.call_tool("browser_screenshot", {"session_id": "browser", "quality": quality})+ assert result.is_error+ page.is_closed.assert_not_called()+ page.screenshot.assert_not_awaited()+ assert session.page_pool.pages[0].state == "ready"+++ "state, message",+ [+ ("missing", "not found"),+ ("dead", "no longer alive"),+ ("static", "'dynamic' or 'stealthy'"),+ ("empty", "browser_fetch"),+ ("busy", "busy"),+ ("closed", "closed page"),+ ],+)+async def test_browser_screenshot_session_errors(state: str, message: str) -> None:+ server, session, page = _server("static" if state == "static" else "dynamic")+ if state == "missing":+ server._sessions.clear()+ elif state == "dead":+ session._is_alive = False+ elif state == "empty":+ session.page_pool.clear()+ elif state == "busy":+ session.page_pool.pages[0].mark_busy()+ elif state == "closed":+ page.is_closed.return_value = True+ async with Client(server._build_server("127.0.0.1", 8000)) as client:+ result = await client.call_tool("browser_screenshot", {"session_id": "browser"})+ assert result.is_error and isinstance(result.content[0], TextContent)+ assert message in result.content[0].text+ page.screenshot.assert_not_awaited()+ if state == "closed":+ assert session.page_pool.pages_count == 0+ elif state == "busy":+ assert session.page_pool.pages[0].state == "busy"++[email protected]("session_type, error_type", [("dynamic", PlaywrightError), ("stealthy", PatchrightError)])[email protected]("closed", [False, True])[email protected]("through_mcp", [False, True])+async def test_browser_screenshot_native_error_releases_page_without_retry(+ session_type: SessionType, error_type: type[Exception], closed: bool, through_mcp: bool+) -> None:+ server, session, page = _server(session_type)+ native_error = error_type("capture failed")++ async def fail(**kwargs: Any) -> bytes:+ page.is_closed.return_value = closed+ raise native_error++ page.screenshot.side_effect = fail+ if through_mcp:+ async with Client(server._build_server("127.0.0.1", 8000)) as client:+ result = await client.call_tool("browser_screenshot", {"session_id": "browser"})+ assert result.is_error and isinstance(result.content[0], TextContent)+ assert "capture failed" in result.content[0].text+ else:+ with pytest.raises(error_type) as error:+ await server.browser_screenshot("browser")+ assert error.value is native_error+ page.screenshot.assert_awaited_once()+ page.close.assert_not_called()+ if closed:+ assert session.page_pool.pages_count == 0+ else:+ assert session.page_pool.pages[0].state == "ready"++[email protected]("cancel_mode", ["task", "scope"])[email protected]("closed", [False, True])+async def test_browser_screenshot_reserves_page_until_cancelled(cancel_mode: str, closed: bool) -> None:+ server, session, page = _server()+ entered = asyncio.Event()+ scope = CancelScope()++ async def pending(**kwargs: Any) -> bytes:+ entered.set()+ await asyncio.Event().wait()+ return b""++ async def capture() -> None:+ with scope:+ await server.browser_screenshot("browser")++ page.screenshot.side_effect = pending+ task = asyncio.create_task(capture())+ try:+ await asyncio.wait_for(entered.wait(), 5)+ assert session.page_pool.pages[0].state == "busy"+ with pytest.raises(RuntimeError, match="busy"):+ await server.browser_screenshot("browser")+ with pytest.raises(RuntimeError, match="busy"):+ await server.browser_actions("browser", [{"type": "press_key", "key": "Enter"}])+ page.is_closed.return_value = closed+ scope.cancel() if cancel_mode == "scope" else task.cancel()+ if cancel_mode == "scope":+ await asyncio.wait_for(task, 5)+ assert scope.cancelled_caught+ else:+ with pytest.raises(asyncio.CancelledError):+ await task+ finally:+ if not task.done():+ task.cancel()+ with suppress(asyncio.CancelledError):+ await task+ page.screenshot.assert_awaited_once()+ page.close.assert_not_called()+ if closed:+ assert session.page_pool.pages_count == 0+ else:+ assert session.page_pool.pages[0].state == "ready"++[email protected]("session_type", ["dynamic", "stealthy"])+async def test_browser_screenshot_live_preserves_actions_and_current_page(session_type: SessionType) -> None:+ server = ScraplingMCPServer(executable_path=getenv("SCRAPLING_EXECUTABLE_PATH"))+ requests: list[str] = []+ html = """<!DOCTYPE html><html><head><style>+ body { margin: 0; height: 4000px; background: linear-gradient(white, #abc); }+ #controls { position: fixed; top: 20px; left: 20px; background: white; }+ </style></head><body><div id=controls>+ <input id=name><input id=agree type=checkbox><button id=toggle>Open menu</button>+ <div id=menu hidden>Saved choices</div></div><script>+ document.querySelector('#toggle').onclick = () => {+ document.querySelector('#menu').hidden = false;+ history.pushState({}, '', '/edited');+ };+ </script></body></html>"""++ async def serve(route: Any) -> None:+ requests.append(route.request.url)+ await route.fulfill(content_type="text/html", body=html)++ async with Client(server._build_server("127.0.0.1", 8000)) as client:+ opened = await client.call_tool("browser_open", {"session_id": "browser", "session_type": session_type})+ assert not opened.is_error+ try:+ session = server._sessions["browser"].session+ await session.context.route("**/*", serve)+ fetched = await client.call_tool(+ "browser_fetch",+ {"session_id": "browser", "url": "https://screenshot.test/start", "google_search": False},+ )+ assert not fetched.is_error+ page_info = session.page_pool.pages[0]+ page = page_info.page+ actions = await client.call_tool(+ "browser_actions",+ {+ "session_id": "browser",+ "actions": [+ {"type": "textbox", "selector": "#name", "value": "Scrapling"},+ {"type": "checkbox", "selector": "#agree", "value": True},+ {"type": "click", "selector": "#toggle"},+ {"type": "click", "selector": "#name"},+ {"type": "move", "x": 400, "y": 300},+ {"type": "wheel", "delta_y": 600},+ ],+ },+ )+ assert not actions.is_error+ await page.wait_for_function("scrollY === 600")+ read_state = """() => ({url: location.href, name: document.querySelector('#name').value,+ checked: document.querySelector('#agree').checked, menu: !document.querySelector('#menu').hidden,+ focus: document.activeElement.id, x: scrollX, y: scrollY})"""+ before = await page.evaluate(read_state)+ assert before == {+ "url": "https://screenshot.test/edited",+ "name": "Scrapling",+ "checked": True,+ "menu": True,+ "focus": "name",+ "x": 0,+ "y": 600,+ }+ initial_requests = requests.copy()+ assert initial_requests == ["https://screenshot.test/start"]+ heights = []+ for full_page in (False, True):+ result = await client.call_tool("browser_screenshot", {"session_id": "browser", "full_page": full_page})+ assert not result.is_error and result.structured_content is None+ assert len(result.content) == 2 and isinstance(result.content[0], ImageContent)+ assert result.content[0].mime_type == "image/png"+ assert result.content[1] == TextContent(type="text", text=before["url"])+ png = b64decode(result.content[0].data)+ assert png.startswith(b"\x89PNG\r\n\x1a\n")+ heights.append(unpack(">II", png[16:24])[1])+ assert await page.evaluate(read_state) == before+ assert requests == initial_requests+ assert session.page_pool.pages == [page_info] and page_info.state == "ready"+ assert heights[1] > heights[0]+ finally:+ assert not (await client.call_tool("close_session", {"session_id": "browser"})).is_error