Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.01 KB | None | 0 0
  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.  
  18. """
  19. The Touch Actions implementation
  20. """
  21.  
  22. from selenium.webdriver.remote.command import Command
  23.  
  24. class TouchActions(object):
  25.     """
  26.    Generate touch actions. Works like ActionChains; actions are stored in the
  27.    TouchActions object and are fired with perform().
  28.    """
  29.  
  30.     def __init__(self, driver):
  31.         """
  32.        Creates a new TouchActions object.
  33.  
  34.        :Args:
  35.         - driver: The WebDriver instance which performs user actions.
  36.           It should be with touchscreen enabled.
  37.        """
  38.         self._driver = driver
  39.         self._actions = []
  40.  
  41.     def perform(self):
  42.         """
  43.        Performs all stored actions.
  44.        """
  45.         for action in self._actions:
  46.             action()
  47.  
  48.     def tap(self, on_element):
  49.         """
  50.        Taps on a given element.
  51.  
  52.        :Args:
  53.         - on_element: The element to tap.
  54.        """
  55.         self._actions.append(lambda:
  56.             self._driver.execute(Command.SINGLE_TAP, {'element': on_element.id}))
  57.         return self
  58.  
  59.     def double_tap(self, on_element):
  60.         """
  61.        Double taps on a given element.
  62.  
  63.        :Args:
  64.         - on_element: The element to tap.
  65.        """
  66.         self._actions.append(lambda:
  67.             self._driver.execute(Command.DOUBLE_TAP, {'element': on_element.id}))
  68.         return self
  69.  
  70.     def tap_and_hold(self, xcoord, ycoord):
  71.         """
  72.        Touch down at given coordinates.
  73.  
  74.        :Args:
  75.         - xcoord: X Coordinate to touch down.
  76.         - ycoord: Y Coordinate to touch down.
  77.        """
  78.         self._actions.append(lambda:
  79.             self._driver.execute(Command.TOUCH_DOWN, {
  80.                 'x': int(xcoord),
  81.                 'y': int(ycoord)}))
  82.         return self
  83.  
  84.     def move(self, xcoord, ycoord):
  85.         """
  86.        Move held tap to specified location.
  87.  
  88.        :Args:
  89.         - xcoord: X Coordinate to move.
  90.         - ycoord: Y Coordinate to move.
  91.        """
  92.         self._actions.append(lambda:
  93.             self._driver.execute(Command.TOUCH_MOVE, {
  94.                 'x': int(xcoord),
  95.                 'y': int(ycoord)}))
  96.         return self
  97.  
  98.     def release(self, xcoord, ycoord):
  99.         """
  100.        Release previously issued tap 'and hold' command at specified location.
  101.  
  102.        :Args:
  103.         - xcoord: X Coordinate to release.
  104.         - ycoord: Y Coordinate to release.
  105.        """
  106.         self._actions.append(lambda:
  107.             self._driver.execute(Command.TOUCH_UP, {
  108.                 'x': int(xcoord),
  109.                 'y': int(ycoord)}))
  110.         return self
  111.  
  112.     def scroll(self, xoffset, yoffset):
  113.         """
  114.        Touch and scroll, moving by xoffset and yoffset.
  115.  
  116.        :Args:
  117.         - xoffset: X offset to scroll to.
  118.         - yoffset: Y offset to scroll to.
  119.        """
  120.         self._actions.append(lambda:
  121.             self._driver.execute(Command.TOUCH_SCROLL, {
  122.                 'xoffset': int(xoffset),
  123.                 'yoffset': int(yoffset)}))
  124.         return self
  125.  
  126.     def scroll_from_element(self, on_element, xoffset, yoffset):
  127.         """
  128.        Touch and scroll starting at on_element, moving by xoffset and yoffset.
  129.  
  130.        :Args:
  131.         - on_element: The element where scroll starts.
  132.         - xoffset: X offset to scroll to.
  133.         - yoffset: Y offset to scroll to.
  134.        """
  135.         self._actions.append(lambda:
  136.             self._driver.execute(Command.TOUCH_SCROLL, {
  137.                 'element': on_element.id,
  138.                 'xoffset': int(xoffset),
  139.                 'yoffset': int(yoffset)}))
  140.         return self
  141.  
  142.     def long_press(self, on_element):
  143.         """
  144.        Long press on an element.
  145.  
  146.        :Args:
  147.         - on_element: The element to long press.
  148.        """
  149.         self._actions.append(lambda:
  150.             self._driver.execute(Command.LONG_PRESS, {'element': on_element.id}))
  151.         return self
  152.  
  153.     def flick(self, xspeed, yspeed):
  154.         """
  155.        Flicks, starting anywhere on the screen.
  156.  
  157.        :Args:
  158.         - xspeed: The X speed in pixels per second.
  159.         - yspeed: The Y speed in pixels per second.
  160.        """
  161.         self._actions.append(lambda:
  162.             self._driver.execute(Command.FLICK, {
  163.                 'xspeed': int(xspeed),
  164.                 'yspeed': int(yspeed)}))
  165.         return self
  166.  
  167.     def flick_element(self, on_element, xoffset, yoffset, speed):
  168.         """
  169.        Flick starting at on_element, and moving by the xoffset and yoffset
  170.        with specified speed.
  171.  
  172.        :Args:
  173.         - on_element: Flick will start at center of element.
  174.         - xoffset: X offset to flick to.
  175.         - yoffset: Y offset to flick to.
  176.         - speed: Pixels per second to flick.
  177.        """
  178.         self._actions.append(lambda:
  179.             self._driver.execute(Command.FLICK, {
  180.                 'element': on_element.id,
  181.                 'xoffset': int(xoffset),
  182.                 'yoffset': int(yoffset),
  183.                 'speed': int(speed)}))
  184.         return self
  185.  
  186.     # Context manager so TouchActions can be used in a 'with .. as' statements.
  187.     def __enter__(self):
  188.         return self # Return created instance of self.
  189.  
  190.     def __exit__(self, _type, _value, _traceback):
  191.         pass # Do nothing, does not require additional cleanup.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement