項目地址:https://github.com/openatx/uiautomator2,詳情見文檔


命令行
python -m weditor(http://atx.open.netease.com)ctrl+c退出服務
每次運行腳本,要執行下邊的命令一遍,這樣才會打開服務,上面的ui,和腳本才會奏效
python -m uiautomator2 init(這個命令會安裝到設備端一個軟件):要連接電腦
也就是雖然可以無線進行控制,但前提是要進行連接之后。。
不常用的
pip install --pre uiautomator2
pip install pillow
python -m uiautomator2 init(這個命令會安裝到設備端一個軟件)
python -m uiautomator2 clear-cache
python -m uiautomator2 app-stop-all device_ip(可以是ip,也可以是序列號)
實用

就用這四種方式定位元素,其他的不用定位元素,全靠不標准的滑動等
定位的時候直接復制code,

d.set_fastinput_ime(True)
這句代碼會關閉我們默認的輸入法,而使用IME輸入法

獲取點擊的時候,默認都是使用的百分比
這個框架到現在還不完善,有好多欠缺,比如監視器不好使,滾動也支持的不多,但日常簡單需求可以滿足
如何停用uiautomator:
輔助功能:就是方便人們使用的一些功能,增加一些快捷鍵。按電源按鈕可以結束通話。,自動安裝等等,在設置中可以打開
uiautomatorviewer:是Androidsdk中的一個工具,可以審查元素等等,appium-desktop好像就是基於他的,反正當你運行着uiautomator時,如果再用appium-desk執行session就會報錯
1.打開app,點擊quit是全部退出,直接退出app,服務也會全部停止
2.d.service("uiautomator").stop()
關於session,不是太明白:
import uiautomator2 as u2
d = u2.connect('192.168.1.103')
# 第一步連接
# d = u2.connect('192.168.1.103'/‘序列號’)
# d = u2.connect_usb("{Your-Device-Serial}")
# d=u2.connect_wifi('10.0.0.1')
# app操作
# 安裝app
# d.app_install('http://some-domain.com/some.apk')
# 清楚數據
# d.app_clear()
# d.app_uninstall()
# d.app_uninstall_all()
# 不能用
# d.dismiss_apps()
# 打開app/切換app
# d.app_start('com.tencent.tim')
# d.app_start('com.kugou.android')
# d.app_start('com.tencent.tim')
# d.app_start('com.kugou.android')
# d.app_start('com.tencent.tim')
# 關閉app,有時我們需要先關閉app
# d.app_stop('com.tencent.tim')
# d.app_stop('com.tencent.mm')
# d.app_stop('com.kugou.android')
# 停止所有正在進行的app
# d.app_stop_all()
# 可以保護的白名單
# d.app_stop_all(excludes=['com.examples.demo'])
# 基於元素選擇器
# 元素的選擇就用復制的
# d(text='Clock', className='android.widget.TextView')
# 但是不能操作可滾動的,只能通過大概的觸摸,任何在元素選擇
# d(text="Settings").get_text() # get widget text
# d(text="Settings").set_text("My text...") # set the text
# d(text="Settings").clear_text() # clear the text
# 元素的拖拽
# d(text="Settings").drag_to(x, y, duration=0.5)
# drag the UI object to (the center position of) another UI object, in 0.25 second
# d(text="Settings").drag_to(text="Clock", duration=0.25)
# 元素的兩指操作
# d(text="Settings").gesture((sx1, sy1), (sx2, sy2), (ex1, ey1), (ex2, ey2))
# d(text="Settings").pinch_in(percent=100, steps=10)
# d(text="Settings").pinch_out()
# 元素是否消失返回真假
# d(text="Settings").wait(timeout=3.0) # return bool
# d(text="Settings").wait_gone(timeout=1.0)
# 從屏幕上找可以滾動的元素
# 慣性滑動
# d(scrollable=True).fling()
# 慣性滑動向前
# d(scrollable=True).fling.horiz.forward()
# # fling backward vertically
# d(scrollable=True).fling.vert.backward()
# # fling to beginning horizontally
# d(scrollable=True).fling.horiz.toBeginning(max_swipes=1000)
# # fling to end vertically
# d(scrollable=True).fling.toEnd()
# 滾動默認向前,垂直方向 向前,向后,開始,結尾,到特定元素
# d(scrollable=True).scroll(steps=10)
# # scroll forward horizontally
# d(scrollable=True).scroll.horiz.forward(steps=100)
# # scroll backward vertically
# d(scrollable=True).scroll.vert.backward()
# # scroll to beginning horizontally
# d(scrollable=True).scroll.horiz.toBeginning(steps=100, max_swipes=1000)
# # scroll to end vertically
# d(scrollable=True).scroll.toEnd()
# # scroll forward vertically until specific ui object appears
# d(scrollable=True).scroll.to(text="Security")
# d(scrollable=True).scroll.to(text="信息學院16級")
d(scrollable=True).scroll.to(text="信息學院16級")
# 操作--session中的
# 點擊d.tap()就是d.click()
# 一定時間內出現就點擊,不出現就不惦記了
# clicked = d(text='Skip').click_exists(timeout=10.0)
# 點擊,坐標直接復制
# d.click()
# d.long_click(x, y)
# d.long_click(x, y, 0.5) # long click 0.5s (default)
# 執行拖拽操作
# d.swipe((x,y),(x1,y1))
# d.swipe((x,y),(x1,y1)) # swipe for 0.5s(default)
# 盡量不用這個
# d.drag(sx, sy, ex, ey)
# d.drag(sx, sy, ex, ey, 0.5) # swipe for 0.5s(default)
# 最后的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)
# 按硬件
# d.press("home")
# d.press("back")
# list=[ 'home','back', 'left','right', 'up', 'down', 'center', 'menu', 'search',
# 'enter','delete', 'recent', 'volume_up', 'volume_down', 'volume_mute', 'camera', 'power']
# for i in list:
# d.press(i)
#上下左右:可以用來選擇一些可選的元素,比如停留在桌面可以用來選擇打開的app
# center,enter就是回車,可以當點擊
# recent就是左鍵
# 聲音上下鍵,還有靜音
# camrea打開
# delet刪除一個字符
# power就是電源按鍵
# 文字操作
# 打開輸入法之后就會替換默認的輸入法,注意只有遇到可以輸入子的地方就會觸發
# d.set_fastinput_ime(True)
# d.send_keys("你好 Hello")
# d.set_fastinput_ime(False)
# d.clear_text()
# 打開通知
# d.open_notification()
# 快速設置----不能用
# d.open_quick_settings()
# # 屏幕操作
# d.screen_on()
# d.screen_off()
# 返回ture/false
# a=d.info.get('screenOn')
# 設置屏幕方向
# orientation = d.orientation
# d.set_orientation('l') # or "left"
# d.set_orientation("l") # or "left"
# d.set_orientation("r") # or "right"
# d.set_orientation("n") # or "natural"
# # freeze rotation
# d.freeze_rotation()
# # un-freeze rotation
# d.freeze_rotation(False)
# 獲取設備信息
# print(d.device_info)
# print(d.window_size())
# print(d.current_app())
# print(d.serial)
# 測試完成關閉手機客戶端
# d.service("uiautomator").stop()
# 沒用的東西
# 檢查並維持設備端守護進程處於運行狀態,幾乎沒什么用,因為當我們第一次打開app的時候他就會檢驗一次並且打開
# d.healthcheck()
# 打開調試,本來就是打開的幾乎沒用
# d.debug = True
# session不能用
# sess = d.session(" com.netease.cloudmusic")
# sess2 = d.session('com.netease.cloudmusic ',attach = True)
# sess(text="Music").click()
# sess.running()
# 電腦手機傳送文件,只能是單文件,不能是文件夾
# 到手機
# d.push("foo.txt", "/sdcard/")
# 可以改名
# d.push("foo.txt", "/sdcard/bar.txt")
# push fileobj
# with open("foo.txt", 'rb') as f:
# d.push(f, "/sdcard/")
# push and change file access mode
# d.push("foo.sh", "/data/local/tmp/", mode=0o755)
# 到電腦
# d.pull("/sdcard/tmp.txt", "tmp.txt")
# FileNotFoundError will raise if the file is not found on the device
# d.pull("/sdcard/some-file-not-exists.txt", "tmp.txt")
# 監視器,當不能進行測試發生意外情況是觸發,方法也就哪幾種:
