實現原理
Hierarchy Viewer:獲得當前手機實時的UI信息,方便用於手機的自動化測試;
python中的subprocess.Popen():調用系統命令;
uiautomator工具:獲取界面控件信息;
adb命令:對手機進行操作;
測試環境
1、HierarchyViewer將把下一台Android設備的4939端口映射到PC的4939端口
adb devices 查看手機設備號
adb -s 手機設備號 forward tcp:4939 tcp:4939
2、判斷手機是否開啟了view server,如果沒開啟則開啟
adb -s 手機設備號 shell service call window 3
通過返回值查看
"Result: Parcel(00000000 00000000 '........') --沒開
"Result: Parcel(00000000 00000001 '........') --開啟
開啟view server
adb -s 手機設備號 shell service call window 1 i32 4939
關閉 view server
adb -s 手機設備號 shell service call window 2 i32 4939
說明:只有模擬器或者root手機才可以開啟view service,實際操作過程中發現root手機開啟view service 並非命令可完成,故先行使用模擬器進行測試
3、關鍵adb命令
adb shell uiautomator dump /mnt/sdcard/window_dump.xml 獲得手機當前界面的UI信息,生成window_dump.xml
adb shell input keyevent 21 模擬鍵盤操作
adb shell input text “123” 輸入text
......