Appium+python自動化8-Appium Python API(上)

2021-03-02 從零開始學自動化測試

前言:

    Appium Python API全集,不知道哪個大神整理的,這裡貼出來分享給大家。

1.contexts

contexts(self):

    Returns the contexts within the current session.

    返回當前會話中的上下文,使用後可以識別H5頁面的控制項

    :Usage:

        driver.contexts

用法 driver.contexts

2. current_context

current_context(self):

    Returns the current context of the current session.

    返回當前會話的當前上下文

    :Usage:

        driver.current_context

用法driver. current_context

3. context

context(self):

    Returns the current context of the current session.

    返回當前會話的當前上下文。

    :Usage:

        driver.context

用法driver. Context

4. find_element_by_ios_uiautomation

find_element_by_ios_uiautomation(self, uia_string):

Finds an element by uiautomation in iOS.

    通過iOS uiautomation查找元素

    :Args:

     - uia_string - The element name in the iOS UIAutomation library

    :Usage:

        driver.find_element_by_ios_uiautomation('.elements()[1].cells()[2]')

用法dr. find_element_by_ios_uiautomation(『elements』)

5. find_element_by_accessibility_id

find_element_by_accessibility_id(self, id):

Finds an element by accessibility id.

    通過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()

用法driver.find_element_by_accessibility_id(『id』)

6.scroll

scroll(self, origin_el, destination_el):

Scrolls from one element to another

    從元素origin_el滾動至元素destination_el

    :Args:

     - originalEl - the element from which to being scrolling

     - destinationEl - the element to scroll to

    :Usage:

        driver.scroll(el1, el2)

用法 driver.scroll(el1,el2)

7. drag_and_drop

drag_and_drop(self, origin_el, destination_el):

Drag the origin element to the destination element

    將元素origin_el拖到目標元素destination_el

    :Args:

     - originEl - the element to drag

     - destinationEl - the element to drag to

用法 driver.drag_and_drop(el1,el2)

8.tap

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)

用法 driver.tap([(x,y),(x1,y1)],500)

9. swipe

swipe(self, start_x, start_y, end_x, end_y, duration=None):

Swipe from one point to another point, for an optional duration.

    從A點滑動至B點,滑動時間為毫秒

    :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 - (optional) time to take the swipe, in ms.

    :Usage:

        driver.swipe(100, 100, 100, 400)

用法 driver.swipe(x1,y1,x2,y2,500)

10.flick

flick(self, start_x, start_y, end_x, end_y):

Flick from one point to another point.

    按住A點後快速滑動至B點

    :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)

用法 driver.flick(x1,y1,x2,y2)

11.pinch

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)

用法 driver.pinch(element)

12.zoom

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)

用法 driver.zoom(element)

13.reset

reset(self):

Resets the current application on the device.

重置應用(類似刪除應用數據)

用法 driver.reset()

14. hide_keyboard

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.

    隱藏鍵盤,iOS使用key_name隱藏,安卓不使用參數

    :Args:

     - key_name - key to press

     - strategy - strategy for closing the keyboard (e.g., `tapOutside`)

driver.hide_keyboard()

15. keyevent

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

用法 dr.keyevent(『4』)

16. press_keycode

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

用法 driver.press_ keycode(『4』)

dr.keyevent(『4』)與driver.press_ keycode(『4』) 功能實現上一樣的,都是按了返回鍵

17. long_press_keycode

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

 用法 driver.long_press_keycode(『4』)

18.current_activity

current_activity(self):

Retrieves the current activity on the device.

獲取當前的activity

用法 print(driver.current_activity())

19. wait_activity

wait_activity(self, activity, timeout, interval=1):

Wait for an activity: block until target activity presents or time out.

    This is an Android-only method.

    等待指定的activity出現直到超時,interval為掃描間隔1秒

即每隔幾秒獲取一次當前的activity

返回的True 或 False

    :Agrs:

     - activity - target activity

     - timeout - max wait time, in seconds

     - interval - sleep interval between retries, in seconds

用法driver.wait_activity(『.activity.xxx』,5,2)

20. background_app

background_app(self, seconds):

Puts the application in the background on the device for a certain duration.

    後臺運行app多少秒

    :Args:

     - seconds - the duration for the application to remain in the background

用法 driver.background_app(5)   置後臺5秒後再運行

21.is_app_installed

is_app_installed(self, bundle_id):

Checks whether the application specified by `bundle_id` is installed on the device.

    檢查app是否有安裝

