Appium+python API示例(基礎版)

 以下代碼,全部放開執行會出報錯,可以放開部分代碼在IDE中執行,

另附我在網上看到的優質API資源文檔地址:

https://blog.csdn.net/zhongbeida_xue/article/details/51210577

 

import time

from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.ui import WebDriverWait

desired_caps = dict()
# TODO:指定要測試的平臺,不區分大小寫
desired_caps['platformName'] = 'Android'
# TODO:指定平臺版本
desired_caps['platformVersion'] = '8.0.0'
desired_caps['deviceName'] = '192.168.120.103:5555'
# TODO:以下兩行指定打開的包名和activity名
desired_caps['appPackage'] = 'com.android.settings'
# desired_caps['appActivity'] = '.Settings'
desired_caps['appActivity'] = '.ChooseLockPattern'
# todo: 加入以下兩行代碼可以保證輸入框輸入中文
desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
# time.sleep(2)
# todo: 獲取當前運行的應用包名和activity名
print(driver.current_package + '/' + driver.current_activity)
# # TODO: 通過id 獲取元素
# list_id = driver.find_elements_by_id('android:id/title')
# todo:通過xpath獲取一組元素
# for title in list_id:
#     print(title.tag_name+'\t'+title.text+'\t'+title.id+'\t')
# driver.find_element_by_xpath('//*[@text="顯示"]').click()
# time.sleep(1)

# todo: 啓動應用
# driver.start_activity('com.android.messaging', '.ui.conversationlist.ConversationListActivity')
# print(driver.current_package+'/'+driver.current_activity)
# TODO: 根據包名判斷是否應用已經安裝
# if driver.is_app_installed('cn.goapk.market'):
#     todo: 卸載應用
#     driver.remove_app('cn.goapk.market')
# todo: 安裝應用
# driver.install_app('D:/SOFTWARE/Genymotion/Apk/anzhishichang.apk')
# time.sleep(1)
# todo: 切換到後臺運行,參數是指在後臺停留的時間,時間結束會自動返回前臺
# driver.background_app(2)
# time.sleep(1)

# TODO:通過xpath獲取帶有‘設’字的元素
# list_she = driver.find_elements_by_xpath('//*[contains(@text,"設")]')
# print(len(list_she))
# for she_ in list_she:
#     print(she_.text)

# todo: 元素等待---加載指定的時長,等待元素出現,若超時仍未出現元素,報錯,在指定時長內找到元素,則繼續往下執行
#   隱式等待:
#       示例:driver.implicitly_wait(20)
#   顯式等待:在total_time的時間內,每flu_time秒檢查一次(默認0.5秒檢查一次),若找到元素,就繼續往下執行,否則報超時錯誤
#       步驟:1。導包 2.創建WebDriverWait對象 3.調用util方法
#       示例:wait_driver = WebDriverWait(driver,total_time,flu_time)
#             display_text = wait_driver.util(lambda x: x.find_element_by_xpath('//*[@text="顯示"]'))
#             display_text.click()

# 點擊搜索按鈕
# wait_search_btn = WebDriverWait(driver, 5, 1).until(lambda x: x.find_element_by_xpath('//*[@content-desc="在設置中搜索"]'))
# wait_search_btn.click()
# search_button = driver.find_element_by_xpath('//*[@content-desc="在設置中搜索"]')
# # search_button.click()
# search_button_input = driver.find_element_by_id('android:id/search_src_text')
# # TODO:文本框輸入
# search_button_input.send_keys('hello')
# time.sleep(2)
# # TODO:清空文本框文本
# search_button_input.clear()
# time.sleep(2)
# # TODO:文本框輸入中文需要再上邊添加兩行代碼
# search_button_input.send_keys('你好')
# todo: 獲取控件的寬和高
# print(wait_search_btn.size)
# print(wait_search_btn.size['width'])
# print(wait_search_btn.size['height'])
# todo: 獲取控件的座標位置
# print(wait_search_btn.location)
# print(wait_search_btn.location['x'])
# print(wait_search_btn.location['y'])


# todo: 根據屬性名獲取元素值
# element_values = driver.find_elements_by_id('android:id/title')
# for i in element_values:
#     print(i.get_attribute('name'), end='\t')
#     print(i.get_attribute('resourceId'), end='\t')
#     print(i.get_attribute('className'), end='\t')
#     print(i.get_attribute('text'))

# todo : 屏幕滑動
#  -swipe
#  -scroll
#  -drag_and_drop
# todo:1)swipe  ----- 參數【start_x,start_y,end_x,end_y,滑動步長(單位:ms)】
# driver.swipe(720, 1500, 720, 200, 1500)
# todo:2)scroll ----- 參數【滑動起始控件,滑動結束控件,步長】
# display_btn = driver.find_element_by_xpath('//*[@text="顯示"]')
# settings_btn = driver.find_element_by_xpath('//*[@text = "設置"]')
# driver.scroll(display_btn, settings_btn, 1000)
# driver.drag_and_drop(display_btn,settings_btn)
# todo:3)drag_and_drop ----- 拖拽 參數【起始位置控件,結束位置控件,步長】
# driver.close_app()
# web_btn = driver.find_element_by_xpath('//*[@text="WebView Browser Tester"]')
# email_btn = driver.find_element_by_xpath('//*[@text="電子郵件"]')
# driver.drag_and_drop(web_btn, email_btn)

# todo: tap -- 模擬多個手指點擊屏幕,最多五個手指
# driver.tap([(100, 620), (100, 360), (100, 500)], 500)
# todo:
#  TouchAction高級手勢,可以將小的動作組合成一系列複雜的動作
#  --tap
#  --press
#  --release
#  --long_press
# todo: 1)tap :輕敲---參數可以直接傳控件元素【要操作的控件,與。click效果一致】也可以傳座標 如:【x=100,y=100】(x=和y=不能省略)
# TouchAction(driver).tap(x=850, y=1550).perform()
# TouchAction(driver).tap(display_btn).perform()
# todo:
#  2)press 按下--參數【x=100,y=100】
#  3)release 擡起
# TouchAction(driver).press(x=200,y=540).release().perform()
# todo:
#  4)long_press 長按   參數【x,y,步長】
# TouchAction(driver).press(650, 650).wait(2000).release().perform()  # 與long_press方法效果一直
# TouchAction(driver).long_press(650, 650, 2000)
# todo:5)move_to 移動 參數【x,y】
# TouchAction(driver).press(x=225, y=960).move_to(x=540, y=960).perform()
# time.sleep(10)

# todo:獲取手機分辨率
print(driver.get_window_size())
# todo:截圖 參數【存放的路徑】
driver.get_screenshot_as_file('/Screenshots/foo.png')
# todo: 獲取手機網絡狀態
print(driver.network_connection)
# todo: 設置手機網絡 參數【ConnectionType】如下
'''
 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`

'''
driver.set_network_connection(2)
time.sleep(3)
'''
    需求:三次音量+ 返回 兩次音量—
'''
# todo: 發送鍵到設備-----更多的keyCode值百度搜索android keyCode就能查到
driver.press_keycode(24)  # 音量+
driver.press_keycode(24)
driver.press_keycode(24)

driver.press_keycode(4)  # 返回
driver.press_keycode(3)  # HOME鍵
driver.press_keycode(25)  # 音量-

# todo: 打開通知欄
driver.open_notifications()
# todo: 關閉運行的app
driver.close_app()
# TODO: 退出驅動,同時關閉驅動關聯的app
driver.quit()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章