1、安装
1. pip install --pre uiautomator2
2. #或者你可以直接从github源码安装
3. git clone https://github.com/openatx/uiautomator2
4. pip install -e uiautomator2
pip install pillow #截屏工具
2、初始化手机,需要的环境sdk
1. python -m uiautomator2 init
自动安装本库所需的设备端程序:
uiautomator-server
,
atx-agent
,
openstf / minicap
,
openstf / minitouch
可单独下载安装
3、打开手机端口:
adb forward tcp:7912 tcp:7912
连接手机
device_ip=127.0.0.1
4、常用命令:
1. 1、安装apk:python -m uiautomator2 install $ device_ip https://example.org/some.apk
2. 2、清缓存:python -m uiautomator2
3. 3、停止所有应用:python -m uiautomator2 app-stop-all $ device_ip
4. 4、截图:python -m uiautomator2截图$ device_ip screenshot.jpg
5. 5、检查守护线程:d.healthcheck()
6. 6、打开调试:d.debug = true
7. 7、获取连接信息:d.info
8. 8、shell命令:d.adb_shell(' pwd ')
9. 9、分辨率:d.window_size()
10. 10、查看当前应用信息:d.current_app()
11. 11、查看序列号:d.serial
5、连接:
1. import uiautomator2 as u2
2. #通过WIFI
3. d
=
u2
.connect('10.0.0.1') # alias for u2.connect_wifi('10.0.0.1')
4. #通过usb获取 devices
5. d
=
u2
.connect('123456f') # alias for u2.connect_usb('123456f')
6、系统操作:
1. 1、安装,只指出url:d.app_install(' http://some-domain.com/some.apk ')
2. 2、启动:d.app_start(“ com.example.hello_world ”)#以包名称开头
3. 3、停止应用:d.app_stop( “ com.example.hello_world ”)或者d.app_clear( ' com.example.hello_world ')
4. 4、停止所有应用:d.app_stop_all()
5. 5、推送一个文件到手机:d.push( “ foo.txt的”, “ / SD卡/ ”)
6. 6、推和重命名 :d.push( “ foo.txt的”, “ /sdcard/bar.txt ”)
7. 7、推送并更改文件模式:d.push("foo.sh", "/data/local/tmp/", mode=0o755)
8. 8、从设备中拉取文件:d.pull(“/ sdcard/tmp.txt ”,“ tmp.txt ”)#如果文件在设备上找不到,FileNotFoundError将会出现
9. d.pull(“/ sdcard/some-file-not-exists.txt ”,“ tmp.txt ”)
7、app连接会话:
1. 1、启动应用:sess = d.session(“ com.netease.cloudmusic ”
2. 2、会话连接运行中的程序:sess = d.session(“ com.netease.cloudmusic ”
3. 3、检测应否崩溃:sess(text="Music").click()
4. 4、检查会话是否正常:sess.running()
8、手机操作
1. 1、d.screen_on()#打开屏幕 d.screen_off()#关闭屏幕
2. 2、获取当前屏幕状态:d.info.get(' screenOn ‘)# android 4.4
3. 3、按软/硬件
4. d.press("home") #按home键,用键名
5. d.press("back") #按返回键,与主要的名称
6. d.press(0x07, 0x02) #按下键码0×07(” 0')与META ALT(0x02)
7. 这些目前支持:
8. home
9. back
10. left
11. right
12. up
13. down
14. center
15. menu
16. search 搜索
17. enter 输入
18. delete(or del)删除
19. recent(recent apps)
20. volume_up 提高音量
21. volume_down 减小音量
22. volume_mute 关闭声音
23. camera 相机
24. power
25. 4、解锁屏幕d.unlock()
9、事件:
1. 支持百分比d.long_click(0.5, 0.5)
2. 1、点击屏幕:d.click(x,y)
3. 2、长按屏幕:d.long_click(x,y)或d.long_click(x,y,0.5)#长按0.5s(默认)
4. 3、拖动:d.drag(sx,sy,ex,ey)或d.drag(sx,sy,ex,ey,0.5)#刷新0.5s(默认)
5. 4、刷卡:d.swipe(sx,sy,ex,ey)或d.swipe(sx,sy,ex,ey,0.5)#刷新0.5 秒(默认)
6. 5、滑动:#由点划动(X0,Y0)到点(X1,Y1),然后到点(X2,Y2)
7. #时间将加速0.2秒bwtween两点
8. d.swipe((X0,Y0),(X1,Y1),( x2,y2), 0.2)
10、屏幕相关:
1. 1、获取方向:d.orientation
2. 2、设置方向:d.set_orientation('l')
3. 值:natural 或者 n
4. left 或者 l
5. right 或者 r
6. upsidedown 或者 u (不能设置)
7. 3、不能选择/解冻:d.freeze_rotation()#冻结旋转 d.freeze_rotation( False)
8. 4、截屏:image = d.screenshot() #支持png和jpg
9. image.save("home.jpg")
10. 5、截屏:这个不知道什么意思
11. import cv2
12. image = d.screenshot(format='opencv')
13. cv2.imwrite('home.jpg', image)
14. 6、屏幕UI转xml:xml = d.dump_hierarchy()
15. 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. 1、选择:d(text='Clock', className='android.widget.TextView')
2. 2、获取孩子或孙子:d( className = “ android.widget.ListView ”).child( text = “ Bluetooth ”)
3. 3、兄弟姐妹:d(text="Google").sibling(className="android.widget.ImageView")
4. 4、获取子节点根据文本:d(className="android.widget.ListView", resourceId="android:id/list") \
5. .child_by_text("Bluetooth", className="android.widget.LinearLayout")
6. 5、获取子节点根据文本允许滚动搜素:d(className="android.widget.ListView", resourceId="android:id/list") \
7. .child_by_text("Bluetooth",allow_scroll_search=True,className="android.widget.LinearLayout")
8. 6、支持多级:d(className = “ android.widget.ListView ”,resourceId = “ android:id / list ”)\
9. .child_by_text(“ Wi-Fi ”,className = “ android.widget.LinearLayout ”)\
10. .child(className = “ android .widget.Switch “)\ .click
11. ()
12. 7、支持相对定位:
13. • d(A).left(B),在A的左侧选择B.
14. • d(A).right(B),在A的右侧选择B.
15. • d(A).up(B),在A之上选择B.
16. • d(A).down(B),在A下选择B.
17. d(text="Wi‑Fi").right(className="android.widget.Switch").click()
18. 8、第一个文本带有“Add new”的实例:d(text = “ Add new ”,instance = 0)
19. 9、在屏幕上查找:d(text="Add new").count
20. d(text="Add new”)[0]#第一个
21. d(text="Add new”)[1]#第二个
22. 10、显示控件信息:view.info
23. 11、检查控件是否存在:d(text = “ Settings ”).exists或者d.exists(text = “ Settings ”)
24. 12、检查控件信息:d(text = “设置”).info
25. 13、文本控件操作:
26. d(text = “ Settings ”).get_text() #获取小部件文本
27. d(text = “ Settings ”).set_text(“ My text ... ”) #设置文本
28. d(text = “ Settings ”).clear_text ) #清除文字
29. 14、点击:
30. d( text = “ Settings ”).click()
31. #等待元素出现最多10秒钟,然后点击
32. d( text = “ Settings ”).click( timeout = 10)
33. #点击的别名
34. d(文本= “设置”).tap()
35. 15、长按:d( text = “ Settings ”).long_click()
36. 16、拖动元素到另一个位子:
37. d( text = “ Settings ”).drag_to(x,y, duration = 0.5)
38. d( text = “ Settings ”).drag_to( text = “ Clock ”, duration = 0.25)#clock的中心点
39. 17、从一点到另一点的两点手势
40. d(text = “ Settings ”).gesture((sx1,sy1),(sx2,sy2),(ex1,ey1),(ex2,ey2))
41. 18、特定UI对象上的两点手势
42. • In,从边缘到中心
43. • Out,从中心到边缘
44. #从边缘到中心。这里是“以”不“在”
45. d(text="Settings").pinch_in(percent=100, steps=10)
46. #从中心到边缘
47. d(text="Settings").pinch_out()
48. 19、等待ui出现或消失
49. d(text="Settings").wait(timeout=3.0) # return bool
50. d(text="Settings").wait_gone(timeout=1.0)
51. 20、#设置默认元素等待超时(秒)
52. d.wait_timeout = 30.0
53. 21、控件不知道控件的情况下
54. d.set_fastinput_ime(True)#切换成FastInputIME输入法
55. d.send_keys(“你好123abcEFG ”)# adb广播输入
56. d.clear_text()#清除输入框所有内容(需要android-uiautomator.apk版本> = 1.0.7 )
57. d.set_fastinput_ime(False)#切换成正常的输入法
58. 22、显示:Toast
59. d.make_toast("Hello world")
60. d.make_toast("Hello world", 1.5) # show for 1.5s
child_by_description
就是找到孙子有特定描述的儿童,其他参数与之相似
child_by_text
。
child_by_instance
就是要找到在其子层次结构中指定实例的任何位置具有子
UI
元素的子元素。它在没有滚动的可见视图上执
行。
详细信息请参阅下面的链接:
•
UiScrollable
,
getChildByDescription
,
getChildByText
,
getChildByInstance
6、常见的问题:
1、发现sdk的某些功能或手机的不能用:
直接打开
uiautomator
的应用程序(
INIT
成功后,就会安装上的),点击
关 闭
UIAutomator
或者
d.service
(
“ uiautomator ”
)
.stop
()
2、502错误:
1. 控制台:adb shell am instrument -w -r -e debug false -e class com.github.uiautomator.stub.Stub \
2. com.github.uiautomator.test/android.support.test.runner.AndroidJUnitRunner
如果运行正常,启动测试之前增加一行代码
d.healthcheck()
如果报错,可能是缺少某个设备组件没有安装,使用下面的命令重新初始化
python -m uiautomator2 init --reinstall
手机
python -m uiautomator2 init
之后,浏览器输入
<device_ip
:
7912>
,会发现一个远程控制功能,延迟非常低噢。
^ _ ^