_artifact.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 pathlib import Path
  16. from typing import Dict, Optional, Union, cast
  17. from playwright._impl._connection import ChannelOwner, from_channel
  18. from playwright._impl._helper import Error, make_dirs_for_file, patch_error_message
  19. from playwright._impl._stream import Stream
  20. class Artifact(ChannelOwner):
  21. def __init__(
  22. self, parent: ChannelOwner, type: str, guid: str, initializer: Dict
  23. ) -> None:
  24. super().__init__(parent, type, guid, initializer)
  25. self.absolute_path = initializer["absolutePath"]
  26. async def path_after_finished(self) -> pathlib.Path:
  27. if self._connection.is_remote:
  28. raise Error(
  29. "Path is not available when using browser_type.connect(). Use save_as() to save a local copy."
  30. )
  31. path = await self._channel.send(
  32. "pathAfterFinished",
  33. None,
  34. )
  35. return pathlib.Path(path)
  36. async def save_as(self, path: Union[str, Path]) -> None:
  37. stream = cast(
  38. Stream,
  39. from_channel(
  40. await self._channel.send(
  41. "saveAsStream",
  42. None,
  43. )
  44. ),
  45. )
  46. make_dirs_for_file(path)
  47. await stream.save_as(path)
  48. async def failure(self) -> Optional[str]:
  49. reason = await self._channel.send(
  50. "failure",
  51. None,
  52. )
  53. if reason is None:
  54. return None
  55. return patch_error_message(reason)
  56. async def delete(self) -> None:
  57. await self._channel.send(
  58. "delete",
  59. None,
  60. )
  61. async def read_info_buffer(self) -> bytes:
  62. stream = cast(
  63. Stream,
  64. from_channel(
  65. await self._channel.send(
  66. "stream",
  67. None,
  68. )
  69. ),
  70. )
  71. buffer = await stream.read_all()
  72. return buffer
  73. async def cancel(self) -> None: # pyright: ignore[reportIncompatibleMethodOverride]
  74. await self._channel.send(
  75. "cancel",
  76. None,
  77. )