uiautomator2 笔记
uiautomator2 笔记
系统操作
1、安装app
d.app_install('http://some-domain.com/some.apk') # app的url
2、启动app
d.app_start('com.example.hello_world') # package
3、停止应用
d.app_stop('com.example.hello_world') d.app_clear('com.example.hello_world')
4、停止所有应用
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将会出现
应用连接会话
启动应用
sess = d.session(“com.netease.cloudmusic”)
会话连接运行中的程序
sess = d.session(“com.netease.cloudmusic”)
检测应否崩溃:
sess = d(text= “音乐”).click()
检查会话是否正常
sess.running()
手机硬件操作
d.screen_on() #打开屏幕 d.screen_off() #关闭屏幕 d.info.get('screenOn') #获取当前屏幕状态 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
事件
1、点击屏幕
d.click(X,Y) # 支持百分比, 如 d.click(0.5,0.5)
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、滑动
d.swipe((X0,Y0),(X1,Y1),(X2,Y2),0.2)#由点划动(X0,Y0)到点(X1,Y1),然后到点(X2,Y2),时间将加速0.2秒bwtween两点
屏幕相关
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()
选择器
识别屏幕对象,请参阅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(text =“谷歌”).sibling(className=“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).right(B) #在A的右侧选择B. d(A).up(B) #在A之上选择B. d(A).down(B) #在A下选择B. d(text= “无线网络连接”).right(className =“android.widget.Switch”).click()
8、第一个文本带有“Add new”的实例
d(text =“Add new”,instance = 0)
9、在屏幕上查找
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()
d(text =“设置”).click(超时时间= 10) #等待元素出现最多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,显示toast
d.make_toast(“Hello world”)
d.make_toast(“Hello world”,1.5) #显示1.5秒
一些复杂屏幕操作
1.等待对象显示并点击,等待最多10s:
d(text="Settings").click(timeout=10)
2.当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 ='测试')

浙公网安备 33010602011771号