Skip to content

Commit

Permalink
fix: page.video should be None if not recording (#2410)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Apr 16, 2024
1 parent 57b41b2 commit ad9087a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
16 changes: 12 additions & 4 deletions playwright/_impl/_page.py
Expand Up @@ -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":
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 8 additions & 0 deletions tests/async/test_video.py
Expand Up @@ -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()

0 comments on commit ad9087a

Please sign in to comment.