uiautomator是Google提供的用來作安卓自動化測試的一個Java庫。功能很強,能夠對第三方App進行測試,獲取屏幕上任意一個APP的任意一個控件屬性,並對其進行任意操做,但有兩個缺點:node
咱們但願測試可以用Python編寫,可以在電腦上運行的時候就控制手機,因而就有了uiautomator,後來又有了uiautomator2,詳情查看github地址https://github.com/openatx/uiautomator2python
環境準備 python 3.6 uiautomator2 0.3.3linux
分爲兩個部分:android
PC上的python端:運行腳本,並向系統設備發送http請求 移動設備:移動設備上運行了封裝了uiautomator2的HTTP服務,解析收到的請求,並轉化成uiautomator2的代碼。git
詳情能夠參考這篇:https://blog.csdn.net/zhuhuahong/article/details/81868671github
使用pip3安裝uiautomator2 ,安裝的過程當中有點坑,用pip install --pre -U uiautomator2
一直安裝失敗,找了不少教程都是這個。 最後去github上找了下教程https://github.com/openatx/uiautomator2,發現用pip3安裝,抱着試試的心態,安裝成功了 需cd到python的Scripts目錄,再使用pip3安裝app
E:\python36\Scripts>pip3 install -U uiautomator2tcp
確保手機和電腦能連得上,使用adb devices查看連接狀態,adb相關的環境本篇就很少說了,搞android測試的都知道測試
E:\python36\Scripts>adb devices List of devices attached adb server version (31) doesn't match this client (40); killing... * daemon started successfully emulator-5554 device
python -m uiautomator2 initui
E:\python36\Scripts>python -m uiautomator2 init [D 190716 23:34:53 __main__:28] use cache directory: C:\Users\dell\.uiautomator2 Namespace(serial=None, server=None, subparser='init') init [I 190716 23:34:53 __main__:88] >>> Initial device AdbDevice(serial=emulator-5554) [I 190716 23:34:53 __main__:172] Install minicap, minitouch [Kminitouch |################################| 33.5K / 33.5K [?25h[D 190716 23:34:56 __main__:157] Push https://github.com/openatx/stf-binaries/raw/master/node_modules/minitouch-prebuilt/prebuilt/x86/bin/minitouch -> /data/local/tmp/minitouch:0755 [I 190716 23:34:56 __main__:176] abi:x86 seems to be android emulator, skip install minicap [I 190716 23:34:56 __main__:182] Install com.github.uiautomator, com.github.uiautomator.test [D 190716 23:34:56 __main__:100] Shell: ('pm', 'uninstall', 'com.github.uiautomator') [D 190716 23:34:56 __main__:100] Shell: ('pm', 'uninstall', 'com.github.uiautomator.test') [Kapp-uiautomator.apk |################################| 1.7M / 1.7M.7M [?25h[D 190716 23:35:05 __main__:157] Push https://github.com/openatx/android-uiautomator-server/releases/download/1.1.7/app-uiautomator.apk -> /data/local/tmp/app-uiautomator.apk:0644 [D 190716 23:35:05 __main__:100] Shell: ('pm', 'install', '-r', '-t', '/data/local/tmp/app-uiautomator.apk') [Kapp-uiautomator-test.apk |################################| 966.8K / 966.8K [?25h[D 190716 23:35:11 __main__:157] Push https://github.com/openatx/android-uiautomator-server/releases/download/1.1.7/app-uiautomator-test.apk -> /data/local/tmp/app-uiautomator-test.apk:0644 [D 190716 23:35:11 __main__:100] Shell: ('pm', 'install', '-r', '-t', '/data/local/tmp/app-uiautomator-test.apk') [I 190716 23:35:18 __main__:193] Install atx-agent [Katx-agent_0.6.0_linux_386.tar.gz |################################| 4.0M / 4.0M.0M [?25h[D 190716 23:35:23 __main__:157] Push https://github.com/openatx/atx-agent/releases/download/0.6.0/atx-agent_0.6.0_linux_386.tar.gz -> /data/local/tmp/atx-agent:0755 [D 190716 23:35:26 __main__:100] Shell: ('/data/local/tmp/atx-agent', 'server', '--stop') [D 190716 23:35:26 __main__:100] Shell: ('/data/local/tmp/atx-agent', 'server', '-d') [I 190716 23:35:26 __main__:202] Check install [D 190716 23:35:26 __main__:212] Forward: local:tcp:53461 -> remote:tcp:7912 [D 190716 23:35:28 __main__:214] atx-agent version 0.6.0 Successfully init AdbDevice(serial=emulator-5554)
看到Successfully就是安裝成功了(固然老是有幾個小夥伴的手機須要換下了!!!)
python-uiautomator2鏈接手機的方式有兩種,一種是經過WIFI,另一種是經過USB
首先查看uiautomator狀態是啓動狀態
先使用adb devices獲取手機的惟一識別序列號,也就是devices前面的那一串:emulator-5554
E:\python36\Scripts>adb devices List of devices attached emulator-5554 device
測試是否鏈接成功,先打開你的手機上的任意app,好比淘寶,cmd輸入如下信息
E:\python36\Scripts>python Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import uiautomator2 as u2 >>> d = u2.connect_usb('emulator-5554') >>> d.info {'currentPackageName': 'com.taobao.taobao', 'displayHeight': 1280, 'displayRotation': 0, 'displaySizeDpX': 360, 'displaySizeDpY': 640, 'displayWidth': 720, 'productName': 'h60-l01', 'screenOn': True, 'sdkInt': 22, 'naturalOrientation': True} >>>
看到輸出'currentPackageName': 'com.taobao.taobao'說明鏈接成功了
首先保證手機和電腦在一個局域網,能夠先ping 手機ip,看能不能ping的通
>>> import uiautomator2 as u2 >>> d = u2.connect('192.168.1.xx') >>> d.info