uiautomator2中文文檔


1、安裝:

pip install --pre uiautomator2
#或者你可以直接從github上源碼安裝
git clone https://github.com/openatx/uiautomator2
pip install -e uiautomator2
pip install pillow #截屏工具
2、初始化手機,需要的環境SDK

python -m uiautomator2 init
這個只有初始化后才可以用
pip install --pre --upgrade weditor#安裝自動化UI定位
python -m weditor#啟動
自動安裝本庫所需的設備端程序:uiautomator-server ,atx-agent ,openstf / minicap ,openstf / minitouch  可單獨下載安裝

配置工作完成

3、打開手機端口:adb forward tcp:7912 tcp:7912 

     連接手機:device_ip = 127.0.0.1 

    這個不知道干嘛的忘記了好像是是自行安裝atx-agent的時候用的

4、常用命令:

1、安裝apk:python -m uiautomator2 install $ device_ip https://example.org/some.apk
2、清緩存:python -m uiautomator2
3、停止所有應用程序:python -m uiautomator2 app-stop-all $ device_ip
4、截圖:python -m uiautomator2截圖$ device_ip screenshot.jpg
5、檢查守護線程:d.healthcheck()
6、打開調試:d.debug = true
7、獲取連接信息:d.info
8、shell命令:d.adb_shell('pwd')
9、分辨率:d.window_size()
10、查看當前應用信息:d.current_app()
11、查看序列號:d.serial
5、快速開始:

import uiautomator2 as u2
#通過WIFI
d = u2.connect('10 .0.0.1')#u2.connect_wifi('10 .0.0.1')的#別名 
#通過usb獲取設備
d = u2.connect('123456f')#u2.connect_usb('123456f')的#別名
6、系統操作:

1、安裝,只指出url:d.app_install('http://some-domain.com/some.apk')
2、啟動:d.app_start(“com.example.hello_world”)#以包名稱開頭
3、停止應用:d.app_stop(“com.example.hello_world”)
         d.app_clear('com.example.hello_world')
        停止所有應用:d.app_stop_all()
5、推送一個文件到手機:d.push(“foo.txt的”,“/ SD卡/”)
6、推和重命名:d.push(“foo.txt的”,“/sdcard/bar.txt”)
7、推送並更改文件模式:d.push(“foo.sh”,“/ data / local / tmp /”,mode = 0o755)
8、從設備中拉取文件:d.pull(“/ sdcard / tmp.txt”,“tmp.txt”)#如果文件在設備上找不到,FileNotFoundError將會出現 
    d.pull(“/ sdcard / some-file-not-exist.txt”,“tmp.txt”)
7,應用連接會話:

