当前位置:网站首页>使用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()
边栏推荐
- Classifying irises using decision trees
- PAT甲级 1014 排队等候(队列大模拟+格式化时间)
- 电脑重装系统提示activex部件不能创建对象如何解决
- 重要通知 | “移动云杯”算力网络应用创新大赛初赛延期!!
- 关于已拦截跨源请求CORS 头缺少 ‘Access-Control-Allow-Origin‘问题解决
- Makefile missing separator. Stop.怎么解决「建议收藏」
- 学习MySQL 临时表
- 正则表达式(包含各种括号,echo,正则三剑客以及各种正则工具)
- “Oracle 封禁了我的账户”
- Circle 创始人回应美财政部禁止 Tornado :隐私与安全之间关系紧张
猜你喜欢
【Gazebo入门教程】第三讲 SDF文件的静/动态编程建模
AWS 安全基础知识
字节终面:CPU 是如何读写内存的?
Using data intelligence, Amazon cloud technology helps companies build endogenous brand growth
系统架构系列文章三--解决传统企业核心系统的性能问题
写不完的数学试卷-----试卷生成器(Qt含源码)
开源SPL消灭数以万计的数据库中间表
使用mysq语句操作数据库
[JS Advanced] Creating sub-objects and replacing this_10 in ES5 standard specification
微信小程序,自定义输入框与导航胶囊对其
随机推荐
借数据智能,亚马逊云科技助力企业打造品牌内生增长力
司空见惯 - 股市狠狠下跌后,何時能反弹?
antd组件中a-modal设置固定高度,内容滚动显示
Steam教育在新时代中综合学习论
DB2查询2个时间段之间的所有月份,DB2查询2个时间段之间的所有日期
数据产品经理那点事儿 一
重要通知 | “移动云杯”算力网络应用创新大赛初赛延期!!
中学数学建模书籍及相关的视频等(2022.08.09)
产品使用说明书小程序开发制作说明
EVE模拟器的使用-带图超详细(学网络用)「建议收藏」
Flask框架——MongoEngine使用MongoDB数据库
正则表达式(包含各种括号,echo,正则三剑客以及各种正则工具)
file system design
第三方软件测评有什么作用?权威软件检测机构推荐
Data product manager thing 2
面试面到了一个腾讯30k出来的,有见识到何为精通MySQL调优
力扣解法汇总640-求解方程
Parallels 将扩展桌面平台产品,以进一步改善在 Mac 上运行 Windows 的用户体验和工作效率
指针(C语言初解)
SecureCRTPortable – 破解