python ui测试 手机界面控件不可见时_python ui测试 手机界面控件不可见时_uiautomator2小笔记...

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]#第二个

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 之后,浏览器输入 ,会有一个远程控制功能


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部