推荐一款超好用的ui自动化工具--uiautomator2

发布时间:2025-12-09 16:54:46 浏览次数:3

给大家推荐一款超级好用的ui自动化工具 uiautomator2

 

先上目录

  • 简要介绍
  • 实现原理
  • 安装步骤
  • 使用简介
  • 优点缺点
  •  

     

    简要介绍

    项目地址:https://github.com/openatx/uiautomator2

    start:2k

    一看名字,大家可能会觉得uiautormator2是Uiautomator的升级版本,其实并不是。

    大家都知道,UiAutomator是Google提供的用来做安卓自动化测试的一个Java库,功能强大,但支持Java语言,并不支持python。

    然而,当今测试行业,最流行的编程语言,还是python。ui自动化,怎么能不支持python呢?

    所以 uiautormator2 应运而生。

    uiautomator2 是一个开源的自动化测试工具,提供了一系列的python API,方便测试人员用python编写自动化测试脚本。

     

    其他与 uiautomator2 相关的工具

    weditor:

    简介:抓取ui控件的工具,并可进行简单调试

    项目地址:https://github.com/openatx/weditor

     

    atxserver2:

    简介:机型管理平台,可以实现多台手机群控

    项目地址:https://github.com/openatx/atxserver2

     

    ATX-Test:

    简介:UI自动化框架,PO模型,方便管理ui自动化项目代码

    项目地址:https://github.com/pengchenglin/ATX-Test

     

    实现原理

    原理是在手机上运行了一个http rpc服务,将Uiautomator中的功能开放出来,然后再将这些http接口封装成Python库。

     

    原理图:

    如图所示,uiautomator2主要分为两个部分,python客户端,移动设备

    • python端: 运行脚本,并向移动设备发送HTTP请求

    • 移动设备:移动设备上运行了封装了uiautomator2的HTTP服务,解析收到的请求,并转化成uiautomator2的代码。

    整个过程

  • 在移动设备上安装atx-agent(守护进程), 随后atx-agent启动uiautomator2服务(默认7912端口)进行监听

  • 在PC上编写测试脚本并执行(相当于发送HTTP请求到移动设备的server端)

  • 移动设备通过WIFI或USB接收到PC上发来的HTTP请求,执行制定的操作

     

  • 安装步骤

    安装前置步骤

  • python(推荐使用python3)

  • adb环境

  • Android手机(iOS好像也支持了,具体可以看作者的 github 项目)

  •  

    安装步骤

    (1)pip 安装 uiautomator2

    pip install uiautomator2

    (2)pip 安装 weditor

    pip install weditor

    (3)手机连电脑,并安装atx-agent

    python -m uiautomator2 init

     

    只需这三个步骤,即可完成安装。

     

    使用简介

    使用起来也特别方便。

    (1)首先,启动 weditor,连接手机,并抓取元素控件

    python -m weditor

    命令输入之后,会弹出一个浏览器web窗口,输入手机 DeviceId 即可连接weditor。

    点击左侧的手机截屏,即可选择控件,并会生成控件元素代码。

     

    (2)编写 python 自动化测试 脚本

    连接设备

    通过手机 IP 地址连接

    import uiautomator2 as u2d = u2.connect('10.0.0.1')print(d.info)

    也可以通过手机 DeviceId 连接

    import uiautomator2 as u2d = u2.connect('123456f')print(d.info)

     

    安装待测apk

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

     

    选择待测的app

    d.app_start("com.example.hello_world")

     

    使用封装好的API,触发事件,操控手机

    ​​​​​​​# Selector 元素选择器d(text="Settings")# 点击文案是“Settings”的元素d(text="Settings").click()# 点击屏幕的(x,y)点坐标d.click(x, y)  # 选择元素并滑动d(text="Settings").swipe("right")d(text="Settings").swipe("left", steps=10)d(text="Settings").swipe("up", steps=20) # 1 steps is about 5ms, so 20 steps is about 0.1sd(text="Settings").swipe("down", steps=20)# 滑动屏幕d.swipe(sx, sy, ex, ey)d.swipe(sx, sy, ex, ey, 0.5) # swipe for 0.5s(default)# 等待“Settings”元素出现之后再点击,设置超时时间是3秒d(text="Settings").wait(timeout=3.0).click()

     

    元素选择器(Selector)支持以下方式获取控件

    text、resourceId、ClassName、Xpath等

     

    支持多种方式联合定位元素,提高准确性

    d(text="Settings", resourceId="com.xxx.yyy")

     

    支持的操作事件

    click、Double click、Long click、Swipe、Drag、scroll 等等,

     

    更多详情可以查看 github 的说明文档

     

    uiautomator2还有一个比较好的功能是监听控件。原理是轮询查找控件,并且会有超时时间,当捕获到相关控件时,就可以采取自定义的处理。

    # 常用写法,注册匿名监控d.watcher.when("安装").click() # 注册名为ANR的监控,当出现ANR和Force Close时,点击Force Closed.watcher("ANR").when(xpath="ANR").when("Force Close").click() # 其他回调例子d.watcher.when("抢红包").press("back")d.watcher.when("//*[@text = 'Out of memory']").call(lambda d: d.shell('am force-stop com.im.qq'))

     

    监听控件常常用于,跳过APP的弹窗广告

    uiautomator2还可以让手机弹出toast

    d.toast.show("Hello world")d.toast.show("Hello world", 1.0) # show for 1.0s, default 1.0s

     

    使用方法就简单介绍到这,感兴趣的同学可以到作者的github项目上去了解更多内容。

     

    优点缺点

    没有对比就没有伤害。现在大家都潜意识把Appium当成是Ui自动化的行业标杆,那我们就来一起对比一下,uiautomator2和Appium。

     

    不知道大家怎么看,对于一个pythoner来说,uiautomator2显然更好用一些。

     

     

    但是,uiautomator2也是有一些缺陷的。

  • 无线连接手机的时候,稳定性不太好,经常会掉线。

  • 监听控件,d.watchers.watched 置换监听状态会无效,不过查看文档,作者是已经把这块功能移除了。

  •  

    关于作者

    github:https://github.com/GitDzreal93/dev-tester
    微信公众号:测试开发guide

    需要做网站?需要网络推广?欢迎咨询客户经理 13272073477