一、要求

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.2。
d.screenshot( “ home.jpg ”) # get PIL.Image格式化图像。当然,你需要首先安装pillow
image = d.screenshot() # default format =“pillow”
image.save( “ home.jpg ”)#或home.png。目前,只有PNG和JPG支持 #得到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对象拖动到屏幕点(x,y),0.5秒后
d( text = “设置”).drag_to(x,y, duration = 0.5)
#将UI对象拖动到另一个(中心位置) UI对象,在0.25秒
d( text = “设置”).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                   #电源键

六、使用经验
1、使用前初始化
python -m uiautomator2 init
2、打开工具

python3 -m weditor

uiautomator2使用教程的更多相关文章

  1. uiautomator2 入门教程

    一.前言 在 Android 自动化测试方面,Google 提供了一个基于 Java 开发的库 UiAutomator,基本上支持所有的 Android 事件操作,使用简单. 在此基础上,有大佬开发出 ...

  2. 【UIautomator2 安卓自动化】教程

    一.环境准备: [安装] 安装uiautomator2 pip install --pre uiautomator2 pip install pillow [初始化] 部署相关的守护进程. 电脑连接上 ...

  3. uiautomator2.0框架

    1.   Uiautomator1.0 Uiautomator2.0 date 2012 2015 super class UiAutomatorTestCase InstrumentationTes ...

  4. uiautomator2+python自动化测试1-环境准备

    前言 uiautomator是Google提供的用来做安卓自动化测试的一个Java库.功能很强,可以对第三方App进行测试,获取屏幕上任意一个APP的任意一个控件属性,并对其进行任意操作,但有两个缺点 ...

  5. Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求

    上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...

  6. Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数

    上一篇:Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数 之前介绍了简单的路由以及传参,这篇文章我们将要学习复杂一些的路由以及传递其他附加参数.一个好的路由系统可以使我们 ...

  7. Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数

    上一篇:Angular2入门系列教程-服务 上一篇文章我们将Angular2的数据服务分离出来,学习了Angular2的依赖注入,这篇文章我们将要学习Angualr2的路由 为了编写样式方便,我们这篇 ...

  8. Angular2入门系列教程4-服务

    上一篇文章 Angular2入门系列教程-多个组件,主从关系 在编程中,我们通常会将数据提供单独分离出来,以免在编写程序的过程中反复复制粘贴数据请求的代码 Angular2中提供了依赖注入的概念,使得 ...

  9. Angular2入门系列教程1-使用Angular-cli搭建Angular2开发环境

    一直在学Angular2,百忙之中抽点时间来写个简单的教程. 2016年是前端飞速发展的一年,前端越来越形成了(web component)组件化的编程模式:以前Jquery通吃一切的田园时代一去不复 ...

随机推荐

  1. C#测试代码、函数、方法执行时间,方便进行系统性能评估

    代码如下: System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start() ...

  2. Java之路---Day09(继承)

    2019-10-23-22:58:23 目录 1.继承 2.区分成员变量重名的方法 3.区分成员方法重名的方法 4.继承中重写与重载的区别 5.继承中覆盖重写的注意事项 6.继承中覆盖重写的设计原则 ...

  3. TCP三次握手四次挥手介绍

    学过计算机网络的同学都知道TCP协议是计算机网络课程里面最复杂的协议之一,还没有通信就要搞个什么三次握手,断开还要什么四次分手,中间还要什么流量控制啦,拥塞控制,滑动窗口什么的,初学者看了就会头晕. ...

  4. localStorage&sessionStorage&Cookie

    localStorage.sessionStorage.Cookie三者区别如下:

  5. ifup/ifdown

    这两个程序其实是script而已,它会直接到 /etc/ sysconfig/network-scripts目录下搜索对应的配置文件,例如ifup eth0,它会找出ifcfg-eth0这个文件的内容 ...

  6. Building Objective-C static libraries with categories

    Q: How do I fix "selector not recognized" runtime exceptions when trying to use category m ...

  7. 图说jdk1.8新特性(4)--- stream

    总述 jdk1.8引入了Stream相关的API,通过该API.可以实现流式编程,使你写代码的时候行云流水 Stream使得集合的转换变得更加简单,原来可能需要写多个for循环或者多个if判断的,直接 ...

  8. jsonpath 一个简单实用的工具

    import jsonpath import json data = "{\"a\": \"11\", \"c\": {\&quo ...

  9. Python学习日记(三十一) 黏包问题

    import subprocess res = subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE,stderr=subprocess.P ...

  10. 【hadoop】hadoop3.2.0应用环境搭建与使用指南

    下面列出我搭建hadoop应用环境的文章整理在一起,不定期更新,供大家参考,互相学习!!! 杂谈篇: [英语学习]Re-pick up English for learning big data (n ...