Files
browser-cli/tests/conftest.py
T

53 lines
1.7 KiB
Python

"""
Shared pytest fixtures for browser-cli integration tests.
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.
"""
import time
import pytest
from browser_cli.client import send_command, BrowserNotConnected
TEST_BROWSER_PROFILE = "testing"
@pytest.fixture(scope="session")
def browser():
"""Returns a connected send_command callable for the testing profile, or skips the test."""
try:
send_command("tabs.list", profile=TEST_BROWSER_PROFILE)
except (BrowserNotConnected, RuntimeError) as e:
pytest.skip(
f"Browser 'testing' not connected — start Brave/Chrome with the extension loaded for that profile ({e})"
)
def _browser(command, args=None):
return send_command(command, args, profile=TEST_BROWSER_PROFILE)
return _browser
@pytest.fixture()
def http_tab(browser):
"""Opens a dedicated http/https tab for the current test and returns its tab info."""
created = browser("navigate.open", {"url": "https://example.com", "background": True})
tab_id = created["id"]
tab = None
try:
for _ in range(30):
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