当前位置:网站首页>使用Uiautomator2进行APP自动化测试
使用Uiautomator2进行APP自动化测试
2022-08-10 14:29:00 【zljun8210】
1、 安装Uiautomator2
我的环境是Python,可用pip命令安装:
pip install uiautomator2注:Uiautomator2的源代码: GitHub - openatx/uiautomator2: Android Uiautomator2 Python Wrapper
2、 配置设备
首先设备连接到PC,并能够adb devices发现该设备。执行下面的命令会自动安装本库所需要的设备端程序:uiautomator-server,atx-agent
# init就是所有USB连接电脑的手机上都安装uiautomator2
python -m uiautomator2 init
# 指定手机安装uiautomator2, 用 --mirror
python -m uiautomator2 init --mirror --serial $SERIAL
# 嫌弃慢的话,可以用国内的镜像
python -m uiautomator2 init --mirror
注意:据说最新版的uiautomator2已不需手动配置,当代码运行 uiautomator2.connect()时,会先配置设备端。
3、 写代码之前:识别元素
在Andorid SDK里有monitor.bat,运行它,会启动uiautomatorviewer,这是SDK自带的工具。
也可以使用第三方工具,这里推荐 weditor,安装如下:
pip install weditor启动Weditor:
python -m weditor浏览器会自动启动一个网页 http://atx.open.netease.com,看到如下界面

设置adb device看到的设备进行连接。
4、 正式写代码
import unittest
import uiautomator2 as u2
import time
class AppTest(unittest.TestCase):
def EditWO(self):
d = u2.connect()
d.app_start('com.xxxxxx.xxxxxx')
time.sleep(5)
d(text='Work Order').click() # 点击Work order菜单
time.sleep(3)
d(text='Completed').click() # 点击Completed
time.sleep(2)
d(text='Open').click()
time.sleep(2)
d(text='300').click()
time.sleep(1)
d(text='Save').click()
d.press('back')
def Mapview(self):
d = u2.connect()
d.app_start('com.xxxxxx.xxxxxx')
time.sleep(5)
d.xpath(
'//android.widget.RelativeLayout/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]').set_text('char')
d.press('enter')
d(text='CHARIOTOFFIRE').click()
d(text='Cancel').click()
d.xpath(
'//android.widget.RelativeLayout/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[5]/android.widget.ImageView[1]').click()
d(text='+ ADD LOCATIONS').click()
d.xpath(
'//androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.Button[3]').click()
d.click(0.503, 0.498)
d.xpath(
'//androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.widget.Button[3]').click()
d.click(0.081, 0.801)
d.xpath(
'//androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.widget.Button[3]').click()
d.click(0.914, 0.273)
d(text='GET DIRECTIONS').click()
d.press("back")
def test_something(self):
self.EditWO()
if __name__ == '__main__':
unittest.main()边栏推荐
- MySQL - 数据库的存储引擎
- awk的简单使用
- Using data intelligence, Amazon cloud technology helps companies build endogenous brand growth
- 开源SPL消灭数以万计的数据库中间表
- file system design
- 作业
- High-paid programmers & interview questions series 135 How do you understand distributed?Do you know CAP theory?
- 串口服务器调试助手使用教程,串口调试助手使用教程【操作方式】
- 从洞察到决策,一文解读标签画像体系建设方法论
- MQTT服务器搭建
猜你喜欢
随机推荐
vivado闪退或者message无显示
舵机内部结及工作原理浅析[通俗易懂]
镜像瘦身:每一层都不能放过
八大排序总是忘?快来这里~
Circle 创始人回应美财政部禁止 Tornado :隐私与安全之间关系紧张
“Oracle 封禁了我的账户”
电脑重装系统提示activex部件不能创建对象如何解决
正则表达式(包含各种括号,echo,正则三剑客以及各种正则工具)
【MinIO】工具类使用
data product manager
Do not access Object.prototype method ‘hasOwnProperty‘ from target object....
波士顿房价预测
强意识 压责任 安全培训筑牢生产屏障
1004 (tree array + offline operation + discretization)
BCG库简介
领域驱动实践总结(基本理论总结与分析V+架构分析与代码设计+具体应用设计分析)
2022-08-10日报: Swin Transformer作者曹越加入智源,开展视觉基础模型研究
Data product manager thing 2
什么?你还不会JVM调优?
缺少比较器,运放来救场!(运放当做比较器电路记录)









