wabot/tests/unit/test_reattach.py
Mathew Sir Guest the best 68978a34e7 style: apply ruff format across the codebase
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-08 23:12:23 -06:00

55 lines
2.2 KiB
Python

from selenium.common.exceptions import WebDriverException
from wabot.hosts import ReattachingRemote, attach, build_options
class TestReattachingRemote:
def test_adopts_session_id_without_creating_a_session(self):
# constructing against a closed port (127.0.0.1:1) proves no HTTP happens
# until the first command
driver = ReattachingRemote(
"http://127.0.0.1:1", "saved-session-id", options=build_options("chromium")
)
# base __init__ resets session_id to None BEFORE start_session runs;
# our override must still win
assert driver.session_id == "saved-session-id"
assert isinstance(driver.caps, dict)
def test_works_for_firefox_options_too(self):
driver = ReattachingRemote("http://127.0.0.1:1", "sid", options=build_options("firefox"))
assert driver.session_id == "sid"
class TestAttach:
def test_returns_driver_when_probe_succeeds(self, monkeypatch):
monkeypatch.setattr(
ReattachingRemote, "current_url", property(lambda self: "http://example.com")
)
driver = attach("http://127.0.0.1:1", "sid", "chromium")
assert driver is not None
assert driver.session_id == "sid"
def test_returns_none_when_session_is_dead(self, monkeypatch):
def boom(self):
raise WebDriverException("invalid session id")
monkeypatch.setattr(ReattachingRemote, "current_url", property(boom))
assert attach("http://127.0.0.1:1", "sid", "chromium") is None
def test_returns_none_when_server_is_unreachable(self):
# no server on port 1: connection refused is immediate; attach must not raise
assert attach("http://127.0.0.1:1", "sid", "chromium") is None
def test_dead_session_closes_executor(self, monkeypatch):
from selenium.webdriver.remote.remote_connection import RemoteConnection
closed = []
monkeypatch.setattr(RemoteConnection, "close", lambda self: closed.append(1))
def boom(self):
raise WebDriverException("invalid session id")
monkeypatch.setattr(ReattachingRemote, "current_url", property(boom))
assert attach("http://127.0.0.1:1", "sid", "chromium") is None
assert closed == [1] # executor was closed, not leaked