1. 程式人生 > >Appium(Python)API

Appium(Python)API

port 觸摸屏 性能 時間 class 顯示 some emulator RR

1、
創建新的會話
desired_caps = desired_caps = {
‘platformName‘: ‘Android‘,
‘platformVersion‘: ‘7.0‘,
‘deviceName‘: ‘Android Emulator‘,
‘automationName‘: ‘UiAutomator2‘,
‘app‘: PATH(‘/path/to/app‘)
}
self.driver = webdriver.Remote(‘http://127.0.0.1:4723/wd/hub‘, desired_caps)

2、
結束會話
self.driver.quit()

3、
獲取會話功能
desired_caps = self.driver.desired_capabilities()



4、
回退(僅限Web上下文)
self.driver.back()

5、
截圖
screenshotBase64 = self.driver.get_screenshot_as_base64()

6、
超時

設置超時
self.driver.set_page_load_timeout(5000)

設置隱式等待超時
self.driver.implicitly_wait(5000)

設置腳本超時
設置的時間量,
以毫秒為單位,
通過執行異步腳本,
執行異步允許運行它們都將立即中止之前(僅限於Web上下文)
self.driver.set_script_timeout(5000)

7、
方向

獲取定位
orientation = self.driver.orientation()



設置方向
driver.orientation = "LANDSCAPE"

8、
地理物質

獲取地理位置
location = self.driver.location()

設置地理位置
self.driver.set_location(49, 123, 10)

9、
日誌

獲取可用的日誌類型
log_types = driver.log_types()

獲取日誌
logs = driver.get_log(‘driver‘)

10、
設置

更新設備設置
# Python不支持

檢索設備設置
# Python不支持

11、
活動

開始活動
self.driver.start_activity("com.example", "ActivityName")



獲取當前活動
activity = self.driver.current_activity()

獲取當前包
package = self.driver.current_package()

12、
應用

安裝應用程序
self.driver.install_app(‘/Users/johndoe/path/to/app.apk‘)

應用程序已安裝
self.driver.is_app_installed(‘com.example.AppName‘)

啟動應用程序
self.driver.launch_app()

背景應用程序
將當前正在運行的應用程序發送到後臺
self.driver.background_app(10)

關閉應用程序
self.driver.close_app()

重置應用程序
self.driver.reset()

刪除應用程序
self.driver.remove_app(‘com.example.AppName‘)

獲取應用程序字符串
appStrings = self.driver.app_strings("en", "/path/to/file")

獲取測試覆蓋率數據
self.driver.end_test_coverage("Intent", "/path")


13、
文件

推送文件
self.driver.push_file(‘/path/to/device/foo.bar‘, ‘QXJlIHlvdXIgYmVlcnMgb2theT8=‘)

拉文件
file_base64 = self.driver.pull_file(‘/path/to/device/foo.bar‘)

拉文件夾
folder_base64 = self.driver.pull_folder(‘/path/to/device/foo.bar‘)

14、
動作

搖晃
self.driver.shake()

鎖定
self.driver.lock()

解鎖
self.driver.unlock()

設備是否被鎖定
# Python不支持

旋轉
# Python不支持

15、
按鍵

按鍵代碼
self.driver.press_keycode(10)

長按鍵代碼
self.driver.long_press_keycode(10)

隱藏鍵盤
self.driver.hide_keyboard()

顯示鍵盤
# Python不支持

16、
網絡

切換飛行模式
# Python不支持

切換數據服務
# Python不支持

切換WiFi
# Python不支持

切換位置服務的狀態
self.driver.toggle_location_services()

模擬短信(僅適用於仿真器)
# Python不支持

撥打GSM電話(僅限Emulator)
# Python不支持

設置GSM信號強度(僅限仿真器)
# Python不支持

設置GSM語音狀態(僅適用於仿真器)
# Python不支持

17、
性能數據

獲取性能數據
返回支持讀取的系統狀態信息,如CPU,內存,網絡流量和電池
# Python不支持

獲取性能數據類型
# Python不支持

18、
模擬器

執行Touch ID
模擬Touch ID事件(僅適用於iOS模擬器)
self.driver.touch_id(false)
# 模擬失敗的觸摸
self.driver.touch_id(true)
# 模擬通過的觸摸

切換正在註冊的模擬器以接受Touch ID(僅適用於iOS模擬器)
self.driver.toggle_touch_id_enrollment()

19、
系統

打開Android通知(僅適用於仿真器)
self.driver.open_notifications()

獲取系統欄
檢索狀態和導航欄的可見性和邊界信息
# Python不支持

獲取系統時間
time = self.driver.device_time()

20、
查找元素
el = self.driver.find_element_by_accessibility_id(‘SomeAccessibilityID‘)

21、
操作

點擊
el = self.driver.find_element_by_accessibility_id(‘SomeId‘)
el.click()

輸入
self.driver.find_element_by_accessibility_id(‘SomeAccessibilityID‘).send_keys(‘Hello world!‘)

清除元素的值
self.driver.find_element_by_accessibility_id(‘SomeAccessibilityID‘).clear()

22、
屬性

獲取元素文本
el = self.driver.find_element_by_accessibility_id(‘SomeAccessibilityID‘)
text = el.text

獲取標簽名稱
tagName = self.driver.find_element_by_accessibility_id(‘SomeAccessibilityID‘).tag_name

獲取元素屬性
tagName = self.driver.find_element_by_accessibility_id(‘SomeAccessibilityID‘).get_attribute(‘content-desc‘)

元素被選中
確定是否選擇了表單或表單類元素(復選框,選擇等)
self.driver.find_element_by_accessibility_id(‘SomeAccessibilityID‘).is_selected()

元素已啟用
確定元素當前是否啟用
self.driver.find_element_by_accessibility_id(‘SomeAccessibilityID‘).is_enabled()

獲取元素位置
確定元素在頁面或屏幕上的位置
location = self.driver.find_element_by_accessibility_id(‘SomeAccessibilityID‘).location

獲取元素大小
以像素為單位確定元素的大小
size = self.driver.find_element_by_accessibility_id(‘SomeAccessibilityID‘).size

獲取元素矩形
獲取元素的尺寸和坐標
# Python不支持

獲取元素CSS值
查詢Web元素的計算CSS屬性的值
cssProperty = self.driver.find_element_by_accessibility_id(‘SomeId‘).value_of_css_property("style")

在視圖中獲取元素位置
一旦將元素滾動到視圖中,確定元素在屏幕上的位置(主要是內部命令並且不被所有客戶端支持)
# Python不支持

23、
提交表單
提交一個FORM元素
el = self.driver.find_element_by_accessibility_id(‘SomeAccessibilityID‘)
el.submit();

24、
獲取活動元素
element = driver.switch_to.active_element

25、
元素是否相等
# Python不支持

26、
上下文

獲取當前上下文
context = driver.current_context
或者
context = driver.context

獲取所有上下文
contexts = driver.contexts

設置當前上下文
webview = driver.contexts[1]
driver.switch_to.context(webview)
# ...
driver.switch_to.context(‘NATIVE_APP‘)

27、
鼠標

將鼠標移至
actions = ActionChains(driver)
actions.move_to(element, 10, 10)
actions.perform()

在當前鼠標坐標點擊任意鼠標按鈕
actions = ActionChains(driver)
actions.move_to_element(element)
actions.click()
actions.perform()

雙擊當前鼠標坐標(由moveto設置)
actions = ActionChains(driver)
actions.move_to_element(element)
actions.double_click()
actions.perform()

按鈕關閉
在當前的鼠標坐標上單擊並按住鼠標左鍵
actions = ActionChains(driver)
actions.move_to_element(element)
actions.click_and_hold()
actions.perform()

扣緊
釋放先前保持的鼠標按鈕
actions = ActionChains(driver)
actions.move_to_element(element)
actions.click_and_hold()
actions.move_to_element(element, 10, 10)
action.release()
actions.perform()

28、
觸摸

單擊輕觸設備
actions = TouchActions(driver)
actions.tap(element)
actions.perform()

雙擊
使用手指動作事件雙擊觸摸屏
actions = TouchActions(driver)
actions.double_tap(element)
actions.perform()

移動
手指在屏幕上移動
actions = TouchActions(driver)
actions.tap_and_hold(element)
actions.move(50, 50)
actions.perform()

觸摸下來
手指在屏幕上
actions = TouchActions(driver)
actions.tap_and_hold(element)
actions.move(50, 50)
actions.perform()

潤色(做小的修改)
手指在屏幕上
actions = TouchActions(driver)
actions.tap_and_hold(20, 20)
actions.release(50, 50)
actions.perform()

長按
使用手指運動事件長按觸摸屏
actions = TouchActions(driver)
actions.long_press(element)
actions.perform()

滾動
使用基於手指的動作事件在觸摸屏上滾動
actions = TouchActions(driver)
actions.scroll_from_element(element, 10, 100)
actions.scroll(10, 100)
actions.perform()

拂去
使用手指運動事件輕擊觸摸屏
actions = TouchActions(driver)
actions.flick_element(element, 1, 10, 10)
actions.perform()

多點觸摸執行
執行多點觸摸動作序列
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.common.multi_action import MultiAction

a1 = TouchAction()
a1.press(10, 20)
a1.move_to(10, 200)
a1.release()

a2 = TouchAction()
a2.press(10, 10)
a2.move_to(10, 100)
a2.release()

ma = MultiAction(self.driver)
ma.add(a1, a2)
ma.perform()

觸摸執行
執行一個觸摸動作序列
from appium.webdriver.common.touch_action import TouchAction

actions = TouchAction(driver)
actions.tap_and_hold(20, 20)
actions.move_to(10, 100)
actions.release()
actions.perform()

29、
窗口

切換到窗口
將焦點更改為另一個窗口(僅限Web上下文)
self.driver.switch_to.window("handle")

關閉窗口
關閉當前窗口(僅限Web上下文)
self.driver.close()

獲取窗口句柄
檢索當前窗口句柄(僅限Web上下文)
window_handle = self.driver.current_window_handle()

獲取所有窗口句柄
檢索可用於會話的所有窗口句柄的列表(僅限Web上下文)
window_handles = self.driver.window_handles()

獲取標題
獲取當前頁面標題(僅限Web上下文)
title = self.driver.title()

獲取窗口大小
獲取指定窗口的大小(僅限Web上下文)
handle_one_size = self.driver.get_window_size()
handle_two_size = self.driver.get_window_size("handleName")

設置窗口大小
更改指定窗口的大小(僅限Web上下文)
self.driver.set_window_size(10, 10)

獲取窗口位置
獲取指定窗口的位置(僅限Web上下文)
handle_one_position = self.driver.get_window_position()
handle_two_position = self.driver.get_window_position("handleName")

設置窗口位置
更改指定窗口的位置(僅限Web上下文)
self.driver.set_window_position(10, 10)

最大化窗口
最大化指定的窗口(僅限Web上下文)
self.driver.maximize_window()

30、
導航

導航到URL
導航到新的URL(僅限Web上下文)
self.driver.get("http://appium.io/")

獲取URL
檢索當前頁面的URL(僅限Web上下文)
url = self.driver.current_url()

回去
如果可能,在瀏覽器歷史記錄中向後瀏覽(僅限Web上下文)
self.driver.back()

前進
如果可能,在瀏覽器歷史記錄中向前瀏覽(僅限Web上下文)
self.driver.forward()

刷新
刷新當前頁面(僅限Web上下文)
self.driver.refresh()

31、
Cookie

獲取所有Cookie
檢索當前頁面可見的所有cookie(僅限Web上下文)
cookies = self.driver.get_cookies()

設置Cookie
設置一個cookie(僅限Web上下文)
self.driver.add_cookie({name: ‘foo‘, value: ‘bar‘})

刪除Cookie
刪除具有給定名稱的cookie(僅限Web上下文)
self.driver.delete_cookie("cookie_name")

刪除所有Cookie
刪除當前頁面可見的所有cookie(僅限Web上下文)
self.driver.delete_all_cookies()

32、
Frame

切換到frame
將焦點更改為頁面上的其他frame(僅限Web上下文)
self.driver.switch_to.frame(3)

切換到父frame
將焦點更改為父上下文(僅限Web上下文)
self.driver.switch_to.parent()

33、
執行異步JavaScript
將JavaScript片段註入頁面以在當前選定框架的上下文中執行(僅限Web上下文)
self.driver.execute_async_script(‘document.title’)

34、
執行JavaScript
將JavaScript片段註入頁面以在當前選定框架的上下文中執行(僅限Web上下文)
self.driver.execute_script(‘document.title’)

Appium(Python)API