Spaces:
Running
Running
Download src/browser_automation_agent/testing/test_page.py from sitcod3/dracull-agent: direct link, hf CLI and curl.
- Browser
- Download file 4.12 kB
-
https://huggingface.co/spaces/sitcod3/dracull-agent/resolve/main/src/browser_automation_agent/testing/test_page.py
- Command line
-
hf download hf://spaces/sitcod3/dracull-agent/src/browser_automation_agent/testing/test_page.py
-
curl -L -o test_page.py https://huggingface.co/spaces/sitcod3/dracull-agent/resolve/main/src/browser_automation_agent/testing/test_page.py
4.12 kB
| """A deterministic HTTP page used by the Phase 4 browser test matrix. | |
| The server binds only to ``127.0.0.1`` on an operating-system-selected free | |
| port. It serves one page containing a form, a text-changing button, an | |
| in-page DOM mutation button, and an iframe that points at a second route on | |
| the same server. | |
| """ | |
| from __future__ import annotations | |
| import socket | |
| from collections.abc import Awaitable, Callable | |
| from typing import Any | |
| from aiohttp import web | |
| PAGE_TITLE = "Dracull Deterministic Test Page" | |
| IFRAME_TITLE = "Dracull Deterministic Iframe" | |
| FORM_RESULT_PREFIX = "Submitted:" | |
| _PAGE_HTML = """<!doctype html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>Dracull Deterministic Test Page</title> | |
| </head> | |
| <body> | |
| <main> | |
| <h1>Dracull deterministic test page</h1> | |
| <form id="test-form"> | |
| <label for="message">Message</label> | |
| <input id="message" name="message" type="text" value="" aria-label="Message"> | |
| <button id="submit-button" type="submit">Submit</button> | |
| </form> | |
| <output id="form-result" aria-live="polite"></output> | |
| <button id="change-button" type="button">Change text</button> | |
| <p id="changed-text">Original text</p> | |
| <button id="mutate-button" type="button">Append element</button> | |
| <div id="mutation-container"></div> | |
| <iframe id="test-iframe" title="Dracull deterministic iframe" src="/iframe"></iframe> | |
| </main> | |
| <script> | |
| document.getElementById("test-form").addEventListener("submit", (event) => { | |
| event.preventDefault(); | |
| const message = document.getElementById("message").value; | |
| document.getElementById("form-result").textContent = "Submitted: " + message; | |
| }); | |
| document.getElementById("change-button").addEventListener("click", () => { | |
| document.getElementById("changed-text").textContent = "Changed text"; | |
| }); | |
| let mutationCount = 0; | |
| document.getElementById("mutate-button").addEventListener("click", () => { | |
| mutationCount += 1; | |
| const item = document.createElement("p"); | |
| item.className = "mutated-item"; | |
| item.textContent = "Mutation " + mutationCount; | |
| document.getElementById("mutation-container").appendChild(item); | |
| }); | |
| </script> | |
| </body> | |
| </html> | |
| """ | |
| _IFRAME_HTML = """<!doctype html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>Dracull Deterministic Iframe</title> | |
| </head> | |
| <body> | |
| <p id="iframe-distinct-text">Distinct iframe content</p> | |
| </body> | |
| </html> | |
| """ | |
| async def _main_page(_request: web.Request) -> web.Response: | |
| return web.Response(text=_PAGE_HTML, content_type="text/html") | |
| async def _iframe_page(_request: web.Request) -> web.Response: | |
| return web.Response(text=_IFRAME_HTML, content_type="text/html") | |
| async def start_test_server() -> tuple[str, Callable[[], Awaitable[None]]]: | |
| """Start the local test server and return ``(base_url, cleanup)``. | |
| The socket is explicitly bound to loopback with port ``0``. The caller | |
| owns cleanup and must await it in a ``finally`` block so no listener is | |
| left behind. | |
| """ | |
| listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
| listener.bind(("127.0.0.1", 0)) | |
| listener.listen() | |
| port = int(listener.getsockname()[1]) | |
| app = web.Application() | |
| app.router.add_get("/", _main_page) | |
| app.router.add_get("/iframe", _iframe_page) | |
| runner = web.AppRunner(app) | |
| try: | |
| await runner.setup() | |
| site = web.SockSite(runner, listener) | |
| await site.start() | |
| except BaseException: | |
| listener.close() | |
| await runner.cleanup() | |
| raise | |
| async def cleanup() -> None: | |
| await runner.cleanup() | |
| return f"http://127.0.0.1:{port}/", cleanup | |
| def page_facts() -> dict[str, Any]: | |
| """Return the stable facts a test may compare with browser read-back.""" | |
| return { | |
| "title": PAGE_TITLE, | |
| "iframe_title": IFRAME_TITLE, | |
| "form_result_prefix": FORM_RESULT_PREFIX, | |
| } | |