新手学appium-python-client+appium api 1-from appium import webdriver

  • Post author:
  • Post category:python
  1 #各位客官,虽然今天是周末,我还是奋斗到工作的第一线
  2 #研究python-client,也就是api,哈哈,大家自己翻译吧,我把代码贴出来
  3 @property
  4     def contexts(self):
  5         """
  6         Returns the contexts within the current session.
  7 
  8         :Usage:
  9             driver.contexts
 10         """
 11         return self.execute(Command.CONTEXTS)['value']
 12 
 13     @property
 14     def current_context(self):
 15         """
 16         Returns the current context of the current session.
 17 
 18         :Usage:
 19             driver.current_context
 20         """
 21         return self.execute(Command.GET_CURRENT_CONTEXT)['value']
 22 
 23     @property
 24     def context(self):
 25         """
 26         Returns the current context of the current session.
 27 
 28         :Usage:
 29             driver.context
 30         """
 31         return self.current_context
 32 
 33     def find_element_by_ios_uiautomation(self, uia_string):
 34         """Finds an element by uiautomation in iOS.
 35 
 36         :Args:
 37          - uia_string - The element name in the iOS UIAutomation library
 38 
 39         :Usage:
 40             driver.find_element_by_ios_uiautomation('.elements()[1].cells()[2]')
 41         """
 42         return self.find_element(by=By.IOS_UIAUTOMATION, value=uia_string)
 43 
 44     def find_elements_by_ios_uiautomation(self, uia_string):
 45         """Finds elements by uiautomation in iOS.
 46 
 47         :Args:
 48          - uia_string - The element name in the iOS UIAutomation library
 49 
 50         :Usage:
 51             driver.find_elements_by_ios_uiautomation('.elements()[1].cells()[2]')
 52         """
 53         return self.find_elements(by=By.IOS_UIAUTOMATION, value=uia_string)
 54 
 55     def find_element_by_android_uiautomator(self, uia_string):
 56         """Finds element by uiautomator in Android.
 57 
 58         :Args:
 59          - uia_string - The element name in the Android UIAutomator library
 60 
 61         :Usage:
 62             driver.find_element_by_android_uiautomator('.elements()[1].cells()[2]')
 63         """
 64         return self.find_element(by=By.ANDROID_UIAUTOMATOR, value=uia_string)
 65 
 66     def find_elements_by_android_uiautomator(self, uia_string):
 67         """Finds elements by uiautomator in Android.
 68 
 69         :Args:
 70          - uia_string - The element name in the Android UIAutomator library
 71 
 72         :Usage:
 73             driver.find_elements_by_android_uiautomator('.elements()[1].cells()[2]')
 74         """
 75         return self.find_elements(by=By.ANDROID_UIAUTOMATOR, value=uia_string)
 76 
 77     def find_element_by_accessibility_id(self, id):
 78         """Finds an element by accessibility id.
 79 
 80         :Args:
 81          - id - a string corresponding to a recursive element search using the
 82          Id/Name that the native Accessibility options utilize
 83 
 84         :Usage:
 85             driver.find_element_by_accessibility_id()
 86         """
 87         return self.find_element(by=By.ACCESSIBILITY_ID, value=id)
 88 
 89     def find_elements_by_accessibility_id(self, id):
 90         """Finds elements by accessibility id.
 91 
 92         :Args:
 93          - id - a string corresponding to a recursive element search using the
 94          Id/Name that the native Accessibility options utilize
 95 
 96         :Usage:
 97             driver.find_elements_by_accessibility_id()
 98         """
 99         return self.find_elements(by=By.ACCESSIBILITY_ID, value=id)
