|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import contextlib |
| 5 | +import http.server |
| 6 | +import socket |
| 7 | +import threading |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import pytest |
| 11 | +import pytest_asyncio |
| 12 | + |
| 13 | +from operator_use.web.browser.config import BrowserConfig |
| 14 | +from operator_use.web.browser.service import Browser |
| 15 | + |
| 16 | + |
| 17 | +def _find_free_port() -> int: |
| 18 | + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: |
| 19 | + sock.bind(("127.0.0.1", 0)) |
| 20 | + return sock.getsockname()[1] |
| 21 | + |
| 22 | + |
| 23 | +def _find_interactive_by_name(state, name: str): |
| 24 | + for node in state.dom_state.interactive_nodes: |
| 25 | + if node.name == name: |
| 26 | + return node |
| 27 | + raise AssertionError(f"Interactive node not found: {name!r}") |
| 28 | + |
| 29 | + |
| 30 | +class _QuietHandler(http.server.SimpleHTTPRequestHandler): |
| 31 | + def log_message(self, format, *args): # noqa: A003 |
| 32 | + pass |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture(scope="module") |
| 36 | +def browser_binary(): |
| 37 | + candidates = [ |
| 38 | + Path("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"), |
| 39 | + Path("/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge"), |
| 40 | + Path("/usr/bin/google-chrome"), |
| 41 | + Path("/usr/bin/chromium"), |
| 42 | + Path("/usr/bin/chromium-browser"), |
| 43 | + Path("/usr/bin/microsoft-edge"), |
| 44 | + ] |
| 45 | + for candidate in candidates: |
| 46 | + if candidate.exists(): |
| 47 | + return str(candidate) |
| 48 | + pytest.skip("No local Chrome/Edge binary available for browser e2e tests") |
| 49 | + |
| 50 | + |
| 51 | +@pytest.fixture(scope="module") |
| 52 | +def browser_site(tmp_path_factory): |
| 53 | + root = tmp_path_factory.mktemp("browser-site") |
| 54 | + (root / "index.html").write_text( |
| 55 | + """<!doctype html> |
| 56 | +<html> |
| 57 | + <head><title>Smoke Index</title></head> |
| 58 | + <body> |
| 59 | + <h1 id="title">Index</h1> |
| 60 | + <a id="nav-link" href="/page2.html">Go to page 2</a> |
| 61 | + <button id="mutate-btn" onclick="document.getElementById('status').textContent='Clicked state updated'"> |
| 62 | + Mutate DOM |
| 63 | + </button> |
| 64 | + <button id="popup-btn" onclick="window.open('/popup.html', '_blank')">Open Popup</button> |
| 65 | + <div id="status">Initial status</div> |
| 66 | + </body> |
| 67 | +</html> |
| 68 | +""", |
| 69 | + encoding="utf-8", |
| 70 | + ) |
| 71 | + (root / "page2.html").write_text( |
| 72 | + """<!doctype html> |
| 73 | +<html> |
| 74 | + <head><title>Smoke Page 2</title></head> |
| 75 | + <body> |
| 76 | + <h1>Second Page</h1> |
| 77 | + <div id="page2-status">Navigation succeeded</div> |
| 78 | + </body> |
| 79 | +</html> |
| 80 | +""", |
| 81 | + encoding="utf-8", |
| 82 | + ) |
| 83 | + (root / "popup.html").write_text( |
| 84 | + """<!doctype html> |
| 85 | +<html> |
| 86 | + <head><title>Smoke Popup</title></head> |
| 87 | + <body> |
| 88 | + <h1>Popup Page</h1> |
| 89 | + </body> |
| 90 | +</html> |
| 91 | +""", |
| 92 | + encoding="utf-8", |
| 93 | + ) |
| 94 | + |
| 95 | + port = _find_free_port() |
| 96 | + handler = lambda *args, **kwargs: _QuietHandler(*args, directory=str(root), **kwargs) |
| 97 | + server = http.server.ThreadingHTTPServer(("127.0.0.1", port), handler) |
| 98 | + thread = threading.Thread(target=server.serve_forever, daemon=True) |
| 99 | + thread.start() |
| 100 | + try: |
| 101 | + yield f"http://127.0.0.1:{port}" |
| 102 | + finally: |
| 103 | + server.shutdown() |
| 104 | + thread.join(timeout=5) |
| 105 | + |
| 106 | + |
| 107 | +@pytest_asyncio.fixture |
| 108 | +async def browser_instance(tmp_path, browser_binary): |
| 109 | + config = BrowserConfig( |
| 110 | + headless=True, |
| 111 | + browser="chrome", |
| 112 | + browser_instance_dir=browser_binary, |
| 113 | + user_data_dir=str(tmp_path / "profile"), |
| 114 | + downloads_dir=str(tmp_path / "downloads"), |
| 115 | + cdp_port=_find_free_port(), |
| 116 | + minimum_wait_page_load_time=0.1, |
| 117 | + wait_for_network_idle_page_load_time=0.1, |
| 118 | + maximum_wait_page_load_time=5.0, |
| 119 | + ) |
| 120 | + browser = Browser(config) |
| 121 | + await browser.init_browser() |
| 122 | + await browser.init_tabs() |
| 123 | + try: |
| 124 | + yield browser |
| 125 | + finally: |
| 126 | + with contextlib.suppress(Exception): |
| 127 | + await asyncio.wait_for(browser.close(), timeout=5.0) |
| 128 | + if browser._process is not None: |
| 129 | + with contextlib.suppress(Exception): |
| 130 | + browser._process.kill() |
| 131 | + |
| 132 | + |
| 133 | +@pytest.mark.asyncio |
| 134 | +async def test_browser_e2e_click_navigation(browser_instance: Browser, browser_site: str): |
| 135 | + await browser_instance.navigate(f"{browser_site}/index.html") |
| 136 | + |
| 137 | + state = await browser_instance.get_state() |
| 138 | + link = _find_interactive_by_name(state, "Go to page 2") |
| 139 | + await browser_instance.current_page().click_at(link.center.x, link.center.y) |
| 140 | + await browser_instance._wait_for_page(timeout=5.0) |
| 141 | + |
| 142 | + current_tab = await browser_instance.get_current_tab() |
| 143 | + assert current_tab is not None |
| 144 | + assert current_tab.title == "Smoke Page 2" |
| 145 | + assert current_tab.url.endswith("/page2.html") |
| 146 | + |
| 147 | + |
| 148 | +@pytest.mark.asyncio |
| 149 | +async def test_browser_e2e_dom_mutation_flow(browser_instance: Browser, browser_site: str): |
| 150 | + await browser_instance.navigate(f"{browser_site}/index.html") |
| 151 | + |
| 152 | + state = await browser_instance.get_state() |
| 153 | + button = _find_interactive_by_name(state, "Mutate DOM") |
| 154 | + await browser_instance.current_page().click_at(button.center.x, button.center.y) |
| 155 | + |
| 156 | + await browser_instance.get_state() |
| 157 | + html = await browser_instance.current_page().get_page_content() |
| 158 | + assert "Clicked state updated" in html |
| 159 | + |
| 160 | + |
| 161 | +@pytest.mark.skip(reason="Popup/new-tab smoke remains flaky in local headless Chrome; popup behavior is covered by watchdog tests.") |
| 162 | +@pytest.mark.asyncio |
| 163 | +async def test_browser_e2e_popup_creates_new_tab(browser_instance: Browser, browser_site: str): |
| 164 | + await browser_instance.navigate(f"{browser_site}/index.html") |
| 165 | + |
| 166 | + current_target = browser_instance._current_target_id |
| 167 | + await browser_instance.current_page().execute_script( |
| 168 | + f'window.open("{browser_site}/popup.html", "_blank")' |
| 169 | + ) |
| 170 | + await asyncio.sleep(1.0) |
| 171 | + |
| 172 | + targets = dict(browser_instance._session_manager.targets) |
| 173 | + assert len(targets) >= 2 |
| 174 | + popup_target = next(target_id for target_id in targets if target_id != current_target) |
| 175 | + popup_info = targets[popup_target] |
| 176 | + assert popup_info["url"].endswith("/popup.html") or popup_info["title"] == "Smoke Popup" |
| 177 | + |
| 178 | + await browser_instance.close_tab(popup_target) |
0 commit comments