1.導包
from airtest.core.api import *
from airtest.core.api import using
using("timeDecorator.air")
from timeDecorator import count_time #導入count_time方法
2. connect_device("Android:///")
(1) 連接本地真機設備
connect_device("Android:///SJE5T17B17?cap_method=javacap&touch_method=adb")
(2) 連接模擬器
connect_device("Android://127.0.0.1:5037/10.254.60.1:5555")
常見模擬器端口號:
網易mumu模擬器 7555
夜神模擬器 62001
3.auto_setup(basedir=None,devices=None,logdir=None,project_root=None,compress=0)
自動配置運行環境,如果當前沒有連接設備,默認嘗試連接Android設備
參數
·basedir -設置當前腳本所在路徑,也可以直接傳__file__變量進來
·devices - 一個內容為connect_device uri 字符串的列表
·logdir - 可設置腳本運行時log保存路徑,默認值為None則不保存log,如果設置為True則自動保存在<basedir>/log目錄中
·project_root - 用於設置PROJECT_ROOT變量,方便using接口的調用
4. shell(*args, **kwargs)# 在目標設備上運行遠程命令shell命令
dev = connect_device("Android:///device1")
dev.shell("ls")
dev.shell('input text "test"') #輸入test
5. start_app() #在設備上啟動目標應用,傳的是包名
start_app("com.tencent.qq")
6. stop_app() #在設備上停止目標應用,傳的是包名
stop_app("com.tencent.qq")
7. clear_app() # 清理設備上的目標應用數據,傳的是包名
clear_app("com.tencent.qq")
8. install() 安裝應用到設備
install(r"D:\demo\test.apk")
9. uninstall ()#傳入的是包名
uninstall("com.tencent.qq")
10. snapshot() #對目標設備進行一次截圖,並且保存到文件中
11. wake() #喚醒並解鎖目標設備
12. home() #返回home頁面
13. touch() # 在當前設備畫面上 進行一次點擊
(1) 點擊絕對坐標touch((100, 100))
(2) 點擊圖片的中心位置
touch(Template(r"tpl1606730579419.png", target_pos=5)
(3) 點擊兩次 touch((100,100),times=2)
14. click() #點擊動作,比較常用
15. double_click() #雙擊
16. swipe() #滑動頁面
(1) swipe((100, 100), (200, 200))
17. keyevent() #鍵盤事件 稍后給出鍵盤事件
(1) keyevent("KEYCODE_DEL")
(2) keyevent("HOME")
18. text() #輸入文本,文本框需要處於激活狀態
(1) text("test")
(2) text("test", enter=False)
(3) text("test", search=True)
19. sleep()#設置一個等待sleep時間
20. wait() #設置一個等待時間直到出現目標圖片
21. exists() #判斷是否存在,如果存在返回坐標,否則返回false
if exists(截圖):
22. find_all() #在設備屏幕上查找所有出現的目標並返回其坐標列表
23. 斷言
(1) assert_exists()
(2) assert_not_exists()
(3) assert_equal
(4) assert_not_equal