返回 True or False

    :Args:

     - bundle_id - the id of the application to query

用法 driver.is_app_installed(「com.xxxx」)

22.install_app

install_app(self, app_path):

Install the application found at `app_path` on the device.

    安裝app,app_path為安裝包路徑

    :Args:

     - app_path - the local or remote path to the application to install

用法 driver.install_app(app_path)

23.remove_app

remove_app(self, app_id):

Remove the specified application from the device.

    刪除app

    :Args:

     - app_id - the application id to be removed

用法 driver.remove_app(「com.xxx.」)

24.launch_app

launch_app(self):

Start on the device the application specified in the desired capabilities.

啟動app

用法 driver.launch_app()

25.close_app

close_app(self):

Stop the running application, specified in the desired capabilities, on the device.

關閉app

用法 driver.close_app()

啟動和關閉app運行好像會出錯

26. start_activity

start_activity(self, app_package, app_activity, **opts):

Opens an arbitrary activity during a test. If the activity belongs to

    another application, that application is started and the activity is opened.

    This is an Android-only method.

    在測試過程中打開任意活動。如果活動屬於另一個應用程式,該應用程式的啟動和活動被打開。

這是一個安卓的方法

    :Args:

    - app_package - The package containing the activity to start.

    - app_activity - The activity to start.

    - app_wait_package - Begin automation after this package starts (optional).

    - app_wait_activity - Begin automation after this activity starts (optional).

    - intent_action - Intent to start (optional).

    - intent_category - Intent category to start (optional).

    - intent_flags - Flags to send to the intent (optional).

    - optional_intent_arguments - Optional arguments to the intent (optional).

    - stop_app_on_reset - Should the app be stopped on reset (optional)?

用法 driver.start_activity(app_package, app_activity)

27.lock

lock(self, seconds):

Lock the device for a certain period of time. iOS only.

    鎖屏一段時間  iOS專有

    :Args:

     - the duration to lock the device, in seconds

用法 driver.lock()

28.shake

shake(self):

Shake the device.

搖一搖手機

用法 driver.shake()

29.open_notifications

open_notifications(self):

Open notification shade in Android (API Level 18 and above)

打系統通知欄(僅支持API 18 以上的安卓系統)

用法 driver.open_notifications()

30.network_connection

network_connection(self):

Returns an integer bitmask specifying the network connection type.

    Android only.

返回網絡類型  數值

    Possible values are available through the enumeration `appium.webdriver.ConnectionType`

用法 driver.network_connection

31. set_network_connection

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

用法  先加載from appium.webdriver.connectiontype import ConnectionType

dr.set_network_connection(ConnectionType.WIFI_ONLY)

ConnectionType的類型有

NO_CONNECTION = 0

AIRPLANE_MODE = 1

WIFI_ONLY = 2

DATA_ONLY = 4

ALL_NETWORK_ON = 6

32. available_ime_engines

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.

返回安卓設備可用的輸入法

用法print(driver.available_ime_engines)

33.is_ime_active

is_ime_active(self):

Checks whether the device has IME service active. Returns True/False.

    Android only.

檢查設備是否有輸入法服務活動。返回真/假。

安卓

用法 print(driver.is_ime_active())

34.activate_ime_engine

activate_ime_engine(self, engine):

Activates the given IME engine on the device.

    Android only.

    激活安卓設備中的指定輸入法,設備可用輸入法可以從「available_ime_engines」獲取

    :Args:

     - engine - the package and activity of the IME engine to activate (e.g.,

        'com.android.inputmethod.latin/.LatinIME')

用法 driver.activate_ime_engine(「com.android.inputmethod.latin/.LatinIME」)

35.deactivate_ime_engine

deactivate_ime_engine(self):

Deactivates the currently active IME engine on the device.

    Android only.

關閉安卓設備當前的輸入法

用法 driver.deactivate_ime_engine()

36.active_ime_engine

active_ime_engine(self):

Returns the activity and package of the currently active IME engine (e.g.,

    'com.android.inputmethod.latin/.LatinIME').

    Android only.

    返回當前輸入法的包名

用法 driver.active_ime_engine

37. toggle_location_services

toggle_location_services(self):

Toggle the location services on the device. Android only.

打開安卓設備上的位置定位設置

用法 driver.toggle_location_services()

38.set_location

set_location(self, latitude, longitude, altitude):

Set the location of the device

    設置設備的經緯度

    :Args:

     - latitude緯度 - String or numeric value between -90.0 and 90.00

     - longitude經度 - String or numeric value between -180.0 and 180.0

     - altitude海拔高度- String or numeric value

