使用的是UIautomator2和weditor,配合上酷Q機器人進行qq回復
初始化
手機連接電腦,打開開發者模式,打開usb調試,文件傳輸模式,然后命令行輸入以下
adb devives
確認設備id號
手機安裝控制組件,確認授權調試:
python -m uiautomator2 init
打開輔助編輯器
python -m weditor
常用命令:
import uiautomator2 as u2 d = u2.connect("--serial-here--") # 只有一個設備也可以省略參數 d = u2.connect() # 一個設備時 d = u2.connect("10.1.2.3") # 通過設備的IP連接(需要在同一局域網且設備上的atx-agent已經安裝並啟動) d.app_current() # 獲取前台應用 packageName, activity d.app_start("com.example.app") # 啟動應用 d.app_start("com.example.app", stop=True) # 啟動應用前停止應用 d.app_stop("com.example.app") # 停止應用 app = d.session("com.example.app") # 啟動應用並獲取session # session的用途是操作的同時監控應用是否閃退,當閃退時操作,會拋出SessionBrokenError app.click(10, 20) # 坐標點擊 # 無session狀態下操作 d.click(10, 20) # 坐標點擊 d.swipe(10, 20, 80, 90) # 從(10, 20)滑動到(80, 90) d.swipe_ext("right") # 整個屏幕右滑動 d.swipe_ext("right", scale=0.9) # 屏幕右滑,滑動距離為屏幕寬度的90% d.press("back") # 模擬點擊返回鍵 d.press("home") # 模擬Home鍵 d.send_keys("hello world") # 模擬輸入,需要光標已經在輸入框中才可以 d.clear_text() # 清空輸入框 # 執行shell命令 output, exit_code = d.shell("ps -A", timeout=60) # 執行shell命令,獲取輸出和exitCode output = d.shell("pwd").output # 這樣也可以 exit_code = d.shell("pwd").exit_code # 這樣也可以 # 元素操作 d.xpath("立即開戶").wait() # 等待元素,最長等10s(默認) d.xpath("立即開戶").wait(timeout=10) # 修改默認等待時間 # 常用配置 d.settings['wait_timeout'] = 20 # 控件查找默認等待時間(默認20s) # xpath操作 d.xpath("立即開戶").click() # 包含查找等待+點擊操作,匹配text或者description等於立即開戶的按鈕 d.xpath("//*[@text='私人FM']/../android.widget.ImageView").click() d.xpath('//*[@text="私人FM"]').get().info # 獲取控件信息 for el in d.xpath('//android.widget.EditText').all(): print("rect:", el.rect) # output tuple: (left_x, top_y, width, height) print("bounds:", el.bounds) # output tuple: (left, top, right, bottom) print("center:", el.center()) el.click() # click operation print(el.elem) # 輸出lxml解析出來的Node # 監控彈窗(在線程中監控) d.watcher.when("跳過").click() d.watcher.start()
watcher的用法:
https://blog.csdn.net/VeastLee/article/details/87743018
uiautomator2 使用Python測試 Android應用
http://www.cnblogs.com/fnng/p/8486863.html
UIautomator2的api
https://blog.csdn.net/Eugene_3972/article/details/76629066
https://blog.csdn.net/ricky_yangrui/article/details/81415365
https://github.com/openatx/uiautomator2/blob/master/QUICK_REFERENCE.md
adb無法連接,計算機拒絕問題解決
https://blog.csdn.net/u013250071/article/details/80527993