Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions Browser/keywords/playwright_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,12 +968,7 @@ def new_persistent_context(
traceFile=str(trace_file),
)
)
video_path = self._register_context(
response.id,
trace_file,
self._video_size(params.get("recordVideo"), params.get("viewport")),
json.loads(response.video),
)
self.add_context_and_keyword_call_stack_to_trace(trace_file, response.id)
context_options = self._mask_credentials(
json.loads(response.contextOptions)
)
Expand Down Expand Up @@ -1002,6 +997,11 @@ def new_persistent_context(
self.close_browser()
logger.debug("Teardown: Closed new persistent context.")
raise e
video_path = self._cache_context(
response.id,
self._video_size(params.get("recordVideo"), params.get("viewport")),
json.loads(response.video),
)
return (
browser_id,
response.id,
Expand Down Expand Up @@ -1118,6 +1118,8 @@ def add_context_and_keyword_call_stack_to_trace(self, trace_file, ctx_id):
return None
self.library.tracing_contexts.append(ctx_id)
if self.library.tracing_group_mode == TracingGroupMode.Browser:
if not self.library.keyword_call_stack:
return None
return self.open_trace_group(
**self.library._trace_group_arguments(
self.library.keyword_call_stack[-1]
Expand All @@ -1144,6 +1146,14 @@ def _register_context(
video: dict | None = None,
) -> str:
self.add_context_and_keyword_call_stack_to_trace(trace_file, context_id)
return self._cache_context(context_id, video_size, video)

def _cache_context(
self,
context_id: str,
video_size: ViewportDimensions,
video: dict | None = None,
) -> str:
self.context_cache.add(context_id, video_size)
return self._embed_video(video) if video else ""

Expand Down
67 changes: 67 additions & 0 deletions utest/test_adopted_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,19 @@ def test_playwright_tracing_group_mode_adds_no_trace_groups(tmpdir, monkeypatch)
assert browser.tracing_contexts == []


def test_registered_context_outside_a_keyword_is_traced_without_trace_groups(
tmpdir, monkeypatch
):
browser = library(tmpdir, monkeypatch)
browser.tracing_group_mode = TracingGroupMode.Browser
options = browser.adopted_context_options(tracing=True)

browser.register_adopted_context("context=1", options)

assert not browser._playwright.stub.OpenTraceGroup.called
assert browser.tracing_contexts == ["context=1"]


def test_registered_context_embeds_its_video(tmpdir, monkeypatch):
browser = library(tmpdir, monkeypatch)
options = browser.adopted_context_options(recordVideo={})
Expand Down Expand Up @@ -298,3 +311,57 @@ def test_new_persistent_context_sends_action_annotations(tmpdir, monkeypatch):

options = sent_options(browser._playwright.stub.NewPersistentContext)
assert options["recordVideo"]["showActions"] == {"position": "top"}


def persistent_context_with_video(browser: Browser) -> MagicMock:
stub = browser._playwright.stub
stub.NewPersistentContext.return_value = MagicMock(
id="context=1",
browserId="browser=1",
contextOptions="{}",
video=json.dumps({"video_path": "page.webm", "contextUuid": "context=1"}),
)
return stub


def test_new_persistent_context_caches_and_embeds_its_video_once_its_url_opened(
tmpdir, monkeypatch, caplog
):
caplog.set_level("INFO")
browser = library(tmpdir, monkeypatch)
persistent_context_with_video(browser)

browser.new_persistent_context(url="http://localhost:1", recordVideo={})

assert browser._playwright_state.context_cache.get("context=1") == {
"width": 1280,
"height": 720,
}
assert "<video" in caplog.text


def test_new_persistent_context_is_traced_before_its_url_opens(tmpdir, monkeypatch):
browser = library(tmpdir, monkeypatch)
browser.keyword_call_stack.append(keyword_call("New Persistent Context"))
stub = persistent_context_with_video(browser)
stub.GoTo.side_effect = RuntimeError("net::ERR_CONNECTION_REFUSED")

with pytest.raises(RuntimeError):
browser.new_persistent_context(url="http://localhost:1", tracing=True)

assert stub.OpenTraceGroup.call_args.args[0].contextId == "context=1"


def test_new_persistent_context_is_not_cached_when_its_url_fails_to_open(
tmpdir, monkeypatch, caplog
):
caplog.set_level("INFO")
browser = library(tmpdir, monkeypatch)
stub = persistent_context_with_video(browser)
stub.GoTo.side_effect = RuntimeError("net::ERR_CONNECTION_REFUSED")

with pytest.raises(RuntimeError, match="ERR_CONNECTION_REFUSED"):
browser.new_persistent_context(url="http://localhost:1", recordVideo={})

assert browser._playwright_state.context_cache.get("context=1") is None
assert "<video" not in caplog.text
Loading