1.設備屏幕事件
熄滅屏幕d.screen_off()
喚醒屏幕d.screen_on()
屏蔽狀態d.info.get('screenOn')#返回True or False
解鎖屏幕d.unlock()#安卓7.0試了可以,安卓9.0就不行了,實際行為是 1.啟動activity: com.github.uiautomator,2.按下home鍵
2.設備按鍵事件(所有的keyevent請點我)或官方地址
用法:d.press(keycode)
當前支持以下事件鍵值:,
home(主屏),back(返回上一級)
left,right,up,down,center(打開一個文本編輯框,可看到移動光標)
search(搜索框),enter(回車),delete ( or del 刪除選中內容)
recent (recent apps),menu(菜單鍵) 這倆效果貌似一樣的
volume_up(音量+),volume_down(音量-),volume_mute(靜音)
camera(相機--沒打開),power(電源鍵)
3.設備手勢交互
#x,y表示坐標,或填入小數(0.5,0.5)百分比表示屏幕的中心點,適用於:click,swipe,drag,long_click
單擊d.click(x,y)
雙擊d.double_click(x,y,[duration])#兩次點擊之間的間隔默認0.1秒
長按d.long_click(x,y,[delay])#delay默認0.5秒
滑動d.swipe(sx,sy,ex,ey,[delay])#delay默認0.5秒
拖拽d.drag(sx,sy,ex,ey,[delay])#delay默認0.5秒
多點滑動:hw.swipe_points([(209,943),(541,973),(551,1265),(857,1265)],0.2)
按下並拖動(Beta):
d.touch.down(x,y)
time.sleep(0.1)
d.touch.move(x,y)
d.touch.up()
4.屏幕方向設定
#打開撥號界面或短信界面:在android7.0上試了n,l,r均有效,android9.0上相冊有受影響
n(natural),l(or left),r(right),u(upsidedown 這個無效)
direct = d.orientation#默認方向,輸出natural
d.set_orientation('l')
#鎖定/解鎖屏幕旋轉
d.freeze_rotation()
d.freeze_rotation(false)
5.屏幕截圖
#要獲得PIL格式的圖像,需要安裝pillow庫,支持png、jpg格式:d.screenshot("c:/name.jpg")
#要獲得opencv格式的圖像,需要安裝numpy和cv2庫,用法如下:
img=xm.screenshot(format='opencv')
cv2.imwrite('c:/test.jpg',img)
#獲得原始的jpeg數據
imgbin=d.screenshot(format='raw')
open("test.jpg","wb").write(imgbin)
6.轉儲UI hierarchy
xml=d.dump_hierarchy()
7.打開通知中心/快捷設置
d.open_notification()
d.open_quick_settings()