fix live testing tothe browser alias testing

This commit is contained in:
2026-04-10 13:40:14 +02:00
parent 57c4ccc48a
commit 54760412b3
+36 -14
View File
@@ -1,30 +1,52 @@
""" """
Shared pytest fixtures for browser-cli integration tests. Shared pytest fixtures for browser-cli integration tests.
Tests that require a live browser connection use the `browser` fixture. Tests that require a live browser connection use the `browser` fixture and
target the `testing` browser profile.
They are automatically skipped if the native host socket is not reachable. They are automatically skipped if the native host socket is not reachable.
""" """
import time
import pytest import pytest
from browser_cli.client import send_command, BrowserNotConnected from browser_cli.client import send_command, BrowserNotConnected
TEST_BROWSER_PROFILE = "testing"
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
def browser(): def browser():
"""Returns a connected send_command callable, or skips the test.""" """Returns a connected send_command callable for the testing profile, or skips the test."""
try: try:
send_command("tabs.list") send_command("tabs.list", profile=TEST_BROWSER_PROFILE)
except BrowserNotConnected: except BrowserNotConnected:
pytest.skip("Browser not connected — start Brave/Chrome with the extension loaded") pytest.skip(
return send_command "Browser 'testing' not connected — start Brave/Chrome with the extension loaded for that profile"
)
def _browser(command, args=None):
return send_command(command, args, profile=TEST_BROWSER_PROFILE)
return _browser
@pytest.fixture(scope="session") @pytest.fixture()
def http_tab(browser): def http_tab(browser):
"""Ensures at least one http/https tab is open; returns its tab info.""" """Opens a dedicated http/https tab for the current test and returns its tab info."""
tabs = browser("tabs.list") created = browser("navigate.open", {"url": "https://example.com", "background": True})
http_tab = next( tab_id = created["id"]
(t for t in tabs if t.get("url", "").startswith("http")), None
) tab = None
if http_tab is None: try:
pytest.skip("No http/https tab open — open a web page first") for _ in range(30):
return http_tab tabs = browser("tabs.list")
tab = next((t for t in tabs if t.get("id") == tab_id and t.get("url", "").startswith("http")), None)
if tab is not None:
break
time.sleep(0.1)
if tab is None:
pytest.skip("Dedicated http/https test tab did not finish loading")
yield tab
finally:
try:
browser("tabs.close", {"tabId": tab_id})
except Exception:
pass