Skip to content

Commit ad9087a

Browse files
authored
fix: page.video should be None if not recording (#2410)
1 parent 57b41b2 commit ad9087a

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

playwright/_impl/_page.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ def _on_download(self, params: Any) -> None:
311311

312312
def _on_video(self, params: Any) -> None:
313313
artifact = from_channel(params["artifact"])
314-
cast(Video, self.video)._artifact_ready(artifact)
314+
self._force_video()._artifact_ready(artifact)
315315

316316
@property
317317
def context(self) -> "BrowserContext":
@@ -1064,13 +1064,21 @@ async def pdf(
10641064
await async_writefile(path, decoded_binary)
10651065
return decoded_binary
10661066

1067+
def _force_video(self) -> Video:
1068+
if not self._video:
1069+
self._video = Video(self)
1070+
return self._video
1071+
10671072
@property
10681073
def video(
10691074
self,
10701075
) -> Optional[Video]:
1071-
if not self._video:
1072-
self._video = Video(self)
1073-
return self._video
1076+
# Note: we are creating Video object lazily, because we do not know
1077+
# BrowserContextOptions when constructing the page - it is assigned
1078+
# too late during launchPersistentContext.
1079+
if not self._browser_context._options.get("recordVideo"):
1080+
return None
1081+
return self._force_video()
10741082

10751083
def _close_error_with_reason(self) -> TargetClosedError:
10761084
return TargetClosedError(

tests/async/test_video.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,11 @@ async def test_should_not_error_if_page_not_closed_before_save_as(
7676
await saved
7777
await page.context.close()
7878
assert os.path.exists(out_path)
79+
80+
81+
async def test_should_be_None_if_not_recording(
82+
browser: Browser, tmpdir: Path, server: Server
83+
) -> None:
84+
page = await browser.new_page()
85+
assert page.video is None
86+
await page.close()

0 commit comments

Comments
 (0)