Skip to content

Commit 96b0ffc

Browse files
committed
video
1 parent e971bb9 commit 96b0ffc

2 files changed

Lines changed: 19 additions & 42 deletions

File tree

playwright/_impl/_page.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ def __init__(
181181
self._video: Optional[Video] = None
182182
self._screencast: Screencast = Screencast(self)
183183
self._opener = cast("Page", from_nullable_channel(initializer.get("opener")))
184+
self._video = Video(
185+
self, self._connection, from_nullable_channel(initializer.get("video"))
186+
)
184187
self._close_reason: Optional[str] = None
185188
self._close_was_called = False
186189
self._har_routers: List[HarRouter] = []
@@ -228,7 +231,6 @@ def __init__(
228231
self._on_web_socket_route(from_channel(params["webSocketRoute"]))
229232
),
230233
)
231-
self._channel.on("video", lambda params: self._on_video(params))
232234
self._channel.on("viewportSizeChanged", self._on_viewport_size_changed)
233235
self._channel.on(
234236
"webSocket",
@@ -360,10 +362,6 @@ def _on_download(self, params: Any) -> None:
360362
Page.Events.Download, Download(self, url, suggested_filename, artifact)
361363
)
362364

363-
def _on_video(self, params: Any) -> None:
364-
artifact = from_channel(params["artifact"])
365-
self._force_video()._artifact_ready(artifact)
366-
367365
def _on_viewport_size_changed(self, params: Any) -> None:
368366
self._viewport_size = params["viewportSize"]
369367

@@ -1196,11 +1194,6 @@ async def pdf(
11961194
await async_writefile(path, decoded_binary)
11971195
return decoded_binary
11981196

1199-
def _force_video(self) -> Video:
1200-
if not self._video:
1201-
self._video = Video(self)
1202-
return self._video
1203-
12041197
@property
12051198
def video(
12061199
self,
@@ -1210,7 +1203,7 @@ def video(
12101203
# too late during launchPersistentContext.
12111204
if not self._browser_context._videos_dir:
12121205
return None
1213-
return self._force_video()
1206+
return self._video
12141207

12151208
@property
12161209
def screencast(self) -> Screencast:

playwright/_impl/_video.py

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,53 +19,37 @@
1919
from playwright._impl._helper import Error
2020

2121
if TYPE_CHECKING: # pragma: no cover
22+
from playwright._impl._connection import Connection
2223
from playwright._impl._page import Page
2324

2425

2526
class Video:
26-
def __init__(self, page: "Page") -> None:
27+
def __init__(
28+
self, page: "Page", connection: "Connection", artifact: Artifact = None
29+
) -> None:
2730
self._loop = page._loop
2831
self._dispatcher_fiber = page._dispatcher_fiber
2932
self._page = page
30-
self._artifact_future = page._loop.create_future()
31-
if page.is_closed():
32-
self._page_closed()
33-
else:
34-
page.on("close", lambda page: self._page_closed())
33+
self._artifact = artifact
34+
self._is_remote = connection.is_remote
3535

3636
def __repr__(self) -> str:
3737
return f"<Video page={self._page}>"
3838

39-
def _page_closed(self) -> None:
40-
if not self._artifact_future.done():
41-
self._artifact_future.set_exception(Error("Page closed"))
42-
43-
def _artifact_ready(self, artifact: Artifact) -> None:
44-
if not self._artifact_future.done():
45-
self._artifact_future.set_result(artifact)
46-
4739
async def path(self) -> pathlib.Path:
48-
if self._page._connection.is_remote:
40+
if self._is_remote:
4941
raise Error(
5042
"Path is not available when using browserType.connect(). Use save_as() to save a local copy."
5143
)
52-
artifact = await self._artifact_future
53-
if not artifact:
54-
raise Error("Page did not produce any video frames")
55-
return artifact.absolute_path
44+
if not self._artifact:
45+
raise Error("Video recording has not been started.")
46+
return pathlib.Path(self._artifact.absolute_path)
5647

5748
async def save_as(self, path: Union[str, pathlib.Path]) -> None:
58-
if self._page._connection._is_sync and not self._page._is_closed:
59-
raise Error(
60-
"Page is not yet closed. Close the page prior to calling save_as"
61-
)
62-
artifact = await self._artifact_future
63-
if not artifact:
64-
raise Error("Page did not produce any video frames")
65-
await artifact.save_as(path)
49+
if not self._artifact:
50+
raise Error("Video recording has not been started.")
51+
await self._artifact.save_as(path)
6652

6753
async def delete(self) -> None:
68-
artifact = await self._artifact_future
69-
if not artifact:
70-
raise Error("Page did not produce any video frames")
71-
await artifact.delete()
54+
if self._artifact:
55+
await self._artifact.delete()

0 commit comments

Comments
 (0)