_tracing.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 Dict, Optional, Union, cast
  16. from playwright._impl._api_structures import TracingGroupLocation
  17. from playwright._impl._artifact import Artifact
  18. from playwright._impl._connection import ChannelOwner, from_nullable_channel
  19. from playwright._impl._helper import locals_to_params
  20. class Tracing(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._include_sources: bool = False
  26. self._stacks_id: Optional[str] = None
  27. self._is_tracing: bool = False
  28. self._traces_dir: Optional[str] = None
  29. async def start(
  30. self,
  31. name: str = None,
  32. title: str = None,
  33. snapshots: bool = None,
  34. screenshots: bool = None,
  35. sources: bool = None,
  36. ) -> None:
  37. params = locals_to_params(locals())
  38. self._include_sources = bool(sources)
  39. await self._channel.send("tracingStart", None, params)
  40. trace_name = await self._channel.send(
  41. "tracingStartChunk", None, {"title": title, "name": name}
  42. )
  43. await self._start_collecting_stacks(trace_name)
  44. async def start_chunk(self, title: str = None, name: str = None) -> None:
  45. params = locals_to_params(locals())
  46. trace_name = await self._channel.send("tracingStartChunk", None, params)
  47. await self._start_collecting_stacks(trace_name)
  48. async def _start_collecting_stacks(self, trace_name: str) -> None:
  49. if not self._is_tracing:
  50. self._is_tracing = True
  51. self._connection.set_is_tracing(True)
  52. self._stacks_id = await self._connection.local_utils.tracing_started(
  53. self._traces_dir, trace_name
  54. )
  55. async def stop_chunk(self, path: Union[pathlib.Path, str] = None) -> None:
  56. await self._do_stop_chunk(path)
  57. async def stop(self, path: Union[pathlib.Path, str] = None) -> None:
  58. await self._do_stop_chunk(path)
  59. await self._channel.send(
  60. "tracingStop",
  61. None,
  62. )
  63. async def _do_stop_chunk(self, file_path: Union[pathlib.Path, str] = None) -> None:
  64. self._reset_stack_counter()
  65. if not file_path:
  66. # Not interested in any artifacts
  67. await self._channel.send("tracingStopChunk", None, {"mode": "discard"})
  68. if self._stacks_id:
  69. await self._connection.local_utils.trace_discarded(self._stacks_id)
  70. return
  71. is_local = not self._connection.is_remote
  72. if is_local:
  73. result = await self._channel.send_return_as_dict(
  74. "tracingStopChunk", None, {"mode": "entries"}
  75. )
  76. await self._connection.local_utils.zip(
  77. {
  78. "zipFile": str(file_path),
  79. "entries": result["entries"],
  80. "stacksId": self._stacks_id,
  81. "mode": "write",
  82. "includeSources": self._include_sources,
  83. }
  84. )
  85. return
  86. result = await self._channel.send_return_as_dict(
  87. "tracingStopChunk",
  88. None,
  89. {
  90. "mode": "archive",
  91. },
  92. )
  93. artifact = cast(
  94. Optional[Artifact],
  95. from_nullable_channel(result.get("artifact")),
  96. )
  97. # The artifact may be missing if the browser closed while stopping tracing.
  98. if not artifact:
  99. if self._stacks_id:
  100. await self._connection.local_utils.trace_discarded(self._stacks_id)
  101. return
  102. # Save trace to the final local file.
  103. await artifact.save_as(file_path)
  104. await artifact.delete()
  105. await self._connection.local_utils.zip(
  106. {
  107. "zipFile": str(file_path),
  108. "entries": [],
  109. "stacksId": self._stacks_id,
  110. "mode": "append",
  111. "includeSources": self._include_sources,
  112. }
  113. )
  114. def _reset_stack_counter(self) -> None:
  115. if self._is_tracing:
  116. self._is_tracing = False
  117. self._connection.set_is_tracing(False)
  118. async def group(self, name: str, location: TracingGroupLocation = None) -> None:
  119. await self._channel.send("tracingGroup", None, locals_to_params(locals()))
  120. async def group_end(self) -> None:
  121. await self._channel.send(
  122. "tracingGroupEnd",
  123. None,
  124. )