uiautomator2學習筆記


准備工作

要求:
    Android版本 4.4+ Python版本3.6+ 連接手機: 開啟開發者選項, 開啟usb調試, 連接電腦,adb devices可以看到設備號 安裝uiautomator2:python -m pip install -U uiautomator2 驗證是否連接成功: import uiautomator2 as u2 driver = u2.connect() # connect to device print(driver.info) 執行以上代碼有正確輸出 安裝: 1、Install uiautomator2 python -m pip install --upgrade --pre uiautomator2 測試是否安裝成功 uiautomator2 --help 2、Install weditor (UI Inspector) python -m pip install -U weditor 在命令行運行weditor --help 確認是否安裝成功 3、Install daemons to a device (Optional) 電腦連接上一個手機或多個手機, 確保adb已經添加到環境變量中,執行下面的命令會自動安裝本庫所需要的設備端程序 python -m uiautomator2 init 安裝提示success即可 4、【可選】AppetizerIO 所見即所得腳本編輯器 AppetizerIO 提供了對uiautomator2的深度集成,可以圖形化管理ATX設備,還有所見即所得腳本編輯器 到網站下載直接打開,首次使用需要注冊賬號 設備管理 界面里可以檢查設備是否正常init,起停atx-agent,抓取atx-agent.log文件 測試腳本調出腳本助手,實時界面同步,點擊界面直接插入各種代碼,同時支持uiautomator和Appium 

連接設備

1、通過wifi,需確保同一網絡 import uiautomator2 as u2 driver = u2.connect('10.0.0.1') # alias for u2.connect_wifi('10.0.0.1') print(driver.info) 2、通過usb線 import uiautomator2 as u2 d = u2.connect('M9N7N15930001618') # 括號內為adb devices 獲取的設備號 print(d.info) 

打開weditor UI查看器

cmd命令:weditor
    1、瀏覽器打開
    2、輸入設備號點擊連接
    3、成功連接設備后刷新獲取最新頁面
    4、鼠標點擊要找的元素,進行定位

cmd命令操作

1、截圖
    uiautomator2 screenshot screenshot.jpg  截圖保存至當前文件夾,命名為screenshot.jpg
    uiautomator2 screenshot E:\liang\tools\screenshot.jpg  截圖保存至指定文件夾,命名為screenshot.jpg
2、獲取當前包名和activity
    uiautomator2 current
    {
        "package": "com.android.browser",
        "activity": "com.uc.browser.InnerUCMobile",
        "pid": 28478
    }
3、卸載
    uiautomator2 uninstall <package-name> # 卸載一個包
    uiautomator2 uninstall <package-name-1> <package-name-2> # 卸載多個包
    uiautomator2 uninstall --all # 全部卸載
4、stop: 停止應用
    uiautomator2 stop com.example.app # 停止一個app
    uiautomator2 stop --all # 停止所有的app

API Documents:

1、啟動/關閉APP
    5、 運行/關閉app
d.app_start('包名')
d.app_stop('包名')
d.app_clear('包名')
擴展:不知道怎么獲取包名的可以打開一個app,doc下輸入uiautomator2 current獲取
    {
        "package": "com.android.browser",
        "activity": "com.uc.browser.InnerUCMobile",
        "pid": 28478
    }

連接手機,啟動APP

import uiautomator2 as u2 d = u2.connect('M9N7N15930001618') #括號內為adb devices獲取的設備號 d.app_start("com.tencent.wework") #括號內為要啟動的APP包名 

API

