首頁/ 汽車/ 正文

python UIAutomator2使用超詳細教程

一、簡介

uiautomator2是一個python庫,用於Android的UI自動化測試,其底層基於Google uiautomator,Google提供的uiautomator庫可以獲取螢幕上任意一個APP的任意一個控制元件屬性,並對其進行任意操作。

GitHub地址:https://github。com/openatx/uiautomator2

二、支援平臺及語言

python-uiautomator2封裝了谷歌自帶的uiautomator2測試框架,提供便利的python介面。他允許測試人員直接在PC上編寫Python的測試程式碼,操作手機應用,完成自動化,大大提高了自動化程式碼編寫的效率。

上面簡介大家自行查詢,簡而言之,就是在PC上得程式可以連線安卓手機進行操控。

話不多說直接上乾貨。

安裝adb:

從谷歌官網下載

Android Platform Tools

, 解壓。

並加包含adb的目錄加入到系統的PATH中。

設定系統環境變數。

python UIAutomator2使用超詳細教程

python UIAutomator2使用超詳細教程

python UIAutomator2使用超詳細教程

點選確定儲存,配置完成。

安裝UIAutomator2 模組

pip install ——pre uiautomator2pip install pillow (如果需要截圖,可安裝pillow)

安裝weditor

有了這個,方便我們快速識別手機上的元素,方便寫程式碼

pip install -U weditor #pip install ——pre weditor#Windows系統可以使用命令在桌面建立一個快捷方式:python -m weditor –shortcut

python UIAutomator2使用超詳細教程

在這裡PC環境搭建完畢。

接下來設定手機配置。

開啟手機的開發者功能,並開啟除錯功能。

python UIAutomator2使用超詳細教程

這裡是華為P20鴻蒙系統。

有部分使用者手機是安卓11以上的系統,有無線除錯功能。當然這個就很棒了。可以開啟無線除錯功能。

手機設定好之後。插上usb與電腦連結。

第一步查詢自己的手機連結驅動名稱

#第一次執行 adb devices 命令adb devices * daemon not running; starting now at tcp:5037 * daemon started successfully List of devices attached#第二次執行 adb devices 命令adb devices List of devices attached (沒有找到連線手機)#插上usb線之後,繼續執行adb devicesadb devices List of devices attached D5F0218314002832 device#D5F0218314002832 就是手機連線電腦的驅動名稱 #後面device是連線狀態。這個是連線正常。#網上很多方法說,在連線手機之後先要初始換手機環境之類的,我覺得這部分沒必要在命令中寫。#初始化這部分可以在python中進行初始化。python -m uiautomator2 init

直接上python程式碼:

import adbutilsimport loggingimport uiautomator2 as u2#這裡會給手機安裝部分軟體ATX。總共安裝五個程式。#minicap#inicap。so#minitouch#com。github。uiautomator#com。github。uiautomator。test#開始安裝def init(): device = adbutils。adb。device(“D5F0218314002832”) init = u2。init。Initer(device, loglevel=logging。INFO) init。install()#這裡是解除安裝在手機上安裝的app程式def uninstall(): device = adbutils。adb。device(“D5F0218314002832”) init = u2。init。Initer(device, loglevel=logging。INFO) init。uninstall()#可以自己執行試一試if __name__ == “__main__”: init() uninstall()

這樣就連線好了。下面介紹常用的方法

