|
19 | 19 | from playwright._impl._helper import Error |
20 | 20 |
|
21 | 21 | if TYPE_CHECKING: # pragma: no cover |
| 22 | + from playwright._impl._connection import Connection |
22 | 23 | from playwright._impl._page import Page |
23 | 24 |
|
24 | 25 |
|
25 | 26 | class Video: |
26 | | - def __init__(self, page: "Page") -> None: |
| 27 | + def __init__( |
| 28 | + self, page: "Page", connection: "Connection", artifact: Artifact = None |
| 29 | + ) -> None: |
27 | 30 | self._loop = page._loop |
28 | 31 | self._dispatcher_fiber = page._dispatcher_fiber |
29 | 32 | 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 |
35 | 35 |
|
36 | 36 | def __repr__(self) -> str: |
37 | 37 | return f"<Video page={self._page}>" |
38 | 38 |
|
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 | | - |
47 | 39 | async def path(self) -> pathlib.Path: |
48 | | - if self._page._connection.is_remote: |
| 40 | + if self._is_remote: |
49 | 41 | raise Error( |
50 | 42 | "Path is not available when using browserType.connect(). Use save_as() to save a local copy." |
51 | 43 | ) |
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) |
56 | 47 |
|
57 | 48 | 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) |
66 | 52 |
|
67 | 53 | 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