Appium_Python_APIjavascript
contexts(self):css
Returns the contexts within the current session.html
返回當前會話中的上下文,使用後能夠識別H5頁面的控件java
:Usage:android
driver.contextsios
用法 driver.contextsgit
current_context(self):web
Returns the current context of the current session.canvas
返回當前會話的當前上下文網絡
:Usage:
driver.current_context
用法driver. current_context
context(self):
Returns the current context of the current session.
返回當前會話的當前上下文。
:Usage:
driver.context
用法driver. Context
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’)
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’)
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)
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)
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)
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)
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)
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)
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)
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()
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’)
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’) 功能實現上同樣的,都是按了返回鍵
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())
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)
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秒後再運行
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」)
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)
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.」)
launch_app(self):
Start on the device the application specified in the desired capabilities.
啓動app
用法 driver.launch_app()
close_app(self):
Stop the running application, specified in the desired capabilities, on the device.
關閉app
用法 driver.close_app()
啓動和關閉app運行好像會出錯
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)
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()
open_notifications(self):
Open notification shade in Android (API Level 18 and above)
打系統通知欄(僅支持API 18 以上的安卓系統)
用法 driver.open_notifications()
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
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
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)
is_ime_active(self):
Checks whether the device has IME service active. Returns True/False.
Android only.
檢查設備是否有輸入法服務活動。返回真/假。
安卓
用法 print(driver.is_ime_active())
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」)
deactivate_ime_engine(self):
Deactivates the currently active IME engine on the device.
Android only.
關閉安卓設備當前的輸入法
用法 driver.deactivate_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
toggle_location_services(self):
Toggle the location services on the device. Android only.
打開安卓設備上的位置定位設置
用法 driver.toggle_location_services()
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(緯度,經度,高度)
tag_name(self):
This element's ``tagName`` property.
返回元素的tagName屬性
經實踐返回的是class name
用法 element.tag_name()
text(self):
The text of the element.
返回元素的文本值
用法 element.text()
click(self):
Clicks the element.
點擊元素
用法 element.click()
submit(self):
Submits a form.
提交表單
用法 暫無
clear(self):
Clears the text if it's a text entry element.
清除輸入的內容
用法 element.clear()
get_attribute(self, name):
Gets the given attribute or property of the element.
1、獲取 content-desc 的方法爲 get_attribute("name") ,並且還不能保證返回的必定是 content-desc (content-desc 爲空時會返回 text 屬性值)
2、get_attribute 方法不是咱們在 uiautomatorviewer 看到的全部屬性都能獲取的(此處的名稱均爲使用 get_attribute 時使用的屬性名稱):
可獲取的:
字符串類型:
name(返回 content-desc 或 text)
text(返回 text)
className(返回 class,只有 API=>18 才能支持)
resourceId(返回 resource-id,只有 API=>18 才能支持)
This method will first try to return the value of a property with the
given name. If a property with that name doesn't exist, it returns the
value of the attribute with the same name. If there's no attribute with
that name, ``None`` is returned.
Values which are considered truthy, that is equals "true" or "false",
are returned as booleans. All other non-``None`` values are returned
as strings. For attributes or properties which do not exist, ``None``
is returned.
:Args:
- name - Name of the attribute/property to retrieve.
Example::
# Check if the "active" CSS class is applied to an element.
is_active = "active" in target_element.get_attribute("class")
用法 暫無
is_selected(self):
Returns whether the element is selected.
Can be used to check if a checkbox or radio button is selected.
返回元素是否選擇。
能夠用來檢查一個複選框或單選按鈕被選中。
用法 element.is_slected()
is_enabled(self):
Returns whether the element is enabled.
返回元素是否可用True of False
用法 element.is_enabled()
find_element_by_id(self, id_):
Finds element within this element's children by ID.
經過元素的ID定位元素
:Args:
- id_ - ID of child element to locate.
用法 driver. find_element_by_id(「id」)
find_elements_by_id(self, id_):
Finds a list of elements within this element's children by ID.
經過元素ID定位,含有該屬性的全部元素
:Args:
- id_ - Id of child element to find.
用法 driver. find_elements_by_id(「id」)
find_element_by_name(self, name):
Finds element within this element's children by name.
經過元素Name定位(元素的名稱屬性text)
:Args:
- name - name property of the element to find.
用法 driver.find_element_by_name(「name」)
find_elements_by_name(self, name):
Finds a list of elements within this element's children by name.
經過元素Name定位(元素的名稱屬性text),含有該屬性的全部元素
:Args:
- name - name property to search for.
用法 driver.find_element_by_name(「name」)
find_element_by_link_text(self, link_text):
Finds element within this element's children by visible link text.
經過元素可見連接文本定位
:Args:
- link_text - Link text string to search for.
用法 driver.find_element_by_link_text(「text」)
find_element_by_link_text(self, link_text):
Finds a list of elements within this element's children by visible link text
經過元素可見連接文本定位,含有該屬性的全部元素
:Args:
- link_text - Link text string to search for.
用法 driver.find_elements_by_link_text(「text」)
find_element_by_partial_link_text(self, link_text):
Finds element within this element's children by partially visible link text.
經過元素部分可見連接文本定位
:Args:
- link_text - Link text string to search for.
driver. find_element_by_partial_link_text(「text」)
find_elements_by_partial_link_text(self, link_text):
Finds a list of elements within this element's children by link text.
經過元素部分可見連接文本定位,含有該屬性的全部元素
:Args:
- link_text - Link text string to search for.
driver. find_elements_by_partial_link_text(「text」)
find_element_by_tag_name(self, name):
Finds element within this element's children by tag name.
經過查找html的標籤名稱定位元素
:Args:
- name - name of html tag (eg: h1, a, span)
用法 driver.find_element_by_tag_name(「name」)
find_elements_by_tag_name(self, name):
Finds a list of elements within this element's children by tag name.
經過查找html的標籤名稱定位全部元素
:Args:
- name - name of html tag (eg: h1, a, span)
用法driver.find_elements_by_tag_name(「name」)
find_element_by_xpath(self, xpath):
Finds element by xpath.
經過Xpath定位元素,詳細方法可參閱http://www.w3school.com.cn/xpath/
:Args:
xpath - xpath of element to locate. "//input[@class='myelement']"
Note: The base path will be relative to this element's location.
This will select the first link under this element.
::
myelement.find_elements_by_xpath(".//a")
However, this will select the first link on the page.
::
myelement.find_elements_by_xpath("//a")
用法 find_element_by_xpath(「//*」)
find_elements_by_xpath(self, xpath):
Finds elements within the element by xpath.
:Args:
- xpath - xpath locator string.
Note: The base path will be relative to this element's location.
This will select all links under this element.
::
myelement.find_elements_by_xpath(".//a")
However, this will select all links in the page itself.
::
myelement.find_elements_by_xpath("//a")
用法find_elements_by_xpath(「//*」)
find_element_by_class_name(self, name):
Finds element within this element's children by class name.
經過元素class name屬性定位元素
:Args:
- name - class name to search for.
用法 driver. find_element_by_class_name(「android.widget.LinearLayout」)
find_elements_by_class_name(self, name):
Finds a list of elements within this element's children by class name.
經過元素class name屬性定位全部含有該屬性的元素
:Args:
- name - class name to search for.
用法 driver. find_elements_by_class_name(「android.widget.LinearLayout」)
find_element_by_css_selector(self, css_selector):
Finds element within this element's children by CSS selector.
經過CSS選擇器定位元素
:Args:
- css_selector - CSS selctor string, ex: 'a.nav#home'
send_keys(self, *value):
Simulates typing into the element.
在元素中模擬輸入(開啓appium自帶的輸入法並配置了appium輸入法後,能夠輸入中英文)
:Args:
- value - A string for typing, or setting form fields. For setting
file inputs, this could be a local file path.
Use this to send simple key events or to fill out form fields::
form_textfield = driver.find_element_by_name('username')
form_textfield.send_keys("admin")
This can also be used to set file inputs.
::
file_input = driver.find_element_by_name('profilePic')
file_input.send_keys("path/to/profilepic.gif")
# Generally it's better to wrap the file path in one of the methods
# in os.path to return the actual path to support cross OS testing.
# file_input.send_keys(os.path.abspath("path/to/profilepic.gif"))
driver.element.send_keys(「中英」)
is_displayed(self):
Whether the element is visible to a user.
此元素用戶是否可見。簡單地說就是隱藏元素和被控件擋住沒法操做的元素(僅限 Selenium,appium是否實現了相似功能不是太肯定)這一項都會返回 False
用法 driver.element.is_displayed()
location_once_scrolled_into_view(self):
"""THIS PROPERTY MAY CHANGE WITHOUT WARNING. Use this to discover
where on the screen an element is so that we can click it. This method
should cause the element to be scrolled into view.
Returns the top lefthand corner location on the screen, or ``None`` if
the element is not visible.
暫不知道用法
"""
size(self):
The size of the element.
獲取元素的大小(高和寬)
new_size["height"] = size["height"]
new_size["width"] = size["width"]
用法 driver.element.size
value_of_css_property(self, property_name):
The value of a CSS property.
CSS屬性
用法 暫不知
location(self):
The location of the element in the renderable canvas.
獲取元素左上角的座標
用法 driver.element.location
'''返回element的x座標, int類型'''
driver.element.location.get('x')
'''返回element的y座標, int類型'''
driver.element.location.get('y')
rect(self):
A dictionary with the size and location of the element.
元素的大小和位置的字典
screenshot_as_base64(self):
Gets the screenshot of the current element as a base64 encoded string.
獲取當前元素的截圖爲Base64編碼的字符串
:Usage:
img_b64 = element.screenshot_as_base64
execute_script(self, script, *args):
Synchronously Executes JavaScript in the current window/frame.
在當前窗口/框架(特指 Html 的 iframe )同步執行 javascript 代碼。你能夠理解爲若是這段代碼是睡眠5秒,這五秒內主線程的 javascript 不會執行
:Args:
- script: The JavaScript to execute.
- \*args: Any applicable arguments for your JavaScript.
:Usage:
driver.execute_script('document.title')
execute_async_script(self, script, *args):
Asynchronously Executes JavaScript in the current window/frame.
插入 javascript 代碼,只是這個是異步的,也就是若是你的代碼是睡眠5秒,那麼你只是本身在睡,頁面的其餘 javascript 代碼仍是照常執行
:Args:
- script: The JavaScript to execute.
- \*args: Any applicable arguments for your JavaScript.
:Usage:
driver.execute_async_script('document.title')
current_url(self):
Gets the URL of the current page.
獲取當前頁面的網址。
:Usage:
driver.current_url
用法 driver.current_url
page_source(self):
Gets the source of the current page.
獲取當前頁面的源。
:Usage:
driver.page_source
close(self):
Closes the current window.
關閉當前窗口
:Usage:
driver.close()
quit(self):
Quits the driver and closes every associated window.
退出腳本運行並關閉每一個相關的窗口鏈接
:Usage:
driver.quit()