_input.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 playwright._impl._connection import Channel
  15. from playwright._impl._helper import MouseButton, locals_to_params
  16. class Keyboard:
  17. def __init__(self, channel: Channel) -> None:
  18. self._channel = channel
  19. self._loop = channel._connection._loop
  20. self._dispatcher_fiber = channel._connection._dispatcher_fiber
  21. async def down(self, key: str) -> None:
  22. await self._channel.send("keyboardDown", None, locals_to_params(locals()))
  23. async def up(self, key: str) -> None:
  24. await self._channel.send("keyboardUp", None, locals_to_params(locals()))
  25. async def insert_text(self, text: str) -> None:
  26. await self._channel.send("keyboardInsertText", None, locals_to_params(locals()))
  27. async def type(self, text: str, delay: float = None) -> None:
  28. await self._channel.send("keyboardType", None, locals_to_params(locals()))
  29. async def press(self, key: str, delay: float = None) -> None:
  30. await self._channel.send("keyboardPress", None, locals_to_params(locals()))
  31. class Mouse:
  32. def __init__(self, channel: Channel) -> None:
  33. self._channel = channel
  34. self._loop = channel._connection._loop
  35. self._dispatcher_fiber = channel._connection._dispatcher_fiber
  36. async def move(self, x: float, y: float, steps: int = None) -> None:
  37. await self._channel.send("mouseMove", None, locals_to_params(locals()))
  38. async def down(
  39. self,
  40. button: MouseButton = None,
  41. clickCount: int = None,
  42. ) -> None:
  43. await self._channel.send("mouseDown", None, locals_to_params(locals()))
  44. async def up(
  45. self,
  46. button: MouseButton = None,
  47. clickCount: int = None,
  48. ) -> None:
  49. await self._channel.send("mouseUp", None, locals_to_params(locals()))
  50. async def _click(
  51. self,
  52. x: float,
  53. y: float,
  54. delay: float = None,
  55. button: MouseButton = None,
  56. clickCount: int = None,
  57. title: str = None,
  58. ) -> None:
  59. await self._channel.send(
  60. "mouseClick", None, locals_to_params(locals()), title=title
  61. )
  62. async def click(
  63. self,
  64. x: float,
  65. y: float,
  66. delay: float = None,
  67. button: MouseButton = None,
  68. clickCount: int = None,
  69. ) -> None:
  70. params = locals()
  71. del params["self"]
  72. await self._click(**params)
  73. async def dblclick(
  74. self,
  75. x: float,
  76. y: float,
  77. delay: float = None,
  78. button: MouseButton = None,
  79. ) -> None:
  80. await self._click(
  81. x, y, delay=delay, button=button, clickCount=2, title="Double click"
  82. )
  83. async def wheel(self, deltaX: float, deltaY: float) -> None:
  84. await self._channel.send("mouseWheel", None, locals_to_params(locals()))
  85. class Touchscreen:
  86. def __init__(self, channel: Channel) -> None:
  87. self._channel = channel
  88. self._loop = channel._connection._loop
  89. self._dispatcher_fiber = channel._connection._dispatcher_fiber
  90. async def tap(self, x: float, y: float) -> None:
  91. await self._channel.send("touchscreenTap", None, locals_to_params(locals()))