pointer_input.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Licensed to the Software Freedom Conservancy (SFC) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The SFC licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. from .input_device import InputDevice
  18. from selenium.webdriver.remote.webelement import WebElement
  19. class PointerInput(InputDevice):
  20. DEFAULT_MOVE_DURATION = 250
  21. def __init__(self, type_, name):
  22. super(PointerInput, self).__init__()
  23. self.type = type_
  24. self.name = name
  25. def create_pointer_move(self, duration=DEFAULT_MOVE_DURATION, x=None, y=None, origin=None):
  26. action = dict(type="pointerMove", duration=duration)
  27. action["x"] = x
  28. action["y"] = y
  29. if isinstance(origin, WebElement):
  30. action["origin"] = {"element-6066-11e4-a52e-4f735466cecf": origin.id}
  31. elif origin is not None:
  32. action["origin"] = origin
  33. self.add_action(action)
  34. def create_pointer_down(self, button):
  35. self.add_action({"type": "pointerDown", "duration": 0, "button": button})
  36. def create_pointer_up(self, button):
  37. self.add_action({"type": "pointerUp", "duration": 0, "button": button})
  38. def create_pointer_cancel(self):
  39. self.add_action({"type": "pointerCancel"})
  40. def create_pause(self, pause_duration):
  41. self.add_action({"type": "pause", "duration": pause_duration * 1000})
  42. def encode(self):
  43. return {"type": self.type,
  44. "parameters": {"pointerType": self.name},
  45. "id": self.name,
  46. "actions": [acts for acts in self.actions]}