_video.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright (c) Microsoft Corporation.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import pathlib
  15. from typing import TYPE_CHECKING, Optional, Union
  16. from playwright._impl._artifact import Artifact
  17. from playwright._impl._helper import Error
  18. if TYPE_CHECKING: # pragma: no cover
  19. from playwright._impl._page import Page
  20. class Video:
  21. def __init__(self, page: "Page", artifact: Optional[Artifact]) -> None:
  22. self._loop = page._loop
  23. self._dispatcher_fiber = page._dispatcher_fiber
  24. self._page = page
  25. self._is_remote = page._connection.is_remote
  26. self._artifact = artifact
  27. def __repr__(self) -> str:
  28. return f"<Video page={self._page}>"
  29. async def path(self) -> pathlib.Path:
  30. if self._is_remote:
  31. raise Error(
  32. "Path is not available when using browserType.connect(). Use save_as() to save a local copy."
  33. )
  34. if not self._artifact:
  35. raise Error("Video recording has not been started.")
  36. return self._artifact.absolute_path
  37. async def save_as(self, path: Union[str, pathlib.Path]) -> None:
  38. if self._page._connection._is_sync and not self._page._is_closed:
  39. raise Error(
  40. "Page is not yet closed. Close the page prior to calling save_as"
  41. )
  42. if not self._artifact:
  43. raise Error("Video recording has not been started.")
  44. await self._artifact.save_as(path)
  45. async def delete(self) -> None:
  46. if self._artifact:
  47. await self._artifact.delete()