手勢交互
    點擊屏幕(坐標,支持相對坐標)
        d.click(x, y) d.click(0.5, 0.5) #其中(0.235, 0.456) 代表 X(50%) Y(50%) 雙擊 d.double_click(x, y) #默認兩次點擊相隔0.1秒 d.double_click(x, y, 1) #兩次點擊相隔1秒 長按 d.long_click(x, y) #默認按0.5秒 d.long_click(x, y, 5) # l長按5秒 滑動(滑動屏幕) d.swipe(sx, sy, ex, ey) 從坐標(sx,sy)滑動至坐標(ex,ey),支持相對坐標 d.swipe(sx, sy, ex, ey, 0.5) # 默認滑動時間0.5秒,可修改 SwipeExt 擴展功能 d.swipe_ext("right") # 屏幕右滑,4選1 "left", "right", "up", "down" d.swipe_ext("right", scale=0.9) # 默認0.9, 滑動距離為屏幕寬度的90% d.swipe_ext("right", box=(0, 0, 100, 100)) # 在 (0,0) -> (100, 100) 這個區域做滑動 拖動(例:拖動某個APP) d.drag(sx, sy, ex, ey) #支持相對坐標 d.drag(sx, sy, ex, ey, 0.5) # 默認滑動時間0.5秒,可修改 多個點連續滑動(典型案例:9宮格解鎖) 從point(x0, y0) 滑動到point(x1, y1)然后滑動到point(x2, y2)...等等 d.swipe_points([(x0, y0), (x1, y1), (x2, y2)], 0.2)) #0.2為每次兩點之間滑動的時間,坐標可以是絕對坐標,也可以是相對坐標 d.swipe_points([(0.235, 0.456), (0.503, 0.449), (0.509, 0.601), (0.777, 0.603), (0.771, 0.763), (0.222, 0.75)], 0.2) #其中(0.235, 0.456) 代表 X(23.5%) Y(45.6%) 模擬按下-等待-移動-松開 d.touch.down(10, 10) # 模擬按下 time.sleep(0.01) # down 和 move 之間的延遲,自己控制 d.touch.move(15, 15) # 模擬移動 d.touch.up() # 模擬抬起 截圖 d.screenshot("E:\liang\tools\screenshot.jpg")傳入路徑和名稱 元素選擇器 #定位text為'Clock'並且className為'android.widget.TextView'的元素 d(text='Clock', className='android.widget.TextView') 后代元素 #定位className為"android.widget.ListView"的元素下面text為"Bluetooth"的元素 d(className="android.widget.ListView").child(text="Bluetooth") 兄弟姐妹,同級元素 #定位text為"Google"的元素的同級元素中className="android.widget.ImageView"的元素 d(text="Google").sibling(className="android.widget.ImageView") 通過多個層級多個屬性定位 #className="android.widget.ListView"且resourceId="android:id/list"的元素下面text為"Bluetooth"且className="android.widget.LinearLayout"的元素 d(className="android.widget.ListView", resourceId="android:id/list").child_by_text("Bluetooth", className="android.widget.LinearLayout") 可以由多個層級往下定位,例: d(className="###", resourceId="###").child_by_text("Wi-Fi", className="¥¥¥¥").child(className="****").click() 相對定位 d(A).left(B), 定位A左邊的B d(A).right(B), 定位A右邊的B d(A).up(B), 定位A上邊的B d(A).down(B), 定位A下邊的B 多個同屬性元素索引 如果元素選擇器選擇到了多個同屬性的元素而無法進行更細致的區分,可以用索引選擇指定的元素 d(text="Add new", instance=0) #instance=0表示選擇第一個,依次類推 其他操作: d(text="Add new").count #返回當前屏幕某個屬性元素的個數 len(d(text="Add new")) #返回當前屏幕某個屬性元素列表的長度 d(text="Add new")[0] #用下標選擇指定元素,同d(text="Add new", instance=0) 迭代: for view in d(text="Add new"): view.info 獲取所選ui對象的狀態及其信息 1、檢查元素是否存在 d(text="Settings").exists # 返回布爾值,True if exists, else False d(text="Settings").exists(timeout=3) #增加等待時間為3秒 2、輸出指定元素的信息 d(text="Settings").info 3、獲取、輸入、清除輸入框文本 d(text="Settings").get_text() # 獲取文本內容 d(text="Settings").set_text("My text...") #輸入文本內容 d(text="Settings").clear_text() # 清除文本內容 4、獲取指定元素中心點坐標 x, y = d(text="Settings").center() x, y = d(text="Settings").center(offset=(0, 0)) # offset為指定元素的相對坐標,(0,0)表示元素左上角,(0.5,0.5)表示元素中心,(1,1)表示元素右下角 指定元素點擊操作 d(text="Settings").click() #點擊指定元素中心位置 d(text="Settings").click(timeout=10) #等待元素出現(最多等待10秒)后點擊 d(text="Settings").click(offset=(0.5, 0.5)) # offset為指定元素的相對坐標,(0,0)表示元素左上角,(0.5,0.5)表示元素中心,(1,1)表示元素右下角 clicked = d(text='Skip').click_exists(timeout=10.0) #如果10秒內元素存在,則點擊,默認等待10秒 is_gone = d(text="Skip").click_gone(maxretry=10, interval=1.0) #等待元素消失后點擊,返回布爾值,默認輪詢次數10次,每次間隔時間1秒 d(text="Settings").long_click() #長按指定元素 特定元素的手勢操作 1、將元素拖向另一個點或另一個元素 備注:安卓4.3以下不能用 d(text="Settings").drag_to(x, y, duration=0.5) #將指定元素在0.5秒的時間內拖動至指定坐標 d(text="Settings").drag_to(text="Clock", duration=0.25) #將指定元素在0.25秒的時間內拖動至指定元素的中心位置 2、等待元素出現或消失 d(text="Settings").wait(timeout=3.0) #等待元素出現,等待時間最長3秒,返回布爾值,默認等待時間20秒 d(text="Settings").wait_gone(timeout=1.0) #等待元素消失,等待時間最長1秒,返回布爾值,默認等待時間20秒 3、滾動屏幕 scroll()里面的參數steps默認是滑動一個屏幕的距離 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="Security") 全局設置 # 設置每次UI點擊后1.5秒的延遲 d.click_post_delay = 1.5 # default no delay # 設置默認元素等待超時(秒) d.wait_timeout = 30.0 # default 20.0 設置元素查找等待時間(默認20s) d.implicitly_wait(10.0) 輸入內容 d.send_keys('str') Toast 1、顯示toast d.toast.show("Hello world", 1.0) # show for 1.0s, default 1.0s 2、獲取toast # 5.0: 最大等待時間 # 10.0: toast出現后的緩存時間. 默認 10.0 # "default message": 返回的toast值. Default None d.toast.get_message(5.0, 10.0, "default message") # 一般用法 assert "Short message" in d.toast.get_message(5.0, default="") # 清除toast緩存 d.toast.reset() xpath description -> content-desc(xpath) resourceId -> resource-id(xpath) # 等待元素存在(等待10秒) d.xpath("//android.widget.TextView").wait(10.0) # return bool # 找到元素並點擊 d.xpath("//*[@content-desc='分享']").click() # 檢查是否存在 if d.xpath("//android.widget.TextView[contains(@text, 'Se')]").exists: print("exists") # 獲取所有輸入框的text,屬性和中心點坐標 for elem in d.xpath("//android.widget.TextView").all(): print("Text:", elem.text) # Dictionary eg #{'index': '1', 'text': '999+', 'resource-id': 'com.netease.cloudmusic:id/qb', 'package': 'com.netease.cloudmusic', 'content-desc': '', 'checkable': 'false', 'checked': 'false', 'clickable': 'false','enabled': 'true', 'focusable': 'false', 'focused': 'false','scrollable': 'false','long-clickable': 'false', 'password': 'false', 'selected': 'false', 'visible-to-user': 'true', 'bounds': '[661,1444][718,1478]'}''' print("Attrib:", elem.attrib) # Coordinate eg: (100, 200) print("Position:", elem.center())





免責聲明!

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



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