uiautomator2使用教程
一、簡單介紹
1、自動化測試開源工具,僅支持Android平台的原生應用測試
2、目前僅支持腳本語言python
3、封裝谷歌自帶的uiautomator測試框架,提供便利的python接口
google提供的uiautomator庫可獲取安卓APP的控件屬性,並執行相關操作,但有兩個缺點:
① 測試腳本只能使用Java語言
② 測試腳本必須每次被上傳到設備上運行
4、工作原理:
原理是在手機上運行了一個http服務器,將uiautomator中的功能開放出來,然后再將這些http接口,封裝成Python庫
以下解釋直接引用網址:https://testerhome.com/topics/11357
① 在移動設備上安裝atx-agent(守護進程), 隨后atx-agent啟動uiautomator2服務(默認7912端口)進行監聽
② 在PC上編寫測試腳本並執行(相當於發送HTTP請求到移動設備的server端)
③ 移動設備通過WIFI或USB接收到PC上發來的HTTP請求,執行制定的操作
二、實現功能
1、獲取手機部分信息(d.info)
2、操作手機設備,比如點擊Home鍵、鎖屏、查看快捷鍵等(d.press("home")、d.screen_off()、d.open_quick_settings()等)
3、安裝、啟動、卸載應用
d.app_install('http://some-domain.com/some.apk')
d.app_start("com.example.xxx")
d.app_stop("com.example.xxx")
4、識別手機上控件、對控件進行相關操作等
d(text='Clock', className='android.widget.TextView')
參數可支持以下:
● text, textContains, textMatches, textStartsWith
● className, classNameMatches
● description, descriptionContains, descriptionMatches, descriptionStartsWith
● checkable, checked, clickable, longClickable
● scrollable, enabled,focusable, focused, selected
● packageName, packageNameMatches
● resourceId, resourceIdMatches
● index, instance
uiautomator2 是一個可以使用Python對Android設備進行UI自動化的庫。其底層基於Google uiautomator,Google提供的uiautomator庫可以獲取屏幕上任意一個APP的任意一個控件屬性,並對其進行任意操作。
三、地址部署相關的守護進程。
電腦連接上一個手機或多個手機, 確保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)
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
(ordel
) #對應鍵盤上的DEL鍵 用於刪除recent
(recent apps) #任務切換volume_up #聲音向上調整
volume_down #聲音向下調整
volume_mute #靜音按鍵
camera #拍照
power #電源鍵
一、Uiautomator2原理介紹
1.uiautomator2是一個可以使用Python對Android設備進行UI自動化的庫。其底層基於Google uiautomator,Google提供的uiautomator庫可以獲取屏幕上任意一個APP的任意一個控件屬性,並對其進行任意操作,目前僅支持android平台的原生應用測試,https://github.com/openatx/uiautomator2。但有兩個缺點:
測試腳本只能使用Java語言。
測試腳本必須每次被上傳到設備上運行。
2.工作原理
分為兩個部分:
PC上的python端:運行腳本,並向系統設備發送http請求
移動設備:移動設備上運行了封裝了uiautomator2的HTTP服務,解析收到的請求,並轉化成uiautomator2的代碼。
二、安裝uiautomator2
pip install --pre uiautomator2
pip install pillow
三、初始化
部署相關的守護進程。
電腦連接上一個手機或多個手機, 確保adb已經添加到環境變量中,執行下面的命令會自動安裝本庫所需要的設備端程序:uiautomator-server 、atx-agent、openstf/minicap、openstf/minitouch
1
|
python -m uiautomator2 init
|
安裝完成,設備上會多一個uiautomator的應用。
配置手機設備參數:
有兩種方法,一種是通過WIFI,另一種是通過USB數據線將手機鏈接電腦。 (我是通過USB這個方法,按照后手機會多一個ATX小汽車圖標的軟件)
WiFi連接更方便一點,需要保持PC和手機使用的一個WIFI,查看手機連接WIFI的IP地址。
連接手機的方式:
1.通過WiFi:ip根據情況自己修改
d=u2.connect_wifi("ip")
2.通過d=u2.connect_usb()
d=u2.connect_usb()
3.安裝應用
d.app_install(' url ')
4.跳過彈窗,禁止彈窗
d.disable_popups() #自動跳過彈出窗口
d.disable_popups(假)#禁用自動跳過彈出窗口
5.獲取基本信息
d.info
6.獲取窗口大小
print(d.window_size())
7.打開/關閉屏幕
d.screen_on()#打開
d.screen_off()#關閉
8.獲取當前屏幕狀態
d.info.get(' screenOn ')
9.解鎖屏幕
d.unlock()
10.點擊屏幕
d.click(x,y)
11.雙擊
d.double_click(x,y)
12.長按一下屏幕
d.long_click(x,y)
d.long_click(X,Y,1)#長按1秒(默認)
13.滑動
d.swipe(x, y, x, y)
14.按鍵操作
d.press("home")
d.press("back")
d.press(x, y)
15.向上滑動
d(scrollable=True).scroll.vert.backward()
16.解鎖屏幕
d.healthcheck() # 解鎖屏幕並啟動uiautomator服務
最后的d.service("uiautomator").stop()
是因為,安卓上的UiAutomator是獨享的,一旦一個服務使用了它,其他人就不讓碰了。所以 appium
, macaca
, uiautomatorviewer.bat
只要你用了UiAutomator服務,都是沖突的。只有再用完之后,停止掉uiautomator service,才能讓其他服務使用
testerhome.cn
https://106.75.214.88/topics/12521