diff --git a/tests/conftest.py b/tests/conftest.py index 4425c72..573d4fe 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,30 +1,52 @@ """ 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. """ +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, or skips the test.""" + """Returns a connected send_command callable for the testing profile, or skips the test.""" try: - send_command("tabs.list") + send_command("tabs.list", profile=TEST_BROWSER_PROFILE) except BrowserNotConnected: - pytest.skip("Browser not connected — start Brave/Chrome with the extension loaded") - return send_command + pytest.skip( + "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): - """Ensures at least one http/https tab is open; returns its tab info.""" - tabs = browser("tabs.list") - http_tab = next( - (t for t in tabs if t.get("url", "").startswith("http")), None - ) - if http_tab is None: - pytest.skip("No http/https tab open — open a web page first") - return http_tab + """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