pointer_actions.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 . import interaction
  18. from .interaction import Interaction
  19. from .mouse_button import MouseButton
  20. from .pointer_input import PointerInput
  21. from selenium.webdriver.remote.webelement import WebElement
  22. class PointerActions(Interaction):
  23. def __init__(self, source=None):
  24. if source is None:
  25. source = PointerInput(interaction.POINTER, "mouse")
  26. self.source = source
  27. super(PointerActions, self).__init__(source)
  28. def pointer_down(self, button=MouseButton.LEFT):
  29. self._button_action("create_pointer_down", button=button)
  30. def pointer_up(self, button=MouseButton.LEFT):
  31. self._button_action("create_pointer_up", button=button)
  32. def move_to(self, element, x=None, y=None):
  33. if not isinstance(element, WebElement):
  34. raise AttributeError("move_to requires a WebElement")
  35. if x is not None or y is not None:
  36. el_rect = element.rect
  37. left_offset = el_rect['width'] / 2
  38. top_offset = el_rect['height'] / 2
  39. left = -left_offset + (x or 0)
  40. top = -top_offset + (y or 0)
  41. else:
  42. left = 0
  43. top = 0
  44. self.source.create_pointer_move(origin=element, x=int(left), y=int(top))
  45. return self
  46. def move_by(self, x, y):
  47. self.source.create_pointer_move(origin=interaction.POINTER, x=int(x), y=int(y))
  48. return self
  49. def move_to_location(self, x, y):
  50. self.source.create_pointer_move(origin='viewport', x=int(x), y=int(y))
  51. return self
  52. def click(self, element=None):
  53. if element:
  54. self.move_to(element)
  55. self.pointer_down(MouseButton.LEFT)
  56. self.pointer_up(MouseButton.LEFT)
  57. return self
  58. def context_click(self, element=None):
  59. if element:
  60. self.move_to(element)
  61. self.pointer_down(MouseButton.RIGHT)
  62. self.pointer_up(MouseButton.RIGHT)
  63. return self
  64. def click_and_hold(self, element=None):
  65. if element:
  66. self.move_to(element)
  67. self.pointer_down()
  68. return self
  69. def release(self):
  70. self.pointer_up()
  71. return self
  72. def double_click(self, element=None):
  73. if element:
  74. self.move_to(element)
  75. self.click()
  76. self.click()
  77. def pause(self, duration=0):
  78. self.source.create_pause(duration)
  79. return self
  80. def _button_action(self, action, button=MouseButton.LEFT):
  81. meth = getattr(self.source, action)
  82. meth(button)
  83. return self