1、啟動應用:sess = d.session(“com.netease.cloudmusic”
2、會話連接運行中的程序:sess = d.session(“com.netease.cloudmusic”
3、檢測應否崩潰:SESS(文字= “音樂”)點擊()
    檢查會話是否正常:sess.running()
8,手機硬件操作


1、d.screen_on()#打開屏幕d.screen_off()#關閉屏幕
2、獲取當前屏幕狀態:d.info.get('screenOn')#android 4.4
3、按軟/硬件
d、press(“home”)#按home鍵,用鍵名 
d、press(“back”)#按返回鍵,與主要的名稱 
d、press(0x07,0x02)#按下鍵碼0×07(“0”)與META ALT(0x02)
d、unlock()解鎖屏幕                                                                                                                 
這些目前支持:
home                    back                  left                  right
up                        down
center                   menu                 search             enter
delete ( or del)          recent (recent apps)        volume_up           
volume_down         volume_mute           camera    power
9、事件:

支持百分比d.long_click(0.5,0.5)

1、點擊屏幕:d.click(X,Y)

2、長按屏幕:d.long_click(X,Y)或d.long_click(X,Y,0.5)#長按0.5秒(默認)

3、拖動:d.drag(SX,SY,EX,EY)或d.drag(SX,SY,EX,EY,0.5)#刷新0.5秒(默認)

4、刷卡:d.swipe(sx,sy,ex,ey)或d.swipe(sx,sy,ex,ey,0.5)#刷新0.5秒(默認)

5、滑動:#由點划動(X0,Y0)到點(X1,Y1),然后到點(X2,Y2) 

     #時間將加速0.2秒bwtween兩點 

     d.swipe((X0,Y0),(X1,Y1),(X2,Y2),0.2)

10,屏幕相關:


1、獲取方向:d.orientation

2、設置方向:d.set_orientation( 'L') 

      值:natural或者n

       left或者l

       right或者r

        upsidedown或者u(不能設置)

3、不能選擇/解凍:d.freeze_rotation()#凍結旋轉d.freeze_rotation(False)

4、截屏:image = d.screenshot()#支持png和jpg

        image.save( “home.jpg”) 

5、截屏: 

         import cv2

         image = d.screenshot(format ='opencv')

         cv2.imwrite('home.jpg',image)

6、屏幕UI轉xml:xml = d.dump_hierarchy()

7、打開通知或快速設置:d.open_notification()d.open_quick_settings()

11、選擇器:識別屏幕對象,請參閱UiSelector Java doc。

支持的參數:

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 
1、選擇:d(text ='Clock',className ='android.widget.TextView')

2、獲取孩子或孫子:d(className =“android.widget.ListView”).child(text =“Bluetooth”)

3、兄弟姐妹:d(文本= “谷歌”)兄弟(類名= “android.widget.ImageView”)。

4、獲取子節點根據文本:d(className =“android.widget.ListView”,resourceId =“android:id / list”)\

  .child_by_text(“Bluetooth”,className =“android.widget.LinearLayout”)

5、獲取子節點根據文本允許滾動搜素:d(className =“android.widget.ListView”,resourceId =“android:id / list”)\

  .child_by_text( “藍牙”,allow_scroll_search =真,類名= “android.widget.LinearLayout”)

6、支持多級:d(className =“android.widget.ListView”,resourceId =“android:id / list”)\ 

   .child_by_text(“Wi-Fi”,className =“android.widget.LinearLayout”)\ 

   .child(className =“android .widget.Switch”)\ .click()

7、支持相對定位:

 •d(A).left(B),在A的左側選擇B.

 •d(A)。右(B),在A的右側選擇B.

 •D(A).up(B),在A之上選擇B.

 •d(A).down(B),在A下選擇B.

 d(文= “無線網絡連接”)。右(的className = “android.widget.Switch”)。點擊()

8、第一個文本帶有“Add new”的實例:d(text =“Add new”,instance = 0)

9、在屏幕上查找:d(text =“Add new”)。count

 d(text =“添加新的”)[0]#第一個

 d(text =“添加新的”)[1]#第二個

10、顯示控件信息:view.info 

11、檢查控件是否存在:d(text =“Settings”).exists或者d.exists(text =“Settings”)

12、檢查控件信息:d(text =“設置”).info

13、文本控件操作:

 d(text =“Settings”).get_text()#獲取小部件文本 

 d(text =“Settings”).set_text(“My text ...”)#設置文本 

 d(text =“Settings”).clear_text)#清除文字

14、點擊:

 d(text =“設置”).click()

 #等待元素出現最多10秒鍾,然后點擊 

 d(text =“設置”).click(超時時間= 10)

 #點擊的別名 

 d(text=“設置”).tap()

15、長按:d(text =“設置”).long_click()

16、拖動元素到另一個位子:

 d(text =“Settings”).drag_to(x,y,duration = 0.5)

 d(text =“Settings”).drag_to(text =“Clock”,duration = 0.25)#clock的中心點

17、從一點到另一點的兩點手勢

 d(text =“Settings”).gesture((sx1,sy1),(sx2,sy2),(ex1,ey1),(ex2,ey2))

18、特定UI對象上的兩點手勢

 •從邊緣到中心

 •從中心到邊緣

 #從邊緣到中心。這里是“以”不“在” 

 d(text =“Settings”)。pinch_in(percent = 100,steps = 10)

 #從中心到邊緣 

 d(text= “設置”)pinch_out。()

19、等待用戶界面出現或消失

 d(text =“Settings”)。wait(timeout = 3.0)#return bool

 d(text= “設置”)。wait_gone(超時= 1.0)

20、#設置默認元素等待超時(秒) 

 d.wait_timeout = 30.0 

21、控件不知道控件的情況下

 d.set_fastinput_ime(真)#切換成FastInputIME輸入法 

 d.send_keys(“你好123abcEFG”)#adb廣播輸入 

 d.clear_text()#清除輸入框所有內容(需要android-uiautomator.apk版本> = 1.0.7) 

 d.set_fastinput_ime(假)#切換成正常的輸入法

22,顯示:吐司

 d.make_toast(“Hello world”)

 d.make_toast(“Hello world”,1.5)#顯示1.5秒

child_by_description 就是找到孫子有特定描述的兒童,其他參數與之相似child_by_text 。

child_by_instance 就是要找到在其子層次結構中指定實例的任何位置具有子UI 元素的子元素。在它滾動沒有可見的視圖上執行。

詳細信息請參閱下面的鏈接: • UiScrollable ,getChildByDescription ,getChildByText ,getChildByInstance

12、常見的問題:

    如圖1所示,發現SDK的某些功能或手機的不能用:直接打開uiautomator 的應用程序(INIT 成功后,就會安裝上的),點擊關閉UIAutomator  或者d.service (“uiautomator” ).stop () 

    2502錯誤:

控制台:adb shell am instrument -w -r -e debug false -e class com.github.uiautomator.stub.Stub \
  com.github.uiautomator.test / android.support.test.runner.AndroidJUnitRunner  

13. 復雜屏幕操作

 

2.等待對象顯示並點擊,等待最多10s:

d(text="Settings").click(timeout=10)

3.當10s內對象出現就點擊,默認0s:

clicked = d(text='Skip').click_exists(timeout=10.0)

4.點擊並輪詢對象直到消失,其中maxretry為最多點擊次數,默認10;interval為輪詢時間間隔,默認1:

d(text="Skip").click_gone(maxretry=10, interval=1.0)

5.長按指定的對象,duration為按住時間,timeout為超時時間:

d(text="Settings").long_click(duration=10,timeout=10)

6.拖動指定對象到x,y位置,0.5s完成:

d(text="Settings").drag_to(x, y, duration=0.5)

7.拖動指定對象到另一個對象位置,0.5s內完成:

d(text="Settings").drag_to(text="Clock", duration=0.5)

8.雙指從(a,b),(c,d)滑動(a1,b1),(c1,d1),步長100:

d().gesture((a,b),(c,d), a1,b1),(c1,d1),steps=100)

