uiautomator2使用教程

uiautomator2使用教程

 

一、简单介绍

1、自动化测试开源工具,仅支持Android平台的原生应用测试
2、目前仅支持脚本语言python
3、封装谷歌自带的uiautomator测试框架,提供便利的python接口

google提供的uiautomator库可获取安卓APP的控件属性,并执行相关操作,但有两个缺点:  
测试脚本只能使用Java语言
测试脚本必须每次被上传到设备上运行

4、工作原理:
原理是在手机上运行了一个http服务器,将uiautomator中的功能开放出来,然后再将这些http接口,封装成Python库

 

以下解释直接引用网址:https://testerhome.com/topics/11357
① 在移动设备上安装atx-agent(守护进程), 随后atx-agent启动uiautomator2服务(默认7912端口)进行监听
② 在PC上编写测试脚本并执行(相当于发送HTTP请求到移动设备的server端)
③ 移动设备通过WIFI或USB接收到PC上发来的HTTP请求,执行制定的操作

二、实现功能

1、获取手机部分信息(d.info)

 

2、操作手机设备,比如点击Home键、锁屏、查看快捷键等(d.press("home")、d.screen_off()、d.open_quick_settings()等)

 

3、安装、启动、卸载应用
d.app_install('http://some-domain.com/some.apk')
d.app_start("com.example.xxx")
d.app_stop("com.example.xxx")

 

4、识别手机上控件、对控件进行相关操作等
d(text='Clock', className='android.widget.TextView')

 

参数可支持以下:
● text, textContains, textMatches, textStartsWith
● className, classNameMatches
● description, descriptionContains, descriptionMatches, descriptionStartsWith
● checkable, checked, clickable, longClickable
● scrollable, enabled,focusable, focused, selected
● packageName, packageNameMatches
● resourceId, resourceIdMatches
● index, instance

一、要求
python 3.6+
android 4.4+
 
二、介绍

uiautomator2 是一个可以使用Python对Android设备进行UI自动化的库。其底层基于Google uiautomator,Google提供的uiautomator库可以获取屏幕上任意一个APP的任意一个控件属性,并对其进行任意操作。

三、地址
 
四、安装
1、安装uiautomator2
pip install --pre uiautomator2
pip install pillow
 
2、初始化

部署相关的守护进程。

电脑连接上一个手机或多个手机, 确保adb已经添加到环境变量中,执行下面的命令会自动安装本库所需要的设备端程序:uiautomator-server 、atx-agent、openstf/minicap、openstf/minitouch

python -m uiautomator2 init

安装完成,设备上会多一个uiautomator的应用。

配置手机设备参数:

有两种方法,一种是通过WIFI,另一种是通过USB数据线将手机链接电脑。

WiFi连接更方便一点,需要保持PC和手机使用的一个WIFI,查看手机连接WIFI的IP地址。

3、测试

import uiautomator2 as u2

d = u2.connect('127.0.0.1::6555')

print(d.info)

打印结果:

{'currentPackageName': 'com.android.launcher', 'displayHeight': 1280, 'displayRotation': 1, 'displaySizeDpX': 360, 'displaySizeDpY': 640, 'displayWidth': 720, 'productName': 'DUK-AL20', 'screenOn': True, 'sdkInt': 23, 'naturalOrientation': False}

五、元素定位

1、查看app控件

我们可以借助Android SDK自的uiautomatorviewer查看元素,这就要求手机必须以USB的方式连接PC,我前面使用的是WIFI连接进行连接的。所以,openatx提供了另外一个工具weditor 来解决这个问题。

GitHub地址:https://github.com/openatx/weditor

(1)、安装:

pip install --pre --upgrade weditor
2)、使用
python3 -m weditor
3)、工具打开
默认会通过浏览器打开页面:http://atx.open.netease.com/
(4)工具的操作步骤
选择android、输入手机或者模拟器的ip+端口,点击connect
dump hierarchy是用来刷新页面的
鼠标点击想要的元素,就可以查看他们的控件了
 
 
2、主要语法
 
