_local_utils.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 base64
  15. from typing import Dict, List, Optional, cast
  16. from playwright._impl._api_structures import HeadersArray
  17. from playwright._impl._connection import ChannelOwner, StackFrame
  18. from playwright._impl._helper import HarLookupResult, locals_to_params
  19. class LocalUtils(ChannelOwner):
  20. def __init__(
  21. self, parent: ChannelOwner, type: str, guid: str, initializer: Dict
  22. ) -> None:
  23. super().__init__(parent, type, guid, initializer)
  24. self.devices = {
  25. device["name"]: parse_device_descriptor(device["descriptor"])
  26. for device in initializer["deviceDescriptors"]
  27. }
  28. async def zip(self, params: Dict) -> None:
  29. await self._channel.send("zip", None, params)
  30. async def har_open(self, file: str) -> None:
  31. params = locals_to_params(locals())
  32. await self._channel.send("harOpen", None, params)
  33. async def har_lookup(
  34. self,
  35. harId: str,
  36. url: str,
  37. method: str,
  38. headers: HeadersArray,
  39. isNavigationRequest: bool,
  40. postData: Optional[bytes] = None,
  41. ) -> HarLookupResult:
  42. params = locals_to_params(locals())
  43. if "postData" in params:
  44. params["postData"] = base64.b64encode(params["postData"]).decode()
  45. return cast(
  46. HarLookupResult,
  47. await self._channel.send_return_as_dict("harLookup", None, params),
  48. )
  49. async def har_close(self, harId: str) -> None:
  50. params = locals_to_params(locals())
  51. await self._channel.send("harClose", None, params)
  52. async def har_unzip(self, zipFile: str, harFile: str) -> None:
  53. params = locals_to_params(locals())
  54. await self._channel.send("harUnzip", None, params)
  55. async def tracing_started(self, tracesDir: Optional[str], traceName: str) -> str:
  56. params = locals_to_params(locals())
  57. return await self._channel.send("tracingStarted", None, params)
  58. async def trace_discarded(self, stacks_id: str) -> None:
  59. return await self._channel.send("traceDiscarded", None, {"stacksId": stacks_id})
  60. def add_stack_to_tracing_no_reply(self, id: int, frames: List[StackFrame]) -> None:
  61. self._channel.send_no_reply(
  62. "addStackToTracingNoReply",
  63. None,
  64. {
  65. "callData": {
  66. "stack": frames,
  67. "id": id,
  68. }
  69. },
  70. )
  71. def parse_device_descriptor(dict: Dict) -> Dict:
  72. return {
  73. "user_agent": dict["userAgent"],
  74. "viewport": dict["viewport"],
  75. "device_scale_factor": dict["deviceScaleFactor"],
  76. "is_mobile": dict["isMobile"],
  77. "has_touch": dict["hasTouch"],
  78. "default_browser_type": dict["defaultBrowserType"],
  79. }