|
| 1 | +#!/usr/bin/env python |
| 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 | + |
| 15 | + |
| 16 | + |
| 17 | +# The Selenium team implemented a version of the Touch Action API in their code |
| 18 | +# (https://code.google.com/p/selenium/source/browse/py/selenium/webdriver/common/touch_actions.py) |
| 19 | +# but it is deficient in many ways, and does not work in such a way as to be |
| 20 | +# amenable to Appium's use of iOS UIAutomation and Android UIAutomator |
| 21 | +# So it is reimplemented here. |
| 22 | +# |
| 23 | +# Theirs is `TouchActions`. Appium's is `TouchAction`. |
| 24 | + |
| 25 | +from appium.webdriver.mobilecommand import MobileCommand as Command |
| 26 | + |
| 27 | +import copy |
| 28 | + |
| 29 | +class TouchAction(object): |
| 30 | + def __init__(self, driver=None): |
| 31 | + self._driver = driver |
| 32 | + self._actions = [] |
| 33 | + |
| 34 | + def tap(self, element=None, x=None, y=None, count=1): |
| 35 | + """Perform a tap action on the element |
| 36 | +
|
| 37 | + :Args: |
| 38 | + - element - the element to tap |
| 39 | + - x - (optional) x coordinate to tap, relative to the top left corner of the element. |
| 40 | + - y - (optional) y coordinate. If y is used, x must also be set, and vice versa |
| 41 | +
|
| 42 | + :Usage: |
| 43 | + """ |
| 44 | + opts = self._get_opts(element, x, y) |
| 45 | + opts['count'] = count |
| 46 | + self._add_action('tap', opts) |
| 47 | + |
| 48 | + return self |
| 49 | + |
| 50 | + def press(self, el=None, x=None, y=None): |
| 51 | + """Begin a chain with a press down action at a particular element or point |
| 52 | + """ |
| 53 | + self._add_action('press', self._get_opts(el, x, y)) |
| 54 | + |
| 55 | + return self |
| 56 | + |
| 57 | + def long_press(self, el=None, x=None, y=None, duration=1000): |
| 58 | + """Begin a chain with a press down that lasts `duration` milliseconds |
| 59 | + """ |
| 60 | + self._add_action('longPress', self._get_opts(el, x, y)) |
| 61 | + |
| 62 | + return self |
| 63 | + |
| 64 | + def wait(self, ms=0): |
| 65 | + """Pause for `ms` milliseconds. |
| 66 | + """ |
| 67 | + if ms == None: |
| 68 | + ms = 0 |
| 69 | + |
| 70 | + opts = {} |
| 71 | + opts['ms'] = ms |
| 72 | + |
| 73 | + self._add_action('wait', opts) |
| 74 | + |
| 75 | + return self |
| 76 | + |
| 77 | + def move_to(self, el=None, x=None, y=None): |
| 78 | + """Move the pointer from the previous point to the element or point specified |
| 79 | + """ |
| 80 | + self._add_action('moveTo', self._get_opts(el, x, y)) |
| 81 | + |
| 82 | + return self |
| 83 | + |
| 84 | + def release(self): |
| 85 | + """End the action by lifting the pointer off the screen |
| 86 | + """ |
| 87 | + self._add_action('release', {}) |
| 88 | + |
| 89 | + return self |
| 90 | + |
| 91 | + def perform(self): |
| 92 | + """Perform the action by sending the commands to the server to be operated upon |
| 93 | + """ |
| 94 | + params = {} |
| 95 | + params['actions'] = self._actions |
| 96 | + self._driver.execute(Command.TOUCH_ACTION, params) |
| 97 | + |
| 98 | + # get rid of actions so the object can be reused |
| 99 | + self._actions = [] |
| 100 | + |
| 101 | + return self |
| 102 | + |
| 103 | + |
| 104 | + @property |
| 105 | + def json_wire_gestures(self): |
| 106 | + gestures = [] |
| 107 | + for action in self._actions: |
| 108 | + gestures.append(copy.deepcopy(action)) |
| 109 | + return gestures |
| 110 | + |
| 111 | + def _add_action(self, action, options): |
| 112 | + gesture = {} |
| 113 | + gesture['action'] = action |
| 114 | + gesture['options'] = options |
| 115 | + self._actions.append(gesture) |
| 116 | + |
| 117 | + def _get_opts(self, element, x, y): |
| 118 | + opts = {} |
| 119 | + if element != None: |
| 120 | + opts['element'] = element.id |
| 121 | + |
| 122 | + # it makes no sense to have x but no y, or vice versa. |
| 123 | + if (x == None) | (y == None): |
| 124 | + x = None |
| 125 | + y = None |
| 126 | + opts['x'] = x |
| 127 | + opts['y'] = y |
| 128 | + |
| 129 | + return opts |
| 130 | + |
0 commit comments