Spaces:
Runtime error
Runtime error
Download tests/e2e/test_browser_controller_async_e2e.py from BattleFiven/ai-tester-platform: direct link, hf CLI and curl.
- Browser
- Download file 3.24 kB
-
https://huggingface.co/spaces/BattleFiven/ai-tester-platform/resolve/main/tests/e2e/test_browser_controller_async_e2e.py
- Command line
-
hf download hf://spaces/BattleFiven/ai-tester-platform/tests/e2e/test_browser_controller_async_e2e.py
-
curl -L -o test_browser_controller_async_e2e.py https://huggingface.co/spaces/BattleFiven/ai-tester-platform/resolve/main/tests/e2e/test_browser_controller_async_e2e.py
3.24 kB
| import asyncio | |
| from pathlib import Path | |
| import pytest | |
| from src.core.browser_controller_async import BrowserControllerAsync | |
| CHAT_FIXTURE = """<!doctype html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8" /> | |
| <title>Chat Fixture</title> | |
| <style> | |
| body { font-family: Arial, sans-serif; padding: 16px; } | |
| #messages { margin-bottom: 12px; } | |
| .message { padding: 8px; border-radius: 6px; margin: 6px 0; } | |
| .message.user { background: #dbeafe; } | |
| .message.bot { background: #dcfce7; } | |
| #chat-form { display: flex; gap: 8px; } | |
| #chat-input { width: 360px; padding: 8px; } | |
| </style> | |
| </head> | |
| <body> | |
| <div id="messages"></div> | |
| <form id="chat-form"> | |
| <input id="chat-input" type="text" placeholder="Type here" /> | |
| <button id="send-btn" type="submit">Send</button> | |
| </form> | |
| <script> | |
| const messages = document.getElementById("messages"); | |
| const form = document.getElementById("chat-form"); | |
| const input = document.getElementById("chat-input"); | |
| function addMessage(text, sender) { | |
| const div = document.createElement("div"); | |
| div.className = `message ${sender}`; | |
| div.setAttribute("data-sender", sender); | |
| div.textContent = text; | |
| messages.appendChild(div); | |
| } | |
| form.addEventListener("submit", (ev) => { | |
| ev.preventDefault(); | |
| const value = (input.value || "").trim(); | |
| if (!value) return; | |
| addMessage(value, "user"); | |
| input.value = ""; | |
| setTimeout(() => { | |
| addMessage(`Echo: ${value}`, "bot"); | |
| }, 200); | |
| }); | |
| </script> | |
| </body> | |
| </html> | |
| """ | |
| async def test_browser_controller_async_send_and_receive(tmp_path: Path): | |
| fixture_file = tmp_path / "chat_fixture.html" | |
| fixture_file.write_text(CHAT_FIXTURE, encoding="utf-8") | |
| controller = BrowserControllerAsync( | |
| selectors={ | |
| "chat_input": "#chat-input", | |
| "send_button": "#send-btn", | |
| "chat_messages": "#messages .message", | |
| }, | |
| output_dir=str(tmp_path), | |
| headless=True, | |
| ) | |
| started = await controller.start(fixture_file.resolve().as_uri()) | |
| if not started: | |
| pytest.skip(f"Playwright browser unavailable: {controller.last_error}") | |
| try: | |
| assert await controller.send_message("ciao") | |
| response = None | |
| for _ in range(20): | |
| messages = await controller.get_chat_messages() | |
| bot_messages = [m for m in messages if m.get("sender") == "bot"] | |
| if bot_messages: | |
| response = bot_messages[-1].get("text") | |
| break | |
| await asyncio.sleep(0.25) | |
| assert response is not None | |
| assert "Echo: ciao" in response | |
| messages = await controller.get_chat_messages() | |
| assert any(m.get("sender") == "user" and "ciao" in m.get("text", "") for m in messages) | |
| assert any(m.get("sender") == "bot" and "Echo: ciao" in m.get("text", "") for m in messages) | |
| screenshot_path = await controller.capture_screenshot("e2e_async_chat") | |
| assert screenshot_path | |
| assert Path(screenshot_path).exists() | |
| finally: | |
| await controller.cleanup() | |