action_builder.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 selenium.webdriver.remote.command import Command
  18. from . import interaction
  19. from .key_actions import KeyActions
  20. from .key_input import KeyInput
  21. from .pointer_actions import PointerActions
  22. from .pointer_input import PointerInput
  23. class ActionBuilder(object):
  24. def __init__(self, driver, mouse=None, keyboard=None):
  25. if mouse is None:
  26. mouse = PointerInput(interaction.POINTER, "mouse")
  27. if keyboard is None:
  28. keyboard = KeyInput(interaction.KEY)
  29. self.devices = [mouse, keyboard]
  30. self._key_action = KeyActions(keyboard)
  31. self._pointer_action = PointerActions(mouse)
  32. self.driver = driver
  33. def get_device_with(self, name):
  34. try:
  35. idx = self.devices.index(name)
  36. return self.devices[idx]
  37. except:
  38. pass
  39. @property
  40. def pointer_inputs(self):
  41. return [device for device in self.devices if device.type == interaction.POINTER]
  42. @property
  43. def key_inputs(self):
  44. return [device for device in self.devices if device.type == interaction.KEY]
  45. @property
  46. def key_action(self):
  47. return self._key_action
  48. @property
  49. def pointer_action(self):
  50. return self._pointer_action
  51. def add_key_input(self, name):
  52. new_input = KeyInput(name)
  53. self._add_input(new_input)
  54. return new_input
  55. def add_pointer_input(self, type_, name):
  56. new_input = PointerInput(type_, name)
  57. self._add_input(new_input)
  58. return new_input
  59. def perform(self):
  60. enc = {"actions": []}
  61. for device in self.devices:
  62. encoded = device.encode()
  63. if encoded['actions']:
  64. enc["actions"].append(encoded)
  65. self.driver.execute(Command.W3C_ACTIONS, enc)
  66. def clear_actions(self):
  67. self.driver.execute(Command.W3C_CLEAR_ACTIONS)
  68. def _add_input(self, input):
  69. self.devices.append(input)