#透過USB, 假設裝置序列是import uiautomator2 as u2d = u2。connect(‘D5F0218314002832’)d。app_install(‘http://some-domain。com/some。apk’) #引號內為下載apk地址d。app_start(‘com。eg。android。AlipayGphone’) #引號內為包名稱,這裡為支付寶d。app_stop(‘com。eg。android。AlipayGphone’)d。app_clear(‘com。eg。android。AlipayGphone’)# 停止所有d。app_stop_all()# 停止所有應用程式,除了com。examples。demod。app_stop_all(excludes=[‘com。examples。demo’])d。disable_popups() # 自動跳過彈出視窗d。disable_popups(False) # 禁用自動跳過彈出窗#獲取裝置資訊:d。info # 獲取視窗大小print(d。window_size())# 裝置垂直輸出示例: (1080, 1920)# 裝置水平輸出示例: (1920, 1080) # 獲取當前應用程式資訊。對於某些android裝置,輸出可以為空print(d。current_app()) #獲取裝置序列號print(d。serial) #獲取WIFI IPprint(d。wlan_ip) #獲取詳細的裝置資訊print(d。device_info)#關鍵事件:d。screen_on()#開啟螢幕d。screen_off() #關閉螢幕#獲取當前螢幕狀態d。info。get(‘screenOn’) # 需要 Android> = 4。4#硬鍵盤和軟鍵盤操作d。press(“home”) # 點選home鍵d。press(“back”) # 點選back鍵d。press(“left”) # 點選左鍵d。press(“right”) # 點選右鍵d。press(“up”) # 點選上鍵d。press(“down”) # 點選下鍵d。press(“center”) # 點選選中d。press(“menu”) # 點選menu按鍵d。press(“search”) # 點選搜尋按鍵d。press(“enter”) # 點選enter鍵d。press(“delete”) # 點選刪除按鍵d。press(“recent”) # 點選近期活動按鍵d。press(“volume_up”) # 音量+d。press(“volume_down”) # 音量-d。press(“volume_mute”) # 靜音d。press(“camera”) # 相機d。press(“power”) #電源鍵#解鎖螢幕d。unlock()#手勢與裝置的互動:# 單擊螢幕d。click(x,y) # x,y為點選座標 # 雙擊螢幕d。double_click(x,y)d。double_click(x,y,0。1) # 預設兩個單擊之間間隔時間為0。1秒 # 長按d。long_click(x,y)d。long_click(x,y,0。5) # 長按0。5秒(預設) # 滑動d。swipe(sx, sy, ex, ey)d。swipe(sx, sy, ex, ey, 0。5) #滑動0。5s(default) #拖動d。drag(sx, sy, ex, ey)d。drag(sx, sy, ex, ey, 0。5)#拖動0。5s(default)# 滑動點 多用於九宮格解鎖,提前獲取到每個點的相對座標(這裡支援百分比) # 從點(x0, y0)滑到點(x1, y1)再滑到點(x2, y2)# 兩點之間的滑動速度是0。2秒d。swipe((x0, y0), (x1, y1), (x2, y2), 0。2) # 注意:單擊,滑動,拖動操作支援百分比位置值。例:d。long_click(0。5, 0。5) 表示長按螢幕中心#重點重點重點。。。開發使用中經常用到# 檢索方向# 檢索方向# 檢索方向d。orientation# 檢索方向。輸出可以是 “natural” or “left” or “right” or “upsidedown” # 設定方向d。set_orientation(“l”) # or “left”d。set_orientation(“r”) # or “right”d。set_orientation(“n”) # or “natural” #凍結/ 開啟旋轉d。freeze_rotation() # 凍結旋轉d。freeze_rotation(False) # 開啟旋轉 ########## 截圖 ############# 截圖並儲存到電腦上的一個檔案中,需要Android>=4。2。d。screenshot(“home。jpg”) # 得到PIL。Image格式的影象。 但你必須先安裝pillowimage = d。screenshot() # default format=“pillow”image。save(“home。jpg”) # 或‘home。png’,目前只支援png 和 jpg格式的影象 # 得到OpenCV的格式影象。當然,你需要numpy和cv2安裝第一個import cv2image = d。screenshot(format=‘opencv’)cv2。imwrite(‘home。jpg’, image) # 獲取原始JPEG資料imagebin = d。screenshot(format=‘raw’)open(“some。jpg”, “wb”)。write(imagebin) ############################## 轉儲UI層次結構# get the UI hierarchy dump content (unicoded)。(獲取UI層次結構轉儲內容)d。dump_hierarchy() # 開啟通知或快速設定d。open_notification() #下拉開啟通知欄d。open_quick_settings() #下拉開啟快速設定欄 # 檢查特定的UI物件是否存在d(text=“Settings”)。exists # 返回布林值,如果存在則為True,否則為Falsed。exists(text=“Settings”) # 另一種寫法 # 高階用法d(text=“Settings”)。exists(timeout=3) # 等待‘Settings’在3秒鐘出現 # 獲取特定UI物件的資訊d(text=“Settings”)。info # 獲取/設定/清除可編輯欄位的文字(例如EditText小部件)d(text=“Settings”)。get_text() #得到文字小部件d(text=“Settings”)。set_text(“My text。。。”) #設定文字d(text=“Settings”)。clear_text() #清除文字 # 獲取Widget中心點d(text=“Settings”)。center()#d(text=“Settings”)。center(offset=(0, 0)) # 基準位置左前#UI物件有五種定位方式:# text、resourceId、description、className、xpath、座標# 執行單擊UI物件 #text定位單擊d(text=“Settings”)。click()d(text=“Settings”, className=“android。widget。TextView”)。click() #resourceId定位單擊d(resourceId=“com。ruguoapp。jike:id/tv_title”, className=“android。widget。TextView”)。click() #description定位單擊d(description=“設定”)。click()d(description=“設定”, className=“android。widget。TextView”)。click() #className定位單擊d(className=“android。widget。TextView”)。click() #xpath定位單擊d。xpath(“//android。widget。FrameLayout[@index=‘0’]/android。widget。LinearLayout[@index=‘0’]”)。click() #座標單擊d。click(182, 1264) # 等待元素出現(最多10秒),出現後單擊d(text=“Settings”)。click(timeout=10) # 在10秒時點選,預設的超時0d(text=‘Skip’)。click_exists(timeout=10。0) # 單擊直到元素消失,返回布林d(text=“Skip”)。click_gone(maxretry=10, interval=1。0) # maxretry預設值10,interval預設值1。0 # 點選基準位置偏移d(text=“Settings”)。click(offset=(0。5, 0。5)) # 點選中心位置,同d(text=“Settings”)。click()d(text=“Settings”)。click(offset=(0, 0)) # 點選左前位置d(text=“Settings”)。click(offset=(1, 1)) # 點選右下 # 執行雙擊UI物件d(text=“設定”)。double_click() # 雙擊特定ui物件的中心d。double_click(x, y, 0。1) # 兩次單擊之間的預設持續時間為0。1秒 #執行長按UI物件# 長按特定UI物件的中心d(text=“Settings”)。long_click()d。long_click(x, y, 0。5) # 長按座標位置0。5s預設 # 將UI物件拖向另一個點或另一個UI物件# Android<4。3不能使用drag。# 在0。5秒內將UI物件拖到螢幕點(x, y)d(text=“Settings”)。drag_to(x, y, duration=0。5) # 將UI物件拖到另一個UI物件的中心位置,時間為0。25秒d(text=“Settings”)。drag_to(text=“Clock”, duration=0。25)

有很多方法沒有介紹到,在開發使用過程中可以點擊出來看看。

像python程式碼中的安裝、解除安裝等方法筆者也是看原始碼後找到的方法。在網上很多介紹中沒有提及到這塊的內容。

相關文章

頂部