Python库-uiautomator2(app自动化)

准备:

pip安装uiautomator2包

安装adb,配置环境路径

cmd输入adb检查是否安装完毕
cmd输入adb devices检查已连接设备

手机连接:

usb

1.打开开发者模式-usb调试
如果出现unauthorized,重复步骤1
2.python代码:填手机序列号
device=u2.connect('xxxxxxxxxxxxxxxx')

wifi

python代码:填IP地址
device=u2.connect('http://192.168.xxx.xxx)

函数语法

#调用uiautomator2包并重命名
import uiautomator2 as u2  
device=u2.connect('序列号')  


#打印设备信息
print(device.device_info)  
#点击进入app  
##打印当前app名称包名  
print(device.app_current())  
#打印所有包名  
print(device.app_list_running())  
#打开app  
device.app_start('com.tencent.mm')  
#关闭app
device.app_stop('com.tencent.mm')

基本信息显示

设备信息

#打印设备信息  
print(device.infoo)  
#较为详细设备信息
print(device.device_info)
#获取屏幕大小(括号内可设置文件名)  
print(device.window_size())

app信息

##打印当前app名称包名  
print(device.app_current())  
#打印所有包名  
print(device.app_list_running())

按键操作

解锁屏幕

device.unlock()

点亮关闭屏幕

device.screen_on()
device.screen_off()

获取屏幕开关状态

device.info.get('screenOn')

按按键

device.press()
#括号内输入<“按键名”>或<按键对应代码>

当前支持按键:
- home
- back
- left
- right
- up
- down
- center
- menu
- search
- enter
- delete ( or del)
- recent (recent apps)
- volume_up
- volume_down
- volume_mute
- camera
- power

按键代码定义详见:KeyEvent | Android Developers

手势交互

点击操作

device.click(x, y)

支持:

  • 元素点击(定位元素,再点击)
  • 像素点点击
  • 百分比点击

长按屏幕

device.long_click(x, y) 
device.long_click(x, y, 0.5) # long click 0.5s (default)

滑动操作

device.swipe(startx,starty,endx,endy)
device.swipe_ext('方向',scale=百分比)
#滑动元素
element.swipe('方向',steps=滑动时间)

拖动

device.drag(sx, sy, ex, ey) 
device.drag(sx, sy, ex, ey, 0.5) # swipe for 0.5s(default)

方向与旋转

识别方向

可用的方向有:
natural 或 n
left 或l
right 或r
upsidedown 或u (不可设置)

# 检索方向,可以是"natural" 或"left" 或"right" 或"upsidedown"
orientation = d.orientation 
# 警告:未在我的TT-M1通过测试 
# 设定orientation(方向) 和 冻结旋转. 
d.set_orientation("n") # 或"natural" 
d.set_orientation("l") # 或"left" 
d.set_orientation("r") # 或"right" 
d.set_orientation("n") # or "natural"

冻结/解冻旋转

# 冻结旋转 
d.freeze_rotation() 
# 取消冻结旋转 
d.freeze_rotation(False)

输入

定位输入框元素,输入/清空

#定位输入框元素
element=device(resourceId="**********")
#输入内容
element.send_keys('内容')
#清空内容
element.clear_text()

截屏

device.screenshot('文件名')进行截屏,之后可以用pillow库、cv2库对图片进行进一步处理

pillow库与cv2库简述:
pillow库:Python图像处理库,它能够实现基本的图像操作,如图像打开、保存、缩放、裁剪、旋转、滤波等。Pillow支持多种图像格式,包括JPEG、PNG、BMP、GIF、TIFF等,并且可以在这些格式之间进行转换。
cv2库:开源计算机视觉库,它提供了许多用于处理图像和视频的函数和算法。cv2是基于C++开发的,但也提供了Python接口。它可以实现许多功能,如图像处理、特征检测、人脸识别、目标跟踪、机器学习等。

#截屏
device.screenshot('文件名')
#保存截图后可进一步操作
##保存截图
im=device.screenshot()
im.save('imgname.img')
##加滤镜:模糊
im2=im.filter(ImageFilter.BLUR)
im2.save("文件名2")
##重定大小存储
im3=im.resize((x,y))
im3.save("文件名3")

进一步操作详见pillow库与cv2库说明手册

#例:
# 获取PIL.Image 格式,需要先安装pillow 
image = device.screenshot() 
image.save("home.jpg") # 或 home.png 

# 获取opencv 格式, 需要先安装numpy 和cv2 
import cv2 image = device.screenshot(format='opencv') cv2.imwrite('home.jpg', image)

文件操作

推送

#推送到一个文件夹 
d.push("foo.txt", "/sdcard/") 
# 推送并重命名 
d.push("foo.txt", "/sdcard/bar.txt") 
# 推送fileobj 
with open("foo.txt", 'rb') as f: d.push(f, "/sdcard/") 
# 推送并修改文件模式 
d.push("foo.sh", "/data/local/tmp/", mode=0o755)

获取

d.pull("/sdcard/tmp.txt", "tmp.txt") 
# 设备中没有文件会引发FileNotFoundErr 
d.pull("/sdcard/some-file-not-exists.txt", "tmp.txt")

APP操作

安装

目前仅支持从url安装。

d.app_install(' HTTP://some-domain.com/some.apk ')

启动

以package name启动

d.app_start("com.example.hello_world") # 以package name启动

停止与清除

# 执行强制停止 
d.app_stop("com.example.hello_world") 
# 执行应用清除 
d.app_clear('com.example.hello_world')

停止所有APP的运行

#停止所有 
d.app_stop_all() 
#停止除com.examples.demo以外的所有应用程序 
d.app_stop_all(excludes=['com.examples.demo'])

元素等待

程序很快,app很慢

强制等待

猜测加载时间

time.sleep(时间)

智能等待

什么时候加载出来什么时候运行
超时时间:默认20s 。可用进行全局设置,也可在每条命令后设置单独的等待时间。

device.wait_timeout=时间
device.implicitly_wait()
#等待页面加载完成
device.wait_activity()
#等待元素出现 返回true/false
device().wait()
device(text='demo').wait()
#等待元素消失
device().wait_gone()
#等待元素是否存在
device().exist()
#等待点击

元素定位

元素定位辅助工具weditor

其他辅助工具:uiaotomatorviewer、appium
weditor优势:可以自动生成uiautomator2代码、可以同时管理多个设备、直接在调试页面操作手机

安装:

pip install weditor
出错解决:在cmd输入pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ weditor
或者下载weditor-plus包

使用:

打开终端输入weditor,自动打开页面

元素构成

详情可看xml代码语法

可通过相关语法获取儿孙和同级UI对象

待完善:元素定位,可滚动元素、儿孙同级对象、相对位置元素;触发器;输入法;toast消息

参考资料:
uiautomator2-快速入门app自动化测试
uiautomator2 库中文手册

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值