forked from appium/python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction_helpers.py
More file actions
189 lines (158 loc) · 7.79 KB
/
action_helpers.py
File metadata and controls
189 lines (158 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING, List, Optional, Tuple
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.actions import interaction
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.mouse_button import MouseButton
from selenium.webdriver.common.actions.pointer_input import PointerInput
from appium.webdriver.webelement import WebElement
if TYPE_CHECKING:
from appium.webdriver.webdriver import WebDriver
class ActionHelpers:
def scroll(self, origin_el: WebElement, destination_el: WebElement, duration: Optional[int] = None) -> 'WebDriver':
"""Scrolls from one element to another
Args:
origin_el: the element from which to being scrolling
destination_el: the element to scroll to
duration: a duration after pressing originalEl and move the element to destinationEl.
Default is 600 ms for W3C spec.
Usage:
driver.scroll(el1, el2)
Returns:
Union['WebDriver', 'ActionHelpers']: Self instance
"""
# XCUITest x W3C spec has no duration by default in server side
if duration is None:
duration = 600
# w3c action requires seconds
duration_sec = duration / 1000
actions = ActionChains(self)
actions.w3c_actions = ActionBuilder(self, mouse=PointerInput(interaction.POINTER_TOUCH, "touch"))
dest_el_rect = destination_el.rect
# https://github.com/SeleniumHQ/selenium/blob/3c82c868d4f2a7600223a1b3817301d0b04d28e4/py/selenium/webdriver/common/actions/pointer_actions.py#L83
if duration_sec is None:
actions.w3c_actions.pointer_action.move_to(origin_el)
actions.w3c_actions.pointer_action.pointer_down()
actions.w3c_actions.pointer_action.move_to_location(dest_el_rect['x'], dest_el_rect['y'])
actions.w3c_actions.pointer_action.release()
actions.perform()
else:
actions.w3c_actions.pointer_action.move_to(origin_el)
actions.w3c_actions.pointer_action.pointer_down()
actions.w3c_actions.pointer_action.pause(duration_sec)
actions.w3c_actions.pointer_action.move_to_location(dest_el_rect['x'], dest_el_rect['y'])
actions.w3c_actions.pointer_action.release()
actions.perform()
return self # type: ignore
def drag_and_drop(self, origin_el: WebElement, destination_el: WebElement) -> 'WebDriver':
"""Drag the origin element to the destination element
Args:
origin_el: the element to drag
destination_el: the element to drag to
Returns:
Union['WebDriver', 'ActionHelpers']: Self instance
"""
actions = ActionChains(self)
# 'mouse' pointer action
actions.w3c_actions.pointer_action.click_and_hold(origin_el)
actions.w3c_actions.pointer_action.move_to(destination_el)
actions.w3c_actions.pointer_action.release()
actions.perform()
return self # type: ignore
def tap(self, positions: List[Tuple[int, int]], duration: Optional[int] = None) -> 'WebDriver':
"""Taps on an particular place with up to five fingers, holding for a
certain time
Args:
positions: an array of tuples representing the x/y coordinates of
the fingers to tap. Length can be up to five.
duration: length of time to tap, in ms
Usage:
driver.tap([(100, 20), (100, 60), (100, 100)], 500)
Returns:
Union['WebDriver', 'ActionHelpers']: Self instance
"""
if len(positions) == 1:
actions = ActionChains(self)
actions.w3c_actions = ActionBuilder(self, mouse=PointerInput(interaction.POINTER_TOUCH, "touch"))
x = positions[0][0]
y = positions[0][1]
actions.w3c_actions.pointer_action.move_to_location(x, y)
actions.w3c_actions.pointer_action.pointer_down()
if duration:
actions.w3c_actions.pointer_action.pause(duration / 1000)
else:
actions.w3c_actions.pointer_action.pause(0.1)
actions.w3c_actions.pointer_action.release()
actions.perform()
else:
finger = 0
actions = ActionChains(self)
actions.w3c_actions.devices = []
for position in positions:
finger += 1
x = position[0]
y = position[1]
# https://github.com/SeleniumHQ/selenium/blob/64447d4b03f6986337d1ca8d8b6476653570bcc1/py/selenium/webdriver/common/actions/pointer_input.py#L24
new_input = actions.w3c_actions.add_pointer_input('touch', f'finger{finger}')
new_input.create_pointer_move(x=x, y=y)
new_input.create_pointer_down(MouseButton.LEFT)
if duration:
new_input.create_pause(duration / 1000)
else:
new_input.create_pause(0.1)
new_input.create_pointer_up(MouseButton.LEFT)
actions.perform()
return self # type: ignore
def swipe(self, start_x: int, start_y: int, end_x: int, end_y: int, duration: int = 0) -> 'WebDriver':
"""Swipe from one point to another point, for an optional duration.
Args:
start_x: x-coordinate at which to start
start_y: y-coordinate at which to start
end_x: x-coordinate at which to stop
end_y: y-coordinate at which to stop
duration: time to take the swipe, in ms.
Usage:
driver.swipe(100, 100, 100, 400)
Returns:
Union['WebDriver', 'ActionHelpers']: Self instance
"""
actions = ActionChains(self)
actions.w3c_actions = ActionBuilder(self, mouse=PointerInput(interaction.POINTER_TOUCH, "touch"))
actions.w3c_actions.pointer_action.move_to_location(start_x, start_y)
actions.w3c_actions.pointer_action.pointer_down()
actions.w3c_actions.pointer_action.pause(duration / 1000)
actions.w3c_actions.pointer_action.move_to_location(end_x, end_y)
actions.w3c_actions.pointer_action.release()
actions.perform()
return self # type: ignore
def flick(self, start_x: int, start_y: int, end_x: int, end_y: int) -> 'WebDriver':
"""Flick from one point to another point.
Args:
start_x: x-coordinate at which to start
start_y: y-coordinate at which to start
end_x: x-coordinate at which to stop
end_y: y-coordinate at which to stop
Usage:
driver.flick(100, 100, 100, 400)
Returns:
Union['WebDriver', 'ActionHelpers']: Self instance
"""
actions = ActionChains(self)
actions.w3c_actions = ActionBuilder(self, mouse=PointerInput(interaction.POINTER_TOUCH, "touch"))
actions.w3c_actions.pointer_action.move_to_location(start_x, start_y)
actions.w3c_actions.pointer_action.pointer_down()
actions.w3c_actions.pointer_action.move_to_location(end_x, end_y)
actions.w3c_actions.pointer_action.release()
actions.perform()
return self # type: ignore