_console_message.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. from asyncio import AbstractEventLoop
  15. from typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, Union
  16. from playwright._impl._api_structures import SourceLocation
  17. from playwright._impl._connection import from_channel, from_nullable_channel
  18. from playwright._impl._js_handle import JSHandle
  19. if TYPE_CHECKING: # pragma: no cover
  20. from playwright._impl._page import Page
  21. from playwright._impl._worker import Worker
  22. class ConsoleMessage:
  23. def __init__(
  24. self, event: Dict, loop: AbstractEventLoop, dispatcher_fiber: Any
  25. ) -> None:
  26. self._event = event
  27. self._loop = loop
  28. self._dispatcher_fiber = dispatcher_fiber
  29. self._page: Optional["Page"] = from_nullable_channel(event.get("page"))
  30. self._worker: Optional["Worker"] = from_nullable_channel(event.get("worker"))
  31. def __repr__(self) -> str:
  32. return f"<ConsoleMessage type={self.type} text={self.text}>"
  33. def __str__(self) -> str:
  34. return self.text
  35. @property
  36. def type(self) -> Union[
  37. Literal["assert"],
  38. Literal["clear"],
  39. Literal["count"],
  40. Literal["debug"],
  41. Literal["dir"],
  42. Literal["dirxml"],
  43. Literal["endGroup"],
  44. Literal["error"],
  45. Literal["info"],
  46. Literal["log"],
  47. Literal["profile"],
  48. Literal["profileEnd"],
  49. Literal["startGroup"],
  50. Literal["startGroupCollapsed"],
  51. Literal["table"],
  52. Literal["time"],
  53. Literal["timeEnd"],
  54. Literal["trace"],
  55. Literal["warning"],
  56. ]:
  57. return self._event["type"]
  58. @property
  59. def text(self) -> str:
  60. return self._event["text"]
  61. @property
  62. def args(self) -> List[JSHandle]:
  63. return list(map(from_channel, self._event["args"]))
  64. @property
  65. def location(self) -> SourceLocation:
  66. return self._event["location"]
  67. @property
  68. def page(self) -> Optional["Page"]:
  69. return self._page
  70. @property
  71. def worker(self) -> Optional["Worker"]:
  72. return self._worker