Spaces:
Runtime error

File size: 3,243 Bytes
ed86a97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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()