Spaces:
Runtime error

ai-tester-platform / tests /e2e /test_browser_controller_async_e2e.py
BattleFiven's picture
deploy: round 6 save_intermediate light (sha 05316bb)
ed86a97
Raw History Blame Contribute Delete
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>
"""
@pytest.mark.asyncio
@pytest.mark.e2e
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()