用法 driver.set_location(緯度,經度,高度)

39.tag_name

tag_name(self):

This element's ``tagName`` property.

返回元素的tagName屬性

經實踐返回的是class name

用法 element.tag_name()

40.text

text(self):

The text of the element.

    返回元素的文本值

用法 element.text()

由於篇幅限制,這裡貼出上半部分,下半部分下次發

在學習過程中有遇到疑問的,可以加appium(python+java) QQ群交流:512200893

word版的,在群附件也可以下載到

前面的六篇,可以在我的博客園找到全的:http://www.cnblogs.com/yoyoketang/

相關焦點

  • 【收藏乾貨】基於Appium、Python的自動化測試環境部署和實踐
    1 導言1.1 編制目的該文檔為選用Appium作為行動裝置原生(Native)、混合(Hybrid)、移動Web(Mobile Web)應用UI自動化測試的相關自動化測試人員、開發人員等提供參考。2 概述2.1 Appium設計哲學不需要為了自動化而且重新編譯或修改測試app;不應該讓移動端自動化測試限定在某種語言和某個具體的框架;也就是說任何人都可以使用自己最熟悉最順手的語言以及框架來做移動端自動化測試;不要為了移動端的自動化測試而重新發明輪子,重新寫一套驚天動地的api;也就是說webdriver協議裡的api已經夠好了,拿來改進一下就可以了;
  • Appium自動化測試環境搭建
    Jenkins詳細教程這裡需要注意一點儘量安裝jdk 1.8,防止後面連結手機失敗安裝Node.js安裝nodejs之前,我們需要知道,為什麼要安裝nodejs?安裝Appium Client我們使用python開發appium的自動化測試腳本,肯定離不開appium的一些api庫,所以說這裡的appium 客戶端就是python代碼使用的庫,用來連接appium server 及操作
  • 一套完整的Appium自動化測試環境搭建過程
    Appium是一個開源的自動化測試框架,支持跨平臺,支持多種程式語言,可用於原生,混合和移動web應用程式,使用webdriver驅動ios,android應用程式、那麼為了學習app自動化測試首要任務肯定就是搭建測試開發環境, 因此在這裡記一次搭建Appium自動化測試環境的完整過程,文章較長,需要花費一定的時間,請耐心閱讀,如果文中有什麼錯誤請指正。
  • 記一次搭建Appium自動化測試環境的完整過程
    應用程式、那麼為了學習app自動化測試首要任務肯定就是搭建測試開發環境, 因此在這裡記一次搭建Appium自動化測試環境的完整過程,文章較長,需要花費一定的時間,請耐心閱讀,如果文中有什麼錯誤請指正  JDK環境可以看我之前安裝Jenkins時的隨筆https://www.cnblogs.com/linuxchao/p/linuxchao-jenkins-setup.html,這裡需要注意一點儘量安裝
  • 同時連接多臺手機,使用appium實現app自動化測試
    同時連接多臺手機,使用appium實現app自動化測試使用appium可以實現app自動化測試,我們之前是連接一臺手機去運行,如何同時連接多臺手機呢?很多人可能想到的是多線程(threading)。環境準備appium的安裝:adt,nodejs,appium的python庫,appium serverpytest的安裝:pytestallure的安裝:allure的python庫pytest-allure-adaptor
  • APP自動化測試系列之Appium介紹及運行原理
    Appium是開源的移動端自動化測試框架;Appium可以測試原生的、混合的、以及移動端的web項目;Appium可以測試IOS,Android應用;Appium是跨平臺的,可以用在osx,windows以及linux桌面系統上;Appium支持Ruby、Python、Java、JavaScript、Objective C、php
  • 全網最全最細的appium自動化測試環境搭建教程以及appium工作原理
    appium 自動化測試是很早之前做的,一直想留下點什麼,可是一直抽不出一塊完整的時間來做這件事兒。現在終於有了。反觀各種網際網路的招聘,移動測試已經成了主流,如果再不去學習移動自動化測試技術將已經追不上時代的趕腳!好了,廢話不多說,進入正題!二、Appium簡介
  • Appium用例分模塊與常見錯誤
    urs/bin/python#!_*_ coding:UTF-8 _*_from selenium.common.exceptions import NoSuchElementExceptionfrom appium import webdriverdesired_caps={}
  • Appium自身非必現bug影響自動化測試正常運行?Try-Catch幫您解決
    還有一種情況是測試工具自身的bug,特別是偶現的bug並且開源的自動化測試工具還沒有修復該bug的時候,我們關心的是如何繞過這個已知bug繼續使用appium進行其他場景的自動化測試,本文通過一個實例進行分析,分兩步闡述一、第一步-----------使用appium測試抖音軟體目標
  • Python Appium開啟Android測試之路
    >d:/log.txt  這樣可以將信息指定到某一文件下然後 查看  2、啟動微博  #-*-coding:utf-8-*-  #Time:2017/7/20 18:06  #Author:YangYangJun  import time  from appium import webdriver  desired_caps = {}  desired_caps['platformName'] = 'Android'
  • Appium 1.18.1 發布,移動應用自動化測試工具
    Appium 是一個開源、跨平臺的自動化測試工具,最初主要用於測試原生和輕量移動應用,包括 iOS 和 Android ,目前還支持對 Windows 平臺上的應用的自動化測試。
  • Windows-UI自動化:如何選擇合適的測試框架
    ,而是向PC端+web端+移動端(甚至是不同pc上的不同模擬器端)全方位發展。因此,本篇文章,小編帶領大家認識幾種基於windows系統的UI自動化框架(appium/AirTest/pywinauto),並分析其各自的優劣。
  • APP UI自動化測試思路總結
    首先想要說明一下,APP自動化測試可能很多公司不用,但也是大部分自動化測試工程師、高級測試工程師崗位招聘信息上要求的,所以為了更好的待遇,我們還是需要花時間去掌握的
  • APP UI自動化測試,思路全總結在這裡了
    開源Appium server支持在Windows、macOS、Linux上運行支持多種主流程式語言,如Python、Java、Javascript、Ruby等支持Android、ios、H5自動化測試(集成了UiAutomator、UiAutomation框架)環境搭建較為複雜由網易開發,只支持python
  • [快學Python3]List(列表)
    返回列表中元素最小值list將元組轉換成列表下面我們一起看看上述四個函數的示例:__author__ = u'苦葉子'if __name__ == "__main__":    list_demo = [1, 2, 3, 4, 5, 6, 7, 8,
  • 測試工程師必須要學會的APP UI 自動化都在這裡了!
    首先想要說明一下,APP自動化測試可能很多公司不用,但也是大部分自動化測試工程師、高級測試工程師崗位招聘信息上要求的,所以為了更好的待遇,我們還是需要花時間去掌握的,畢竟誰也不會跟錢過不去。接下來,一起總結一下APP UI自動化測試的思路吧。通常用於自動化測試的程式語言有:Python、Java、Javascript、Ruby、C#、PHP等。
  • appium---uiautomator定位方法
    前言  前面總結了7種定位方法,今天在介紹一種uiautomator方法,其實appium就是基於uiautomator框架實現的,讓我們一起看下uiautomator有哪些定位方法可以使用uiautomator是什麼UIAutomator是android的自動化測試框架,也是Android-Sdk中一個查看頁面組件元素工具uiautomator定位方法前面介紹了常規的定位方法
  • Android + Appium 環境搭建
    .;)2.驗證是否安裝成功:cmd  java -versioncmd  javacmd  javac   一定要看到中文,非亂碼中文坑:版本號1.8      不要有空格,中文和特殊字符,全部用英文命名,路徑不要太複雜 
  • 使用pytest-xdist實現分布式APP自動化測試
    不知道大家有沒有遇到這樣一種情況,實際工作中,app自動化測試的用例可能是成百上千條的,如果放在一臺機器上跑,消耗的時間非常久,那能不能使用分布式的來跑測試用例呢?一般來說對於web自動化測試,使用多核CPU來做分布式比較合適,但對於app自動化測試來說,使用多臺機器做分布式比較合適。這裡主要介紹後者pytest-xdist是工作方式是一個master對應多個worker,每個worker會按照master的要求來執行各自的測試集。
  • Python 裝飾器填坑指南 | 最常見的報錯信息、原因和解決方案
    最近在霍格沃茲測試學院的《Python 測試開發實戰進階》課程中學習了 App 自動化測試框架的異常處理,存在一定重複代碼,正好可以當作題材,拿來練習一下裝飾器。裝飾器學習資料,推薦參考 RealPythonhttps://realpython.com/primer-on-python-decorators/本文主要匯總記錄 Python 裝飾器的常見踩坑經驗,列舉報錯信息、原因和解決方案,供大家參考。