uiautomator2 常用命令

原文来自:https://github.com/openatx/uiautomator2/blob/master/QUICK_REFERENCE.md
import uiautomator2 as u2

d = u2.connect(“–serial-here–”) # 只有一个设备也可以省略参数
d = u2.connect() # 一个设备时
d = u2.connect(“10.1.2.3”) # 通过设备的IP连接(需要在同一局域网且设备上的atx-agent已经安装并启动)

d.app_current() # 获取前台应用 packageName, activity
d.app_start(“com.example.app”) # 启动应用
d.app_start(“com.example.app”, stop=True) # 启动应用前停止应用
d.app_stop(“com.example.app”) # 停止应用

app = d.session(“com.example.app”) # 启动应用并获取session

session的用途是操作的同时监控应用是否闪退,当闪退时操作,会抛出SessionBrokenError

app.click(10, 20) # 坐标点击

无session状态下操作

message_a=d.toast.get_message() #获取toast
d.click(10, 20) # 坐标点击
d.swipe(10, 20, 80, 90) # 从(10, 20)滑动到(80, 90)
d.swipe_ext(“right”) # 整个屏幕右滑动
d.swipe_ext(“right”, scale=0.9) # 屏幕右滑,滑动距离为屏幕宽度的90%

d.press(“back”) # 模拟点击返回键
d.press(“home”) # 模拟Home键

d.send_keys(“hello world”) # 模拟输入,需要光标已经在输入框中才可以
d.clear_text() # 清空输入框

执行shell命令

output, exit_code = d.shell(“ps -A”, timeout=60) # 执行shell命令,获取输出和exitCode
output = d.shell(“pwd”).output # 这样也可以
exit_code = d.shell(“pwd”).exit_code # 这样也可以

元素操作

d.xpath(“立即开户”).wait() # 等待元素,最长等10s(默认)
d.xpath(“立即开户”).wait(timeout=10) # 修改默认等待时间

常用配置

d.settings[‘wait_timeout’] = 20 # 控件查找默认等待时间(默认20s)

xpath操作

d.xpath(“立即开户”).click() # 包含查找等待+点击操作,匹配text或者description等于立即开户的按钮
d.xpath(“//*[@text=‘私人FM’]/…/android.widget.ImageView”).click()

d.xpath(‘//*[@text=“私人FM”]’).get().info # 获取控件信息

for el in d.xpath(‘//android.widget.EditText’).all():
print(“rect:”, el.rect) # output tuple: (left_x, top_y, width, height)
print(“bounds:”, el.bounds) # output tuple: (left, top, right, bottom)
print(“center:”, el.center())
el.click() # click operation
print(el.elem) # 输出lxml解析出来的Node

监控弹窗(在线程中监控)

d.watcher.when(“跳过”).click()
d.watcher.start()

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值