我們可以借助Android SDK自的uiautomatorviewer查看元素,這就要求手機(jī)必須以USB的方式連接PC,我前面使用的是WIFI連接進(jìn)行連接的。所以,openatx提供了另外一個(gè)工具weditor 來解決這個(gè)問題。 GitHub地址:https://github.com/openatx/weditor (1)、安裝: pip install --pre --upgrade weditor (2)、使用 python3 -m weditor (3)、工具打開 默認(rèn)會(huì)通過瀏覽器打開頁面:http://atx.open./
(4)工具的操作步驟
選擇android、輸入手機(jī)或者模擬器的ip+端口,點(diǎn)擊connect
dump hierarchy是用來刷新頁面的
鼠標(biāo)點(diǎn)擊想要的元素,就可以查看他們的控件了
2、主要語法
(1)啟動(dòng)app
d.app_start("com.addcn.android.house591")
(2)關(guān)閉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)#組默認(rèn)元素等待超時(shí)(秒) cls.d.wait_timeout = 20.0 #默認(rèn)20
(2)元素拖拽 (3)開關(guān)點(diǎn)擊
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”右側(cè)的“開關(guān)” d(text="Wi?Fi").right(className="android.widget.Switch").click()
(4)獲取/統(tǒng)計(jì)某個(gè)相同條件的數(shù)目
d(text="Add new").count 或者 len(d(text="Add new")) 得知數(shù)目之后,我們可以通過索引去定位
d(text="Add new")[0]d(text="Add new")[1]
也可以遍歷
for view in d(text="Add new"): view.info (5)截圖
#截取屏幕截圖并保存到計(jì)算機(jī)上的文件中,要求Android> = 4.2。
d.screenshot( “ home.jpg ”) # get PIL.Image格式化圖像。當(dāng)然,你需要首先安裝pillow image = d.screenshot() # default format =“pillow” image.save( “ home.jpg ”)?;騢ome.png。目前,只有PNG和JPG支持
#得到OpenCV的格式圖像。當(dāng)然,你需要先安裝numpy和cv2
import cv2 image = d.screenshot( format = ' opencv') cv2.imwrite( ' home.jpg '圖像)#獲取原始JPEG數(shù)據(jù) imagebin = d.screenshot(格式= '原始') 打開( “ some.jpg ”, “ WB ”).WRITE(imagebin)
(6)手勢操作 1、單擊
d( text = “ Settings ”).click()
2、長按
d( text = “ Settings ”).long_click()
3、將對象拖向另一個(gè)點(diǎn)或另一個(gè)UI對象
#筆記:拖不能用于為Android <4.3。#將UI對象拖動(dòng)到屏幕點(diǎn)(x,y),0.5秒后
d( text = “設(shè)置”).drag_to(x,y, duration = 0.5)#將UI對象拖動(dòng)到另一個(gè)(中心位置) UI對象,在0.25秒
d( text = “設(shè)置”).drag_to( text = “ Clock ”, duration = 0.25)
4、在屏幕上滑動(dòng)
# swipe from (sx, sy) to (ex, ey)d.swipe(sx, sy, ex, ey)
# swipe from (sx, sy) to (ex, ey) with 10 stepsd.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 stepsd.drag(sx, sy, ex, ey, steps=10)
(7)獲取對象信息和狀態(tài) 1、 d(text="Settings").exists #如果存在則為True,否則為假 or d.exists(text="Settings") # 進(jìn)一步使用 d(text="Settings").exists(timeout=3) # 等待設(shè)置出現(xiàn)在3S,相同.wait(3)
2、檢索特定UI對象的信息 d(text="Settings").info
3、獲取/設(shè)置/清除可編輯字段的文本(例如,EditText小部件) d(text = “ Settings ”).get_text() # get widget text
d(text = “ Settings ”).set_text(“ My text ... ”) #設(shè)置文本
d(text = “ Settings ”).clear_text( ) #清除文字、
(8)系統(tǒng)常用按鍵 # 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 #手機(jī)Home鍵
back #手機(jī)返回鍵
left #對應(yīng)鍵盤上的向右鍵<-
right #對應(yīng)鍵盤上的向右鍵->
up #對應(yīng)鍵盤上的向上鍵
down #對應(yīng)鍵盤上的向下鍵
center #選中?
menu #菜單
search #查找?
enter #對應(yīng)鍵盤上的Enter鍵
delete (or del ) #對應(yīng)鍵盤上的DEL鍵 用于刪除
recent (recent apps) #任務(wù)切換
volume_up #聲音向上調(diào)整
volume_down #聲音向下調(diào)整
volume_mute #靜音按鍵
camera #拍照
power #電源鍵
六、使用經(jīng)驗(yàn) 1、使用前初始化 python -m uiautomator2 init 2、打開工具 python3 -m weditor
|