(1)启动app
d.app_start("com.addcn.android.house591")

(2)关闭app
cls.d.app_stop("com.addcn.android.house591")

(3)ResourceId定位
cls.d(resourceId="com.addcn.android.house591:id/ad_banner").click()

(4)Text定位
d(text="精选").click()

(5)Description定位
d(description="..").click()

(6)ClassName定位
d(className="android.widget.TextView").click()

(7)xpath定位
d.xpath("//*[@content-desc='分享']").click()

(8)


3、其他操作
1#组默认元素等待超时(秒)
cls.d.wait_timeout = 20.0 #默认20

(2)元素拖拽
(3)开关点击
  • d(A).left(B), selects B on the left side of A.
  • d(A).right(B), selects B on the right side of A.
  • d(A).up(B), selects B above A.
  • d(A).down(B), selects B under A.
  • 例如:
    #选择“Wi-Fi”右侧的“开关” 
    d(text="Wi‑Fi").right(className="android.widget.Switch").click()


(4)获取/统计某个相同条件的数目
d(text="Add new").count
或者
len(d(text="Add new"))
得知数目之后,我们可以通过索引去定位
d(text="Add new")[0]
d(text="Add new")[1]
也可以遍历
 
for view in d(text="Add new"):
    view.info 

(5)截图
#截取屏幕截图并保存到计算机上的文件中,要求Android> = 4.2d.screenshot( “ home.jpg ”) # get PIL.Image格式化图像。当然,你需要首先安装pillow  
image = d.screenshot() # default format =“pillow” image.save( “ home.jpg ”)#或home.png。目前,只有PNGJPG支持

#得到OpenCV的格式图像。当然,你需要先安装numpy和cv2
import cv2
image = d.screenshot( format = ' opencv') cv2.imwrite( ' home.jpg '图像)#获取原始JPEG数据 imagebin = d.screenshot(格式= '原始') 打开( “ some.jpg ”, “ WB ”).WRITE(imagebin)

(6)手势操作
1、单击
d( text = “ Settings ”).click()

2、长按
d( text = “ Settings ”).long_click()


3、将对象拖向另一个点或另一个UI对象
#笔记:拖不能用于为Android <4.3。
#将UI对象拖动到屏幕点(xy),0.5秒后 
dtext = “设置”).drag_to(x,y, duration = 0.5)
#将UI对象拖动到另一个(中心位置) UI对象,在0.25dtext = “设置”).drag_to( text =  Clock ”, duration = 0.25)

4、在屏幕上滑动
# swipe from (sx, sy) to (ex, ey)
d.swipe(sx, sy, ex, ey)
# swipe from (sx, sy) to (ex, ey) with 10 steps
d.swipe(sx, sy, ex, ey, steps=10)


5、在屏幕上拖拽
# drag from (sx, sy) to (ex, ey)
d.drag(sx, sy, ex, ey)
# drag from (sx, sy) to (ex, ey) with 10 steps
d.drag(sx, sy, ex, ey, steps=10)




(7)获取对象信息和状态
1、
d(text="Settings").exists 
#如果存在则为True,否则为假
or d.exists(text="Settings") # 进一步使用 d(text="Settings").exists(timeout=3) 
# 等待设置出现在3S,相同.wait(3)

2、检索特定UI对象的信息
d(text="Settings").info

3、获取/设置/清除可编辑字段的文本(例如,EditText小部件)
d(text = “ Settings ”).get_text()   # get widget text 
d(text = “ Settings ”).set_text(“ My text ... ”)   #设置文本 
d(text = “ Settings ”).clear_text( )   #清除文字、


(8)系统常用按键
# press home key
d.press.home()
# press back key
d.press.back()
# the normal way to press back key
d.press("back")----亲测可用
# press keycode 0x07('0') with META ALT(0x02) on
d.press(0x07, 0x02)

  • home                   #手机Home键
  • back                   #手机返回键
  • left                   #对应键盘上的向右键<-
  • right                  #对应键盘上的向右键->
  • up                    #对应键盘上的向上键
  • down                   #对应键盘上的向下键
  • center                  #选中?
  • menu                   #菜单
  • search                  #查找?
  • enter                  #对应键盘上的Enter键
  • delete(or del)                          #对应键盘上的DEL键 用于删除
  • recent(recent apps)                  #任务切换
  • volume_up                #声音向上调整
  • volume_down               #声音向下调整
  • volume_mute               #静音按键
  • camera                  #拍照
  • power                   #电源键



一、Uiautomator2原理介绍

1.uiautomator2是一个可以使用Python对Android设备进行UI自动化的库。其底层基于Google uiautomator,Google提供的uiautomator库可以获取屏幕上任意一个APP的任意一个控件属性,并对其进行任意操作,目前仅支持android平台的原生应用测试,https://github.com/openatx/uiautomator2。但有两个缺点:

    测试脚本只能使用Java语言。
    测试脚本必须每次被上传到设备上运行。

2.工作原理

分为两个部分:

PC上的python端:运行脚本,并向系统设备发送http请求

移动设备:移动设备上运行了封装了uiautomator2的HTTP服务,解析收到的请求,并转化成uiautomator2的代码。

二、安装uiautomator2

pip install --pre uiautomator2

pip install pillow

三、初始化

部署相关的守护进程。

电脑连接上一个手机或多个手机, 确保adb已经添加到环境变量中,执行下面的命令会自动安装本库所需要的设备端程序:uiautomator-server 、atx-agent、openstf/minicap、openstf/minitouch

1
python -m uiautomator2 init

安装完成,设备上会多一个uiautomator的应用。

配置手机设备参数:

有两种方法,一种是通过WIFI,另一种是通过USB数据线将手机链接电脑。 (我是通过USB这个方法,按照后手机会多一个ATX小汽车图标的软件)

WiFi连接更方便一点,需要保持PC和手机使用的一个WIFI,查看手机连接WIFI的IP地址。

 
 

连接手机的方式:
1.通过WiFi:ip根据情况自己修改

d=u2.connect_wifi("ip")

2.通过d=u2.connect_usb()

d=u2.connect_usb()

3.安装应用

d.app_install(' url ')

4.跳过弹窗,禁止弹窗

d.disable_popups() #自动跳过弹出窗口

d.disable_popups(假)#禁用自动跳过弹出窗口

5.获取基本信息

d.info

6.获取窗口大小

print(d.window_size())

7.打开/关闭屏幕

d.screen_on()#打开

d.screen_off()#关闭

8.获取当前屏幕状态

d.info.get(' screenOn ')

9.解锁屏幕

d.unlock()

10.点击屏幕

d.click(x,y)

11.双击

d.double_click(x,y)

12.长按一下屏幕

d.long_click(x,y)

d.long_click(X,Y,1)#长按1秒(默认)

13.滑动

d.swipe(x, y, x, y)

14.按键操作

d.press("home")

d.press("back")

d.press(x, y)

15.向上滑动

d(scrollable=True).scroll.vert.backward()

16.解锁屏幕

d.healthcheck() # 解锁屏幕并启动uiautomator服务

 最后的d.service("uiautomator").stop()是因为,安卓上的UiAutomator是独享的,一旦一个服务使用了它,其他人就不让碰了。所以 appiummacacauiautomatorviewer.bat 只要你用了UiAutomator服务,都是冲突的。只有再用完之后,停止掉uiautomator service,才能让其他服务使用

testerhome.cn

https://106.75.214.88/topics/12521

原文地址:https://www.cnblogs.com/du-jun/p/13707638.html