forked from appium/python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebdriver.py
More file actions
728 lines (616 loc) · 25.4 KB
/
webdriver.py
File metadata and controls
728 lines (616 loc) · 25.4 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
#!/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 selenium import webdriver
from .connectiontype import ConnectionType
from .mobilecommand import MobileCommand as Command
from .errorhandler import MobileErrorHandler
from .switch_to import MobileSwitchTo
from .webelement import WebElement as MobileWebElement
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.common.multi_action import MultiAction
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
class WebDriver(webdriver.Remote):
def __init__(self, command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities=None, browser_profile=None, proxy=None, keep_alive=False):
super(WebDriver, self).__init__(command_executor, desired_capabilities, browser_profile, proxy, keep_alive)
if self.command_executor is not None:
self._addCommands()
self.error_handler = MobileErrorHandler()
self._switch_to = MobileSwitchTo(self)
# add new method to the `find_by_*` pantheon
By.IOS_UIAUTOMATION = MobileBy.IOS_UIAUTOMATION
By.ANDROID_UIAUTOMATOR = MobileBy.ANDROID_UIAUTOMATOR
By.ACCESSIBILITY_ID = MobileBy.ACCESSIBILITY_ID
# add methods to the WebElement class
WebElement.set_value = set_value
@property
def contexts(self):
"""
Returns the contexts within the current session.
:Usage:
driver.contexts
"""
return self.execute(Command.CONTEXTS)['value']
@property
def current_context(self):
"""
Returns the current context of the current session.
:Usage:
driver.current_context
"""
return self.execute(Command.GET_CURRENT_CONTEXT)['value']
@property
def context(self):
"""
Returns the current context of the current session.
:Usage:
driver.context
"""
return self.current_context
def find_element_by_ios_uiautomation(self, uia_string):
"""Finds an element by uiautomation in iOS.
:Args:
- uia_string - The element name in the iOS UIAutomation library
:Usage:
driver.find_element_by_ios_uiautomation('.elements()[1].cells()[2]')
"""
return self.find_element(by=By.IOS_UIAUTOMATION, value=uia_string)
def find_elements_by_ios_uiautomation(self, uia_string):
"""Finds elements by uiautomation in iOS.
:Args:
- uia_string - The element name in the iOS UIAutomation library
:Usage:
driver.find_elements_by_ios_uiautomation('.elements()[1].cells()[2]')
"""
return self.find_elements(by=By.IOS_UIAUTOMATION, value=uia_string)
def find_element_by_android_uiautomator(self, uia_string):
"""Finds element by uiautomator in Android.
:Args:
- uia_string - The element name in the Android UIAutomator library
:Usage:
driver.find_element_by_android_uiautomator('.elements()[1].cells()[2]')
"""
return self.find_element(by=By.ANDROID_UIAUTOMATOR, value=uia_string)
def find_elements_by_android_uiautomator(self, uia_string):
"""Finds elements by uiautomator in Android.
:Args:
- uia_string - The element name in the Android UIAutomator library
:Usage:
driver.find_elements_by_android_uiautomator('.elements()[1].cells()[2]')
"""
return self.find_elements(by=By.ANDROID_UIAUTOMATOR, value=uia_string)
def find_element_by_accessibility_id(self, id):
"""Finds an element by accessibility id.
:Args:
- id - a string corresponding to a recursive element search using the
Id/Name that the native Accessibility options utilize
:Usage:
driver.find_element_by_accessibility_id()
"""
return self.find_element(by=By.ACCESSIBILITY_ID, value=id)
def find_elements_by_accessibility_id(self, id):
"""Finds elements by accessibility id.
:Args:
- id - a string corresponding to a recursive element search using the
Id/Name that the native Accessibility options utilize
:Usage:
driver.find_elements_by_accessibility_id()
"""
return self.find_elements(by=By.ACCESSIBILITY_ID, value=id)
def create_web_element(self, element_id):
"""
Creates a web element with the specified element_id.
Overrides method in Selenium WebDriver in order to always give them
Appium WebElement
"""
return MobileWebElement(self, element_id)
# convenience method added to Appium (NOT Selenium 3)
def scroll(self, origin_el, destination_el):
"""Scrolls from one element to another
:Args:
- originalEl - the element from which to being scrolling
- destinationEl - the element to scroll to
:Usage:
driver.scroll(el1, el2)
"""
action = TouchAction(self)
action.press(origin_el).move_to(destination_el).release().perform()
return self
# convenience method added to Appium (NOT Selenium 3)
def drag_and_drop(self, origin_el, destination_el):
"""Drag the origin element to the destination element
:Args:
- originEl - the element to drag
- destinationEl - the element to drag to
"""
action = TouchAction(self)
action.long_press(origin_el).move_to(destination_el).release().perform()
return self
# convenience method added to Appium (NOT Selenium 3)
def tap(self, positions, duration=None):
"""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 - (optional) length of time to tap, in ms
:Usage:
driver.tap([(100, 20), (100, 60), (100, 100)], 500)
"""
if len(positions) == 1:
action = TouchAction(self)
x = positions[0][0]
y = positions[0][1]
if duration:
duration = duration
action.long_press(x=x, y=y, duration=duration).release()
else:
action.tap(x=x, y=y).release()
action.perform()
else:
ma = MultiAction(self)
for position in positions:
x = position[0]
y = position[1]
action = TouchAction(self)
if duration:
duration *= 1000 # we take seconds, but send milliseconds
action.long_press(x=x, y=y, duration=duration).release()
else:
action.press(x=x, y=y).release()
ma.add(action)
ma.perform()
return self
# convenience method added to Appium (NOT Selenium 3)
def swipe(self, start_x, start_y, end_x, end_y, duration=None):
"""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 end
- end_x - x-coordinate at which to stop
- end_y - y-coordinate at which to stop
- duration - (optional) time to take the swipe, in ms.
:Usage:
driver.swipe(100, 100, 100, 400)
"""
# `swipe` is something like press-wait-move_to-release, which the server
# will translate into the correct action
action = TouchAction(self)
action \
.press(x=start_x, y=start_y) \
.wait(ms=duration) \
.move_to(x=end_x, y=end_y) \
.release()
action.perform()
return self
# convenience method added to Appium (NOT Selenium 3)
def flick(self, start_x, start_y, end_x, end_y):
"""Flick from one point to another point.
:Args:
- start_x - x-coordinate at which to start
- start_y - y-coordinate at which to end
- end_x - x-coordinate at which to stop
- end_y - y-coordinate at which to stop
:Usage:
driver.flick(100, 100, 100, 400)
"""
action = TouchAction(self)
action \
.press(x=start_x, y=start_y) \
.move_to(x=end_x, y=end_y) \
.release()
action.perform()
return self
# convenience method added to Appium (NOT Selenium 3)
def pinch(self, element=None, percent=200, steps=50):
"""Pinch on an element a certain amount
:Args:
- element - the element to pinch
- percent - (optional) amount to pinch. Defaults to 200%
- steps - (optional) number of steps in the pinch action
:Usage:
driver.pinch(element)
"""
if element:
element = element.id
opts = {
'element': element,
'percent': percent,
'steps': steps,
}
self.execute_script('mobile: pinchClose', opts)
return self
# convenience method added to Appium (NOT Selenium 3)
def zoom(self, element=None, percent=200, steps=50):
"""Zooms in on an element a certain amount
:Args:
- element - the element to zoom
- percent - (optional) amount to zoom. Defaults to 200%
- steps - (optional) number of steps in the zoom action
:Usage:
driver.zoom(element)
"""
if element:
element = element.id
opts = {
'element': element,
'percent': percent,
'steps': steps,
}
self.execute_script('mobile: pinchOpen', opts)
return self
def app_strings(self, language=None):
"""Returns the application strings from the device for the specified
language.
:Args:
- language - strings language code
"""
data = {}
if language != None:
data['language'] = language
return self.execute(Command.GET_APP_STRINGS, data)['value']
def reset(self):
"""Resets the current application on the device.
"""
self.execute(Command.RESET)
return self
def hide_keyboard(self, key_name=None, key=None, strategy=None):
"""Hides the software keyboard on the device. In iOS, use `key_name` to press
a particular key, or `strategy`. In Android, no parameters are used.
:Args:
- key_name - key to press
- strategy - strategy for closing the keyboard (e.g., `tapOutside`)
"""
data = {}
if key_name is not None:
data['keyName'] = key_name
elif key is not None:
data['key'] = key
else:
# defaults to `tapOutside` strategy
strategy = 'tapOutside'
data['strategy'] = strategy
self.execute(Command.HIDE_KEYBOARD, data)
return self
# TODO: remove when new Appium is out
def keyevent(self, keycode, metastate=None):
"""Sends a keycode to the device. Android only. Possible keycodes can be
found in http://developer.android.com/reference/android/view/KeyEvent.html.
:Args:
- keycode - the keycode to be sent to the device
- metastate - meta information about the keycode being sent
"""
data = {
'keycode': keycode,
}
if metastate is not None:
data['metastate'] = metastate
self.execute(Command.KEY_EVENT, data)
return self
def press_keycode(self, keycode, metastate=None):
"""Sends a keycode to the device. Android only. Possible keycodes can be
found in http://developer.android.com/reference/android/view/KeyEvent.html.
:Args:
- keycode - the keycode to be sent to the device
- metastate - meta information about the keycode being sent
"""
data = {
'keycode': keycode,
}
if metastate is not None:
data['metastate'] = metastate
self.execute(Command.PRESS_KEYCODE, data)
return self
def long_press_keycode(self, keycode, metastate=None):
"""Sends a long press of keycode to the device. Android only. Possible keycodes can be
found in http://developer.android.com/reference/android/view/KeyEvent.html.
:Args:
- keycode - the keycode to be sent to the device
- metastate - meta information about the keycode being sent
"""
data = {
'keycode': keycode
}
if metastate != None:
data['metastate'] = metastate
self.execute(Command.LONG_PRESS_KEYCODE, data)
return self
@property
def current_activity(self):
"""Retrieves the current activity on the device.
"""
return self.execute(Command.GET_CURRENT_ACTIVITY)['value']
def set_value(self, element, value):
"""Set the value on an element in the application.
:Args:
- element - the element whose value will be set
- Value - the value to set on the element
"""
data = {
'elementId': element.id,
'value': [value],
}
self.execute(Command.SET_IMMEDIATE_VALUE, data)
return self
def pull_file(self, path):
"""Retrieves the file at `path`. Returns the file's content encoded as
Base64.
:Args:
- path - the path to the file on the device
"""
data = {
'path': path,
}
return self.execute(Command.PULL_FILE, data)['value']
def pull_folder(self, path):
"""Retrieves a folder at `path`. Returns the folder's contents zipped
and encoded as Base64.
:Args:
- path - the path to the folder on the device
"""
data = {
'path': path,
}
return self.execute(Command.PULL_FOLDER, data)['value']
def push_file(self, path, base64data):
"""Puts the data, encoded as Base64, in the file specified as `path`.
:Args:
- path - the path on the device
- base64data - data, encoded as Base64, to be written to the file
"""
data = {
'path': path,
'data': base64data,
}
self.execute(Command.PUSH_FILE, data)
return self
def complex_find(self, selector):
"""Performs a find for elements in the current application.
:Args:
- selector - an array of selection criteria
"""
data = {
'selector': selector,
}
return self.execute(Command.COMPLEX_FIND, data)['value']
def background_app(self, seconds):
"""Puts the application in the background on the device for a certain
duration.
:Args:
- seconds - the duration for the application to remain in the background
"""
data = {
'seconds': seconds,
}
self.execute(Command.BACKGROUND, data)
return self
def is_app_installed(self, bundle_id):
"""Checks whether the application specified by `bundle_id` is installed
on the device.
:Args:
- bundle_id - the id of the application to query
"""
data = {
'bundleId': bundle_id,
}
return self.execute(Command.IS_APP_INSTALLED, data)['value']
def install_app(self, app_path):
"""Install the application found at `app_path` on the device.
:Args:
- app_path - the local or remote path to the application to install
"""
data = {
'appPath': app_path,
}
self.execute(Command.INSTALL_APP, data)
return self
def remove_app(self, app_id):
"""Remove the specified application from the device.
:Args:
- app_id - the application id to be removed
"""
data = {
'appId': app_id,
}
self.execute(Command.REMOVE_APP, data)
return self
def launch_app(self):
"""Start on the device the application specified in the desired capabilities.
"""
self.execute(Command.LAUNCH_APP)
return self
def close_app(self):
"""Stop the running application, specified in the desired capabilities, on
the device.
"""
self.execute(Command.CLOSE_APP)
return self
def end_test_coverage(self, intent, path):
"""Ends the coverage collection and pull the coverage.ec file from the device.
Android only.
See https://github.com/appium/appium/blob/master/docs/en/android_coverage.md
:Args:
- intent - description of operation to be performed
- path - path to coverage.ec file to be pulled from the device
"""
data = {
'intent': intent,
'path': path,
}
return self.execute(Command.END_TEST_COVERAGE, data)['value']
def lock(self, seconds):
"""Lock the device for a certain period of time. iOS only.
:Args:
- the duration to lock the device, in seconds
"""
data = {
'seconds': seconds,
}
self.execute(Command.LOCK, data)
return self
def shake(self):
"""Shake the device.
"""
self.execute(Command.SHAKE)
return self
def open_notifications(self):
"""Open notification shade in Android (API Level 18 and above)
"""
self.execute(Command.OPEN_NOTIFICATIONS, {})
return self
@property
def network_connection(self):
"""Returns an integer bitmask specifying the network connection type.
Android only.
Possible values are available through the enumeration `appium.webdriver.ConnectionType`
"""
return self.execute(Command.GET_NETWORK_CONNECTION, {})['value']
def set_network_connection(self, connectionType):
"""Sets the network connection type. Android only.
Possible values:
Value (Alias) | Data | Wifi | Airplane Mode
-------------------------------------------------
0 (None) | 0 | 0 | 0
1 (Airplane Mode) | 0 | 0 | 1
2 (Wifi only) | 0 | 1 | 0
4 (Data only) | 1 | 0 | 0
6 (All network on) | 1 | 1 | 0
These are available through the enumeration `appium.webdriver.ConnectionType`
:Args:
- connectionType - a member of the enum appium.webdriver.ConnectionType
"""
data = {
'parameters': {
'type': connectionType.value
}
}
return self.execute(Command.SET_NETWORK_CONNECTION, data)['value']
@property
def available_ime_engines(self):
"""Get the available input methods for an Android device. Package and
activity are returned (e.g., ['com.android.inputmethod.latin/.LatinIME'])
Android only.
"""
return self.execute(Command.GET_AVAILABLE_IME_ENGINES, {})['value']
def is_ime_active(self):
"""Checks whether the device has IME service active. Returns True/False.
Android only.
"""
return self.execute(Command.IS_IME_ACTIVE, {})['value']
def activate_ime_engine(self, engine):
"""Activates the given IME engine on the device.
Android only.
:Args:
- engine - the package and activity of the IME engine to activate (e.g.,
'com.android.inputmethod.latin/.LatinIME')
"""
data = {
'engine': engine
}
self.execute(Command.ACTIVATE_IME_ENGINE, data)
return self
def deactivate_ime_engine(self):
"""Deactivates the currently active IME engine on the device.
Android only.
"""
self.execute(Command.DEACTIVATE_IME_ENGINE, {})
return self
@property
def active_ime_engine(self):
"""Returns the activity and package of the currently active IME engine (e.g.,
'com.android.inputmethod.latin/.LatinIME').
Android only.
"""
return self.execute(Command.GET_ACTIVE_IME_ENGINE, {})['value']
def _addCommands(self):
self.command_executor._commands[Command.CONTEXTS] = \
('GET', '/session/$sessionId/contexts')
self.command_executor._commands[Command.GET_CURRENT_CONTEXT] = \
('GET', '/session/$sessionId/context')
self.command_executor._commands[Command.SWITCH_TO_CONTEXT] = \
('POST', '/session/$sessionId/context')
self.command_executor._commands[Command.TOUCH_ACTION] = \
('POST', '/session/$sessionId/touch/perform')
self.command_executor._commands[Command.MULTI_ACTION] = \
('POST', '/session/$sessionId/touch/multi/perform')
self.command_executor._commands[Command.GET_APP_STRINGS] = \
('POST', '/session/$sessionId/appium/app/strings')
# TODO: remove when new Appium is out
self.command_executor._commands[Command.KEY_EVENT] = \
('POST', '/session/$sessionId/appium/device/keyevent')
self.command_executor._commands[Command.PRESS_KEYCODE] = \
('POST', '/session/$sessionId/appium/device/press_keycode')
self.command_executor._commands[Command.LONG_PRESS_KEYCODE] = \
('POST', '/session/$sessionId/appium/device/long_press_keycode')
self.command_executor._commands[Command.GET_CURRENT_ACTIVITY] = \
('GET', '/session/$sessionId/appium/device/current_activity')
self.command_executor._commands[Command.SET_IMMEDIATE_VALUE] = \
('POST', '/session/$sessionId/appium/element/$elementId/value')
self.command_executor._commands[Command.PULL_FILE] = \
('POST', '/session/$sessionId/appium/device/pull_file')
self.command_executor._commands[Command.PULL_FOLDER] = \
('POST', '/session/$sessionId/appium/device/pull_folder')
self.command_executor._commands[Command.PUSH_FILE] = \
('POST', '/session/$sessionId/appium/device/push_file')
self.command_executor._commands[Command.COMPLEX_FIND] = \
('POST', '/session/$sessionId/appium/app/complex_find')
self.command_executor._commands[Command.BACKGROUND] = \
('POST', '/session/$sessionId/appium/app/background')
self.command_executor._commands[Command.IS_APP_INSTALLED] = \
('POST', '/session/$sessionId/appium/device/app_installed')
self.command_executor._commands[Command.INSTALL_APP] = \
('POST', '/session/$sessionId/appium/device/install_app')
self.command_executor._commands[Command.REMOVE_APP] = \
('POST', '/session/$sessionId/appium/device/remove_app')
self.command_executor._commands[Command.LAUNCH_APP] = \
('POST', '/session/$sessionId/appium/app/launch')
self.command_executor._commands[Command.CLOSE_APP] = \
('POST', '/session/$sessionId/appium/app/close')
self.command_executor._commands[Command.END_TEST_COVERAGE] = \
('POST', '/session/$sessionId/appium/app/end_test_coverage')
self.command_executor._commands[Command.LOCK] = \
('POST', '/session/$sessionId/appium/device/lock')
self.command_executor._commands[Command.SHAKE] = \
('POST', '/session/$sessionId/appium/device/shake')
self.command_executor._commands[Command.RESET] = \
('POST', '/session/$sessionId/appium/app/reset')
self.command_executor._commands[Command.HIDE_KEYBOARD] = \
('POST', '/session/$sessionId/appium/device/hide_keyboard')
self.command_executor._commands[Command.OPEN_NOTIFICATIONS] = \
('POST', '/session/$sessionId/appium/device/open_notifications')
self.command_executor._commands[Command.GET_NETWORK_CONNECTION] = \
('GET', '/session/$sessionId/network_connection')
self.command_executor._commands[Command.SET_NETWORK_CONNECTION] = \
('POST', '/session/$sessionId/network_connection')
self.command_executor._commands[Command.GET_AVAILABLE_IME_ENGINES] = \
('GET', '/session/$sessionId/ime/available_engines')
self.command_executor._commands[Command.IS_IME_ACTIVE] = \
('GET', '/session/$sessionId/ime/activated')
self.command_executor._commands[Command.ACTIVATE_IME_ENGINE] = \
('POST', '/session/$sessionId/ime/activate')
self.command_executor._commands[Command.DEACTIVATE_IME_ENGINE] = \
('POST', '/session/$sessionId/ime/deactivate')
self.command_executor._commands[Command.GET_ACTIVE_IME_ENGINE] = \
('GET', '/session/$sessionId/ime/active_engine')
# monkeypatched method for WebElement
def set_value(self, value):
"""Set the value on this element in the application
"""
data = {
'elementId': self.id,
'value': [value],
}
self._execute(Command.SET_IMMEDIATE_VALUE, data)
return self