当前位置:网站首页>使用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()边栏推荐
猜你喜欢

Cloud Migration Practice of Redis

学习MySQL 临时表

Lack of comparators, op amps come to the rescue!(Op amp is recorded as a comparator circuit)

Error: Rule can only have one resource source (provided resource and test + include + exclude)

Flask框架——基于Celery的后台任务

WSL 提示音关闭

池化技术有多牛?来,告诉你阿里的Druid为啥如此牛逼!

Send a post request at the front desk can't get the data

如何完成新媒体产品策划?

波士顿房价预测
随机推荐
PyTorch multi-machine multi-card training: DDP combat and skills
“Oracle 封禁了我的账户”
epoll学习:思考一种高性能的服务器处理框架
一种能让大型数据聚类快2000倍的方法,真不戳
PHP 判断文件是否有内容,没有内容则复制另一个文件写入
tampercfg内核模块导致机器频繁crash
C#实现访问OPC UA服务器
一汽奥迪:持续34年聚焦品质与体验 立足市场需求推进产品迭代
sql语句 异常 Err] 1064 – You have an error in your SQL syntax; check the manual that corresponds to your
每个月工资表在数据库如何存储?求一个设计思路
2022年中国软饮料市场洞察
【MinIO】Using tools
基于ArcGIS水文分析、HEC-RAS模拟技术在洪水危险性及风险评估
符合信创要求的堡垒机有哪些?支持哪些系统?
注意力模型---Attention Model
MQTT服务器搭建
leetcode 739. Daily Temperatures 每日温度(中等)
Flask框架——MongoEngine使用MongoDB数据库
vivado闪退或者message无显示
awk的简单使用