9.從屏幕外側向中心滑動,percent為左右起始位置占兩邊的比例:

d().pinch_out(percent=33, steps=100)

10.從屏幕中心向外側滑動,percent為左右起始位置占兩邊的比例:

d().pinch_out(percent=33, steps=100)

11.等待對象:

d(text='相機').wait(10)

12.等待對象消失,最多等待10s:

d(text='相機').wait_gone(timeout=10)

13.滾動:

a.向上滾動:d(scrollable=True).scroll(steps=10)

b.向下滑動:d(scrollable=True).scroll.vert.backward()

c.水平向右滾動:d(scrollable=True).scroll.horiz.forward(steps=50)

d.水平向左滾動:d(scrollable=True).scroll.horiz.backward(steps=50)

e.水平滑動到最左邊:d(scrollable=True).scroll.horiz.toBeginning(steps=100, max_swipes=1000)

f.水平滑動到最右邊:d(scrollable=True).scroll.horiz.toEnd(steps=100, max_swipes=1000)

g.豎直滑動到結尾:d(scrollable=True).scroll.toEnd()

h.豎直滑動到開頭:d(scrollable=True).scroll.toBeginning(steps=50)

i.滑動到指定位置(測試):d(scrollable=True).scroll.to(text ='測試')

    如果運行正常,啟動測試之前增加一行代碼d.healthcheck()
   如果報錯,可能是缺少某個設備組件沒有安裝,使用下面的命令重新初始化python -m uiautomator2 init --reinstall 

 手機python -m uiautomator2 init 之后,瀏覽器輸入<device_ip :7912> ,會有一個遠程控制功能


免責聲明!

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



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