1. 程式人生 > >App 自動化,Appium 憑什麼使用 UiAutomator2?

App 自動化,Appium 憑什麼使用 UiAutomator2?

![image](https://upload-images.jianshu.io/upload_images/1466987-7448def5b0898c8b?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ## 1\. UiAutomator2 是什麼 可能很多人對 UiAutomator2 和 UiAutomator 傻傻分不清楚 UiAutomator 是 Google 開發的一款執行在 Android 裝置上的 UI 自動化測試工具,基於JAVA語言,使用它有一個限制,就是必須打包成 APK 或 JAR,然後上傳到裝置,才能執行 事實上,UiAutomator2 同樣有 JAVA和 Python 版,今天我們聊的是 Python 版本的 UiAutomator2 至於 JAVA 版本的可以參考之前寫的文章:[點我檢視](http://mp.weixin.qq.com/s?__biz=MzU1OTI0NjI1NQ==&mid=2247483953&idx=2&sn=ba9ea0780c4c73689f0f813a78391ecf&chksm=fc1b7cf1cb6cf5e7c318fe18a9589bea0d11c9dbf0a0484f4e4a2717b2a3f11d073d02effe0b&scene=21#wechat_redirect) Python 版本的 UiAutomator2 專案地址: https://github.com/openatx/uiautomator2 ## 2.Appium 和 UiAutomator2 作為移動端自動化的鼻祖,早期版本的 Appium 是基於 UiAutomator 和 Bootstrap.jar 其中,Bootstrap 在 Appium初始化的時候,被推送到 Android 裝置上,負責監聽 Appium 發過來的請求,並轉換後傳送給 UiAutomator 去處理,完成自動化操作 最新版本的 Appium 加入了對 UiAutomator2 的支援,原理進行了更新,功能和穩定性更加完善 原理圖可以參考: ![image](https://upload-images.jianshu.io/upload_images/1466987-0bd848754e50f359?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ## 3.準備 在使用 UiAutomator2 之前,需要做如下準備 1、在 PC 端配置 Android 開發環境 2、使用 pip 安裝 uiautomator2 依賴 ``` # 安裝依賴 pip3 install -U uiautomator2 # 如果需要截圖,需要安裝pillow pip3 install pillow ``` 3、在手機上安裝 atx-agent 應用  ps:atx-agent 作為服務端,一直執行在後臺 ``` # 安裝apk服務到手機上 python -m uiautomator2 init ``` 4、安裝 weditor WEditor 通過 ip 連線手機,即可以實時檢視 App 的介面元素資訊 和 Appium DeskTop 類似,可以模擬點選、滑動操作、生成操作原始碼等功能 首先,通過 pip 安裝 weditor 依賴包 ``` # 基於瀏覽器檢視 App 的介面元素 pip3 install -U weditor ``` 然後,在命令列輸入 weditor,會自動在瀏覽器中開啟,接著通過 ip 連線對應的裝置,即可以獲取裝置端當前介面的控制元件資訊 資訊內容包含:控制元件的層級關係、控制元件 ID、文字內容、座標值等內容 ![image](https://upload-images.jianshu.io/upload_images/1466987-253dc9b300635128?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ## 4.實戰一下 還是以閒魚搜尋商品為例,聊聊 UiAutomator2 的使用  1、連線裝置 使用 UiAutomator2 連線裝置有 3 種方式,分別是: * 區域網裝置 IP 地址 * USB 連線 + 裝置序列號 * ADB + IP + 埠號 ``` import uiautomator2 as u2 # 方式一:區域網裝置ip地址 device = u2.connect(手機ip地址) # 方式二:USB + 裝置序列號 device = u2.connect(手機序列號) # 方式三:ADB+ # 首先,裝置用USB線連線PC,輸入命令:adb tcpip 埠號進行對映 # 拔掉USB線,通過ip地址+埠號進行連線 device = u2.connect_adb_wifi(手機ip地址:埠號) ``` 2、開啟閒魚 APP 呼叫上面 device 物件中 app_start() 方法,傳入應用的包名作為引數可以開啟應用 需要注意的是,方法中的第二個引數如果傳入 True,可以冷啟動 App,預設值為 False ``` # 開啟應用 device.app_start(PACKAGE_NAME, stop=True) ``` 3、點選搜尋欄進入搜尋介面 ![image](https://upload-images.jianshu.io/upload_images/1466987-b481864543addaec?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 首先,全域性設定一個隱式等待,保證查詢元素的時候避免因為卡頓、網路導致的異常 ``` # 隱式等待20s,保證控制元件載入完成 device.implicitly_wait(20) ``` 然後,通過 WEditor 定位到搜尋入口控制元件的基本資訊 ![image](https://upload-images.jianshu.io/upload_images/1466987-e2022b27550e3372?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 常用的 UiAutomator2 定位方式有 6 種,分別是: * ID 定位 * Text 文字定位 * Description 定位 * ClassName 定位 * Xpath 定位 * 組合定位 例如: ``` # 常用的6種定位方式 # 方式一:ID定位 d(resourceId=元素ID).click() # 方式二:Text文字定位 d(text="公眾號:AirPython").click() # 方式三:Description值定位 d(description="AirPython").click() # 方式四:ClassName定位 d(className="android.widget.TextView").click() # 方式五:Xpath定位 d.xpath("//*[@content-desc='AirPython']") # 方式六:組合定位 d(className="android.widget.ListView", resourceId=元素ID) ``` 需要指出的是,當介面屬性值不唯一的時候,組合定位就顯得很實用 本例直接使用 ID 去找到元素,然後執行點選操作,跳轉到搜尋介面 ``` # 點選到搜尋頁面 device(resourceId="com.taobao.idlefish:id/search_bg_img_front",).click() ``` 4、搜尋 UiAutomator2 中提供了 send_keys() 方法,用於向輸入框中設定文字 注意:引數 clear 如果設定為 True,則在輸入內容之前,會先清空輸入框,預設值為 False ``` # 輸入內容 device.send_keys("Python", clear=True) # 點選搜尋按鈕 device(text="搜尋").click() ``` 5、滑動 UiAutomator2 提供了兩個方法用於滑動介面,分別是: * swipe_ext( 滑動方向 ) * swipe( 開始 x 軸,開始 y 軸,結束 x 軸,結束 y 軸值,滑動時間 ) 經過測試發現,滑動操作,swipe_ext() 使用效果不穩定,建議使用 swipe() 函式 ``` for i in range(5): print('滑動一次') swipe_custom(device, 0.5, 0.8, 0.5, 0.2, 1.2) ``` 另外,為了保證相容不同解析度的裝置,建議通過螢幕百分比自定義滑動方法 ``` def swipe_custom(device, start_x_percent, start_y_percent, end_x_percent, end_y_percent, during=1.0, interval=1): """ 自定義滑動,適配性更高 :param device: :param start_x_percent: :param start_y_percent: :param end_x_percent: :param end_y_percent: :param during: :return: """ # 獲取螢幕的寬、高度 width, height = device.window_size() device.swipe(start_x_percent * width, start_y_percent * height, end_x_percent * width, end_y_percent * height, during) if interval > 0: sleep(interval) ``` 6、關閉應用 在完成自動化操作後,就可以呼叫 app_stop() 方法強制關閉應用 ``` # 停止App device.app_stop(PACKAGE_NAME) ``` 當然,可以在每次操作完,使用 UiAutomator2 提供的方法 app_clear() 清除 App 資料 ``` # 清除App資料 # device.app_clear(PACKAGE_NAME) ``` ## 5.最後 通過上面的例項,我們發現 UiAutomator2 相比 Appium,語法更簡潔易懂,程式碼量也少了很多 但是由於 Uiautomator2 僅適用於 Android 端,Appium 擁有多語言、跨平臺的特性,企業級自動化一般會選擇後者 我已經將文中全部原始碼上傳到後臺,關注公眾號「 **AirPython** 」後回覆「 **uiauto2** 」即可獲得全部原始碼 如果你覺得文章還不錯,請大家 **點贊、分享、留言**下,因為這將是我持續輸出更多優質文章的最強動力! **推薦閱讀** [帶你用 Python 實現自動化群控(入門篇)](http://mp.weixin.qq.com/s?__biz=MzU1OTI0NjI1NQ==&mid=2247486180&idx=1&sn=7a1d282e608ec14c655e8105984639f7&chksm=fc1b7424cb6cfd3207004c88cd8820a2d7f0b1ad31ba24ab7952a2d66810460bfddc6cd4bc18&scene=21#wechat_redirect) [聊聊 Python 做微信小程式自動化,那些踩過的坑?](http://mp.weixin.qq.com/s?__biz=MzU1OTI0NjI1NQ==&mid=2247485382&idx=1&sn=f88a15bfdcbd6d05d1f3bc71decf6357&chksm=fc1b7906cb6cf01014518deb38f0d8bcd64ada62f0946621d4b64b630cb343cf171843c1967f&scene=21#wechat_redirect) [Python 自動化,Helium 憑什麼取代 Selenium?](http://mp.weixin.qq.com/s?__biz=MzU1OTI0NjI1NQ==&mid=2247486205&idx=1&sn=33dd05fc416daf1cc041bc76b53b733a&chksm=fc1b743dcb6cfd2b3dd1f1585bf0c3a4175e39a6dfdc7fe8a5792a405c50da2d7569fb361454&scene=21#wechat_re