100 
101     def create_web_element(self, element_id):
102         """
103         Creates a web element with the specified element_id.
104         Overrides method in Selenium WebDriver in order to always give them
105         Appium WebElement
106         """
107         return MobileWebElement(self, element_id)
108 
109     # convenience method added to Appium (NOT Selenium 3)
110     def scroll(self, origin_el, destination_el):
111         """Scrolls from one element to another
112 
113         :Args:
114          - originalEl - the element from which to being scrolling
115          - destinationEl - the element to scroll to
116 
117         :Usage:
118             driver.scroll(el1, el2)
119         """
120         action = TouchAction(self)
121         action.press(origin_el).move_to(destination_el).release().perform()
122         return self
123 
124     # convenience method added to Appium (NOT Selenium 3)
125     def drag_and_drop(self, origin_el, destination_el):
126         """Drag the origin element to the destination element
127 
128         :Args:
129          - originEl - the element to drag
130          - destinationEl - the element to drag to
131         """
132         action = TouchAction(self)
133         action.long_press(origin_el).move_to(destination_el).release().perform()
134         return self
135 
136     # convenience method added to Appium (NOT Selenium 3)
137     def tap(self, positions, duration=None):
138         """Taps on an particular place with up to five fingers, holding for a
139         certain time
140 
141         :Args:
142          - positions - an array of tuples representing the x/y coordinates of
143          the fingers to tap. Length can be up to five.
144          - duration - (optional) length of time to tap, in ms
145 
146         :Usage:
147             driver.tap([(100, 20), (100, 60), (100, 100)], 500)
148         """
149         if len(positions) == 1:
150             action = TouchAction(self)
151             x = positions[0][0]
152             y = positions[0][1]
153             if duration:
154                 duration = duration
155                 action.long_press(x=x, y=y, duration=duration).release()
156             else:
157                 action.press(x=x, y=y).release()
158             action.perform()
159         else:
160             ma = MultiAction(self)
161             for position in positions:
162                 x = position[0]
163                 y = position[1]
164                 action = TouchAction(self)
165                 if duration:
166                     duration *= 1000  # we take seconds, but send milliseconds
167                     action.long_press(x=x, y=y, duration=duration).release()
168                 else:
169                     action.press(x=x, y=y).release()
170                 ma.add(action)
171 
172             ma.perform()
173         return self
174 
175     # convenience method added to Appium (NOT Selenium 3)
176     def swipe(self, start_x, start_y, end_x, end_y, duration=None):
177         """Swipe from one point to another point, for an optional duration.
178 
179         :Args:
180          - start_x - x-coordinate at which to start
181          - start_y - y-coordinate at which to end
182          - end_x - x-coordinate at which to stop
183          - end_y - y-coordinate at which to stop
184          - duration - (optional) time to take the swipe, in ms.
185 
186         :Usage:
187             driver.swipe(100, 100, 100, 400)
188         """
189         # `swipe` is something like press-wait-move_to-release, which the server
190         # will translate into the correct action
191         action = TouchAction(self)
192         action \
193             .press(x=start_x, y=start_y) \
194             .wait(ms=duration) \
195             .move_to(x=end_x, y=end_y) \
196             .release()
197         action.perform()
198         return self
199 
200     # convenience method added to Appium (NOT Selenium 3)
201     def flick(self, start_x, start_y, end_x, end_y):
202         """Flick from one point to another point.
203 
204         :Args:
205          - start_x - x-coordinate at which to start
206          - start_y - y-coordinate at which to end
207          - end_x - x-coordinate at which to stop
208          - end_y - y-coordinate at which to stop
209 
210         :Usage:
211             driver.flick(100, 100, 100, 400)
212         """
213         action = TouchAction(self)
214         action \
215             .press(x=start_x, y=start_y) \
216             .move_to(x=end_x, y=end_y) \
217             .release()
218         action.perform()
219         return self
220 
221     # convenience method added to Appium (NOT Selenium 3)
222     def pinch(self, element=None, percent=200, steps=50):
223         """Pinch on an element a certain amount
224 
225         :Args:
226          - element - the element to pinch
227          - percent - (optional) amount to pinch. Defaults to 200%
228          - steps - (optional) number of steps in the pinch action
229 
230         :Usage:
231             driver.pinch(element)
232         """
233         if element:
234             element = element.id
235 
236         opts = {
237             'element': element,
238             'percent': percent,
239             'steps': steps,
240         }
241         self.execute_script('mobile: pinchClose', opts)
242         return self
243 
244     # convenience method added to Appium (NOT Selenium 3)
245     def zoom(self, element=None, percent=200, steps=50):
246         """Zooms in on an element a certain amount
247 
248         :Args:
249          - element - the element to zoom
250          - percent - (optional) amount to zoom. Defaults to 200%
251          - steps - (optional) number of steps in the zoom action
252 
253         :Usage:
254             driver.zoom(element)
255         """
256         if element:
257             element = element.id
258 
259         opts = {
260             'element': element,
261             'percent': percent,
262             'steps': steps,
263         }
264         self.execute_script('mobile: pinchOpen', opts)
265         return self
266 
267     def app_strings(self, language=None):
268         """Returns the application strings from the device for the specified
269         language.
270 
271         :Args:
272          - language - strings language code
273         """
274         data = {}
275         if language != None:
276             data['language'] = language
277         return self.execute(Command.GET_APP_STRINGS, data)['value']
278 
279     def reset(self):
280         """Resets the current application on the device.
281         """
282         self.execute(Command.RESET)
283         return self
284 
285     def hide_keyboard(self, key_name=None):
286         """Hides the software keyboard on the device, using the specified key to
287         press. If no key name is given, the keyboard is closed by moving focus
288         from the text field. iOS only.
289         """
290         data = {}
291         if key_name is not None:
292             data['keyName'] = key_name
293         self.execute(Command.HIDE_KEYBOARD, data)
294         return self
295 
296     # TODO: remove when new Appium is out
297     def keyevent(self, keycode, metastate=None):
298         """Sends a keycode to the device. Android only. Possible keycodes can be
299         found in http://developer.android.com/reference/android/view/KeyEvent.html.
300 
301         :Args:
302          - keycode - the keycode to be sent to the device
303          - metastate - meta information about the keycode being sent
304         """
305         data = {
306             'keycode': keycode,
307         }
308         if metastate is not None:
309             data['metastate'] = metastate
310         self.execute(Command.KEY_EVENT, data)
311         return self
312 
313     def press_keycode(self, keycode, metastate=None):
314         """Sends a keycode to the device. Android only. Possible keycodes can be
315         found in http://developer.android.com/reference/android/view/KeyEvent.html.
316 
317         :Args:
318          - keycode - the keycode to be sent to the device
319          - metastate - meta information about the keycode being sent
320         """
321         data = {
322             'keycode': keycode,
323         }
324         if metastate is not None:
325             data['metastate'] = metastate
326         self.execute(Command.PRESS_KEYCODE, data)
327         return self
328 
329     def long_press_keycode(self, keycode, metastate=None):
330         """Sends a long press of keycode to the device. Android only. Possible keycodes can be
331         found in http://developer.android.com/reference/android/view/KeyEvent.html.
332 
333         :Args:
334          - keycode - the keycode to be sent to the device
335          - metastate - meta information about the keycode being sent
336         """
337         data = {
338             'keycode': keycode
339         }
340         if metastate != None:
341             data['metastate'] = metastate
342         self.execute(Command.LONG_PRESS_KEYCODE, data)
343         return self
344 
345     @property
346     def current_activity(self):
347         """Retrieves the current activity on the device.
348         """
349         return self.execute(Command.GET_CURRENT_ACTIVITY)['value']
350 
351     def set_value(self, element, value):
352         """Set the value on an element in the application.
353 
354         :Args:
355          - element - the element whose value will be set
356          - Value - the value to set on the element
357         """
358         data = {
359             'elementId': element.id,
360             'value': [value],
361         }
362         self.execute(Command.SET_IMMEDIATE_VALUE, data)
363         return self
364 
365     def pull_file(self, path):
366         """Retrieves the file at `path`. Returns the file's content encoded as
367         Base64.
368 
369         :Args:
370          - path - the path to the file on the device
371         """
372         data = {
373             'path': path,
374         }
375         return self.execute(Command.PULL_FILE, data)['value']
376 
377     def push_file(self, path, base64data):
378         """Puts the data, encoded as Base64, in the file specified as `path`.
379 
380         :Args:
381          - path - the path on the device
382          - base64data - data, encoded as Base64, to be written to the file
383         """
384         data = {
385             'path': path,
386             'data': base64data,
387         }
388         self.execute(Command.PUSH_FILE, data)
389         return self
390 
391     def complex_find(self, selector):
392         """Performs a find for elements in the current application.
393 
394         :Args:
395          - selector - an array of selection criteria
396         """
397         data = {
398             'selector': selector,
399         }
400         return self.execute(Command.COMPLEX_FIND, data)['value']
401 
402     def background_app(self, seconds):
403         """Puts the application in the background on the device for a certain
404         duration.
405 
406         :Args:
407          - seconds - the duration for the application to remain in the background
408         """
409         data = {
410             'seconds': seconds,
411         }
412         self.execute(Command.BACKGROUND, data)
413         return self
414 
415     def is_app_installed(self, bundle_id):
416         """Checks whether the application specified by `bundle_id` is installed
417         on the device.
418 
419         :Args:
420          - bundle_id - the id of the application to query
421         """
422         data = {
423             'bundleId': bundle_id,
424         }
425         return self.execute(Command.IS_APP_INSTALLED, data)['value']
426 
427     def install_app(self, app_path):
428         """Install the application found at `app_path` on the device.
429 
430         :Args:
431          - app_path - the local or remote path to the application to install
432         """
433         data = {
434             'appPath': app_path,
435         }
436         self.execute(Command.INSTALL_APP, data)
437         return self
438 
439     def remove_app(self, app_id):
440         """Remove the specified application from the device.
441 
442         :Args:
443          - app_id - the application id to be removed
444         """
445         data = {
446             'appId': app_id,
447         }
448         self.execute(Command.REMOVE_APP, data)
449         return self
450 
451     def launch_app(self):
452         """Start on the device the application specified in the desired capabilities.
453         """
454         self.execute(Command.LAUNCH_APP)
455         return self
456 
457     def close_app(self):
458         """Stop the running application, specified in the desired capabilities, on
459         the device.
460         """
461         self.execute(Command.CLOSE_APP)
462         return self
463 
464     def end_test_coverage(self, intent, path):
465         """Ends the coverage collection and pull the coverage.ec file from the device.
466         Android only.
467 
468         See https://github.com/appium/appium/blob/master/docs/en/android_coverage.md
469 
470         :Args:
471          - intent - description of operation to be performed
472          - path - path to coverage.ec file to be pulled from the device
473         """
474         data = {
475             'intent': intent,
476             'path': path,
477         }
478         return self.execute(Command.END_TEST_COVERAGE, data)['value']
479 
480     def lock(self, seconds):
481         """Lock the device for a certain period of time. iOS only.
482 
483         :Args:
484          - the duration to lock the device, in seconds
485         """
486         data = {
487             'seconds': seconds,
488         }
489         self.execute(Command.LOCK, data)
490         return self
491 
492     def shake(self):
493         """Shake the device.
494         """
495         self.execute(Command.SHAKE)
496         return self
497 
498     def open_notifications(self):
499         """Open notification shade in Android (API Level 18 and above)
500         """
501         self.execute(Command.OPEN_NOTIFICATIONS, {})
502         return self
503 
504     def _addCommands(self):
505         self.command_executor._commands[Command.CONTEXTS] = \
506             ('GET', '/session/$sessionId/contexts')
507         self.command_executor._commands[Command.GET_CURRENT_CONTEXT] = \
508             ('GET', '/session/$sessionId/context')
509         self.command_executor._commands[Command.SWITCH_TO_CONTEXT] = \
510             ('POST', '/session/$sessionId/context')
511         self.command_executor._commands[Command.TOUCH_ACTION] = \
512             ('POST', '/session/$sessionId/touch/perform')
513         self.command_executor._commands[Command.MULTI_ACTION] = \
514             ('POST', '/session/$sessionId/touch/multi/perform')
515         self.command_executor._commands[Command.GET_APP_STRINGS] = \
516             ('POST', '/session/$sessionId/appium/app/strings')
517         # TODO: remove when new Appium is out
518         self.command_executor._commands[Command.KEY_EVENT] = \
519             ('POST', '/session/$sessionId/appium/device/keyevent')
520         self.command_executor._commands[Command.PRESS_KEYCODE] = \
521             ('POST', '/session/$sessionId/appium/device/press_keycode')
522         self.command_executor._commands[Command.LONG_PRESS_KEYCODE] = \
523             ('POST', '/session/$sessionId/appium/device/long_press_keycode')
524         self.command_executor._commands[Command.GET_CURRENT_ACTIVITY] = \
525             ('GET', '/session/$sessionId/appium/device/current_activity')
526         self.command_executor._commands[Command.SET_IMMEDIATE_VALUE] = \
527             ('POST', '/session/$sessionId/appium/element/$elementId/value')
528         self.command_executor._commands[Command.PULL_FILE] = \
529             ('POST', '/session/$sessionId/appium/device/pull_file')
530         self.command_executor._commands[Command.PUSH_FILE] = \
531             ('POST', '/session/$sessionId/appium/device/push_file')
532         self.command_executor._commands[Command.COMPLEX_FIND] = \
533             ('POST', '/session/$sessionId/appium/app/complex_find')
534         self.command_executor._commands[Command.BACKGROUND] = \
535             ('POST', '/session/$sessionId/appium/app/background')
536         self.command_executor._commands[Command.IS_APP_INSTALLED] = \
537             ('POST', '/session/$sessionId/appium/device/app_installed')
538         self.command_executor._commands[Command.INSTALL_APP] = \
539             ('POST', '/session/$sessionId/appium/device/install_app')
540         self.command_executor._commands[Command.REMOVE_APP] = \
541             ('POST', '/session/$sessionId/appium/device/remove_app')
542         self.command_executor._commands[Command.LAUNCH_APP] = \
543             ('POST', '/session/$sessionId/appium/app/launch')
544         self.command_executor._commands[Command.CLOSE_APP] = \
545             ('POST', '/session/$sessionId/appium/app/close')
546         self.command_executor._commands[Command.END_TEST_COVERAGE] = \
547             ('POST', '/session/$sessionId/appium/app/end_test_coverage')
548         self.command_executor._commands[Command.LOCK] = \
549             ('POST', '/session/$sessionId/appium/device/lock')
550         self.command_executor._commands[Command.SHAKE] = \
551             ('POST', '/session/$sessionId/appium/device/shake')
552         self.command_executor._commands[Command.RESET] = \
553             ('POST', '/session/$sessionId/appium/app/reset')
554         self.command_executor._commands[Command.HIDE_KEYBOARD] = \
555             ('POST', '/session/$sessionId/appium/device/hide_keyboard')
556         self.command_executor._commands[Command.OPEN_NOTIFICATIONS] = \
557             ('POST', '/session/$sessionId/appium/device/open_notifications')

 

转载于:https://www.cnblogs.com/wyx123/articles/4488966.html