uiautomator2使用教程


一、要求
python 3.6+
android 4.4+
 
二、介紹

uiautomator2 是一個可以使用Python對Android設備進行UI自動化的庫。其底層基於Google uiautomator,Google提供的uiautomator庫可以獲取屏幕上任意一個APP的任意一個控件屬性,並對其進行任意操作。

三、地址
 
四、安裝
1、安裝uiautomator2
pip install --pre uiautomator2
pip install pillow
 
2、初始化

部署相關的守護進程。

電腦連接上一個手機或多個手機, 確保adb已經添加到環境變量中,執行下面的命令會自動安裝本庫所需要的設備端程序:uiautomator-server 、atx-agent、openstf/minicap、openstf/minitouch

python -m uiautomator2 init

安裝完成,設備上會多一個uiautomator的應用。

 

配置手機設備參數:

有兩種方法,一種是通過WIFI,另一種是通過USB數據線將手機鏈接電腦。

WiFi連接更方便一點,需要保持PC和手機使用的一個WIFI,查看手機連接WIFI的IP地址。

 

3、測試

import uiautomator2 as u2

d = u2.connect('127.0.0.1::6555')

print(d.info)

 

打印結果:

{'currentPackageName': 'com.android.launcher', 'displayHeight': 1280, 'displayRotation': 1, 'displaySizeDpX': 360, 'displaySizeDpY': 640, 'displayWidth': 720, 'productName': 'DUK-AL20', 'screenOn': True, 'sdkInt': 23, 'naturalOrientation': False}

五、元素定位

1、查看app控件

我們可以借助Android SDK自的uiautomatorviewer查看元素,這就要求手機必須以USB的方式連接PC,我前面使用的是WIFI連接進行連接的。所以,openatx提供了另外一個工具weditor 來解決這個問題。

GitHub地址:https://github.com/openatx/weditor

 

(1)、安裝:

pip install --pre --upgrade weditor
(2)、使用
python3 -m weditor
(3)、工具打開
默認會通過瀏覽器打開頁面:http://atx.open.netease.com/
(4)工具的操作步驟
選擇android、輸入手機或者模擬器的ip+端口,點擊connect
dump hierarchy是用來刷新頁面的
鼠標點擊想要的元素,就可以查看他們的控件了
 
 
2、主要語法
 
(1)啟動app
d.app_start("com.addcn.android.house591")

(2)關閉app
cls.d.app_stop("com.addcn.android.house591")

(3)ResourceId定位
cls.d(resourceId="com.addcn.android.house591:id/ad_banner").click()

(4)Text定位
d(text="精選").click()

(5)Description定位
d(description="..").click()

(6)ClassName定位
d(className="android.widget.TextView").click()

(7)xpath定位
d.xpath("//*[@content-desc='分享']").click()

(8)


3、其他操作
(1)#組默認元素等待超時(秒)
cls.d.wait_timeout = 20.0 #默認20

(2)元素拖拽
(3)開關點擊
  • d(A).left(B), selects B on the left side of A.
  • d(A).right(B), selects B on the right side of A.
  • d(A).up(B), selects B above A.
  • d(A).down(B), selects B under A.
  • 例如:
    #選擇“Wi-Fi”右側的“開關” 
    d(text="Wi‑Fi").right(className="android.widget.Switch").click()


(4)獲取/統計某個相同條件的數目
d(text="Add new").count
或者
len(d(text="Add new"))
得知數目之后,我們可以通過索引去定位
d(text="Add new")[0]
d(text="Add new")[1]
也可以遍歷
 
for view in d(text="Add new"):
    view.info 

(5)截圖
#截取屏幕截圖並保存到計算機上的文件中,要求Android> = 4.2。 d.screenshot( “ home.jpg ”) # get PIL.Image格式化圖像。當然,你需要首先安裝pillow 
image = d.screenshot() # default format =“pillow” image.save( “ home.jpg ”)#或home.png。目前,只有PNG和JPG支持

#得到OpenCV的格式圖像。當然,你需要先安裝numpy和cv2
import cv2
image = d.screenshot( format = ' opencv') cv2.imwrite( ' home.jpg '圖像)#獲取原始JPEG數據 imagebin = d.screenshot(格式= '原始') 打開( “ some.jpg ”, “ WB ”).WRITE(imagebin)

(6)手勢操作
1、單擊
d( text = “ Settings ”).click()

2、長按
d( text = “ Settings ”).long_click()


3、將對象拖向另一個點或另一個UI對象
#筆記:拖不能用於為Android <4.3。 #將UI對象拖動到屏幕點(x,y),0.5秒后 d( text = “設置”).drag_to(x,y, duration = 0.5) #將UI對象拖動到另一個(中心位置) UI對象,在0.25秒 d( text = “設置”).drag_to( text = “ Clock ”, duration = 0.25)

4、在屏幕上滑動
# swipe from (sx, sy) to (ex, ey)
d.swipe(sx, sy, ex, ey)
# swipe from (sx, sy) to (ex, ey) with 10 steps
d.swipe(sx, sy, ex, ey, steps=10)


5、在屏幕上拖拽
# drag from (sx, sy) to (ex, ey)
d.drag(sx, sy, ex, ey)
# drag from (sx, sy) to (ex, ey) with 10 steps
d.drag(sx, sy, ex, ey, steps=10)




(7)獲取對象信息和狀態
1、
d(text="Settings").exists 
#如果存在則為True,否則為假
or d.exists(text="Settings") # 進一步使用 d(text="Settings").exists(timeout=3) 
# 等待設置出現在3S,相同.wait(3)

2、檢索特定UI對象的信息
d(text="Settings").info

3、獲取/設置/清除可編輯字段的文本(例如,EditText小部件)
d(text = “ Settings ”).get_text()   # get widget text 
d(text = “ Settings ”).set_text(“ My text ... ”)   #設置文本 
d(text = “ Settings ”).clear_text( )   #清除文字、


(8)系統常用按鍵
# press home key
d.press.home()
# press back key
d.press.back()
# the normal way to press back key
d.press("back")----親測可用
# press keycode 0x07('0') with META ALT(0x02) on
d.press(0x07, 0x02)

  • home                   #手機Home鍵
  • back                   #手機返回鍵
  • left                   #對應鍵盤上的向右鍵<-
  • right                  #對應鍵盤上的向右鍵->
  • up                    #對應鍵盤上的向上鍵
  • down                   #對應鍵盤上的向下鍵
  • center                  #選中?
  • menu                   #菜單
  • search                  #查找?
  • enter                  #對應鍵盤上的Enter鍵
  • delete(or del)                          #對應鍵盤上的DEL鍵 用於刪除
  • recent(recent apps)                  #任務切換
  • volume_up                #聲音向上調整
  • volume_down               #聲音向下調整
  • volume_mute               #靜音按鍵
  • camera                  #拍照
  • power                   #電源鍵



六、使用經驗
1、使用前初始化
python -m uiautomator2 init
2、打開工具
python3 -m weditor


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM