当前位置:网站首页>使用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()
边栏推荐
猜你喜欢
随机推荐
[JS Advanced] Creating sub-objects and replacing this_10 in ES5 standard specification
使用mysq语句操作数据库
重要通知 | “移动云杯”算力网络应用创新大赛初赛延期!!
How is the monthly salary table stored in the database?Ask for a design idea
实现一个深克隆
Error: Rule can only have one resource source (provided resource and test + include + exclude)
NAACL 2022 | 简单且高效!随机中间层映射指导的知识蒸馏方法
Using data intelligence, Amazon cloud technology helps companies build endogenous brand growth
SecureCRTPortable – 破解
普林斯顿微积分读本05第四章--求解多项式的极限问题
第五讲 测试技术与用例设计
指针(C语言初解)
How to code like a pro in 2022 and avoid If-Else
leetcode 739. Daily Temperatures 每日温度(中等)
【219】慕课三千多的那个go工程师的培训课笔记 02 go语言的编程思想
【MinIO】工具类使用
PEST 分析法
从全球价值链视角看,京东云数智供应链对未来经济有何影响?
List集合
epoll学习:思考一种高性能的服务器处理框架