From ad9087a8f7ba9c0efac418fdfd2517d674a3c338 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Tue, 16 Apr 2024 23:37:40 +0200 Subject: [PATCH] fix: page.video should be None if not recording (#2410) --- playwright/_impl/_page.py | 16 ++++++++++++---- tests/async/test_video.py | 8 ++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/playwright/_impl/_page.py b/playwright/_impl/_page.py index db6cf13b8..8efbaf164 100644 --- a/playwright/_impl/_page.py +++ b/playwright/_impl/_page.py @@ -311,7 +311,7 @@ def _on_download(self, params: Any) -> None: def _on_video(self, params: Any) -> None: artifact = from_channel(params["artifact"]) - cast(Video, self.video)._artifact_ready(artifact) + self._force_video()._artifact_ready(artifact) @property def context(self) -> "BrowserContext": @@ -1064,13 +1064,21 @@ async def pdf( await async_writefile(path, decoded_binary) return decoded_binary + def _force_video(self) -> Video: + if not self._video: + self._video = Video(self) + return self._video + @property def video( self, ) -> Optional[Video]: - if not self._video: - self._video = Video(self) - return self._video + # Note: we are creating Video object lazily, because we do not know + # BrowserContextOptions when constructing the page - it is assigned + # too late during launchPersistentContext. + if not self._browser_context._options.get("recordVideo"): + return None + return self._force_video() def _close_error_with_reason(self) -> TargetClosedError: return TargetClosedError( diff --git a/tests/async/test_video.py b/tests/async/test_video.py index 8575aabad..b0ab4c529 100644 --- a/tests/async/test_video.py +++ b/tests/async/test_video.py @@ -76,3 +76,11 @@ async def test_should_not_error_if_page_not_closed_before_save_as( await saved await page.context.close() assert os.path.exists(out_path) + + +async def test_should_be_None_if_not_recording( + browser: Browser, tmpdir: Path, server: Server +) -> None: + page = await browser.new_page() + assert page.video is None + await page.close()