python+uiamatormation2常用操作

1、安装:

pip install --pre uiautomator2
#或者你可以直接从github上源码安装
git clone https://github.com/openatx/uiautomator2
pip install -e uiautomator2
pip install pillow #截屏工具
2、初始化手机,需要的环境SDK

python -m uiautomator2 init
这个只有初始化后才可以用
pip install --pre --upgrade weditor#安装自动化UI定位
python -m weditor#启动
自动安装本库所需的设备端程序:uiautomator-server ,atx-agent ,openstf / minicap ,openstf / minitouch  可单独下载安装

配置工作完成

3、打开手机端口:adb forward tcp:7912 tcp:7912 

     连接手机:device_ip = 127.0.0.1 

    这个不知道干嘛的忘记了好像是是自行安装atx-agent的时候用的

4、常用命令:

1、安装apk:python -m uiautomator2 install $ device_ip https://example.org/some.apk
2、清缓存:python -m uiautomator2
3、停止所有应用程序:python -m uiautomator2 app-stop-all $ device_ip
4、截图:python -m uiautomator2截图$ device_ip screenshot.jpg
5、检查守护线程:d.healthcheck()
6、打开调试:d.debug = true
7、获取连接信息:d.info
8、shell命令:d.adb_shell('pwd')
9、分辨率:d.window_size()
10、查看当前应用信息:d.current_app()
11、查看序列号:d.serial
5、快速开始:

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

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()

 #等待元素出现最多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 之后,浏览器输入<device_ip :7912> ,会有一个远程控制功能

转载于:https://www.cnblogs.com/zhujunsheng/p/10364278.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值