adb做為android的調試橋,在做app自動化中有着巨大的用處,可以幫助我們解決問題,今天主要認識adb shell input
adb shell input
我們首先通過cmd輸入adb shell input有哪些內容
$ adb shell input Usage: input [<source>] <command> [<arg>...] The sources are: mouse keyboard joystick touchnavigation touchpad trackball stylus dpad touchscreen gamepad The commands and default sources are: text <string> (Default: touchscreen) keyevent [--longpress] <key code number or name> ... (Default: keyboard) tap <x> <y> (Default: touchscreen) swipe <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen) press (Default: trackball) roll <dx> <dy> (Default: trackball)
上面這么多到底講的啥?
其實說白了就是支持一下內容
1、text:支持輸入文本內容(暫不支持中文)
2、keyevent:模擬按鍵
3、tap:點擊
4、swipe:滑動
5、press:軌跡球按下
6、roll:軌跡球滾動
text
直接打開終端輸入
# 輸入內容(暫不支持中文) adb shell input text 1111
keyevent
直接打開輸入對應的值
# 模擬手機按鍵home adb shell input keyevent 3
tap
選取手機上的坐標,然后直接輸入
# tap點擊 adb shell input tap 454 204
坐標怎么來的:通過uiautomatorviewer.bat定位工具查看坐標[393,140] [516,268],然后求出中間值[454 204]
swipe
和tap一樣,選取兩個坐標進行滑動,坐標安靜這邊選取的是(x*1/2 Y*3/4 x*1/2 Y*1/4)
代碼中的input
上面這么多都是在cmd中敲出來的,真正的自動化確實要在代碼中,我們可以進行對這些常用的adb命令進行封裝起來
import os class input(object): # 輸入文字 def text(self,text): adb = 'adb shell input text %s'%text os.popen(adb) # 滑動 def swipe(self,x,y,x1,y1): adb = 'adb shell input swipe %s %s %s %s '%(x,y,x1,y1) os.popen(adb) # 模擬按鍵 def keyevent(self,k): adb = 'adb shell input keyevent %s'%k os.popen(adb) if __name__ == '__main__': adb = input() adb.text(1111) adb.swipe(280,720,280,240) adb.keyevent(3)
PS:其實寫了這么多會發現方法有很多種,具體用那種,就要看大家用哪種比較方便就用哪種。