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

2022年网络安全培训火了,缺口达95%,揭开网络安全岗位神秘面纱

八大排序总是忘?快来这里~

laravel throws the error to Dingding

PyTorch 多机多卡训练:DDP 实战与技巧

List集合

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

第五讲 测试技术与用例设计

开源SPL消灭数以万计的数据库中间表
![[Gazebo Introductory Tutorial] Lecture 3 Static/Dynamic Programming Modeling of SDF Files](/img/ec/87566763daf7faa3769ee186f93ee0.jpg)
[Gazebo Introductory Tutorial] Lecture 3 Static/Dynamic Programming Modeling of SDF Files

重要通知 | “移动云杯”算力网络应用创新大赛初赛延期!!
随机推荐
《论文阅读》PLATO: Pre-trained Dialogue Generation Model with Discrete Latent Variable
recursive recursive function
A method that can make large data clustering 2000 times faster
线上线下课程教学培训小程序开发制作功能介绍
第五讲 测试技术与用例设计
SQL学习(基础)
1W字详解线程本地存储 ThreadLocal
正则表达式(包含各种括号,echo,正则三剑客以及各种正则工具)
黑客入门,从HTB开始
写不完的数学试卷-----试卷生成器(Qt含源码)
借数据智能,亚马逊云科技助力企业打造品牌内生增长力
The a-modal in the antd component is set to a fixed height, and the content is scrolled and displayed
C#实现访问OPC UA服务器
Flask框架——基于Celery的后台任务
【有限元分析】异型密封圈计算泄漏量与参数化优化过程(带分析源文件)
简单的写一个防抖跟节流
面试面到了一个腾讯30k出来的,有见识到何为精通MySQL调优
ES5和SE6来实现一个Promise效果
每个月工资表在数据库如何存储?求一个设计思路
2012年下半年 系统架构设计师 下午试卷 II