1、设备对象

引入uiautomator,获取设备对象<所谓设备对象可理解为:Android模拟器或者真机>

语法:from uiautomator import device as d

d 即为设备对象

1.1、获取设备信息

语法:d.info

返回值:

返回值解释如下:

displayRotation 0 代表竖屏 1 代表横屏

currentPackageName 当前的Activity的Package名字

productName 当前设备名称

displayWidth 当前设备屏幕宽度 当 displayRotation 取值为 1 时,也就是说当前是横屏状态时,displayWidth 取值会和 displayHeight 互换

displayHeight 当前设备屏幕高度 当 displayRotation 取值为 1 时,也就是说当前是横屏状态时,displayHeight 取值会和 displayWidth 互换

sdkInt 当前SDK版本

naturalOrientation 当 displayRotation 取值为 1 时,也就是说当前是横屏状态时,取值为False,为竖屏状态时,取值为:True

1.2、点亮或熄灭屏幕(Turn on/off screen)

检查屏幕状态,关闭OR点亮?

1.3、系统常用按键

下面的这些按键也是被支持的,如下:

Next keys are currently supported:

  • 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.4、与设备交互(单击、长按、滑动(手势密码)、拖拽)

单击屏幕坐标点

长按屏幕坐标点

在屏幕上滑动

在屏幕上拖拽

1.5、屏幕操作及屏幕方向获取与控制<上述:displayRotation 0 代表竖屏 1 代表横屏>,竖屏分为 natural(自然的,正常的竖屏) 和 upsidedown(倒过来的竖屏),横屏分为向左和向右两个方向,分别为:left 和 right

设备属性:orientation 可能取得值为:

  • natural or n
  • left or l
  • right or r
  • upsidedown or u (can not be set)

说明:在手机设备上,倒过来的屏幕很少见,因此:d.orientation 取值 upsidedown 的可能性几乎没有

锁屏/解除锁屏

截屏操作

打开通知或快速设置

注意:(如果notification已经打开了,调用d.open.quick_settings()不会打开快速设置)

等待空闲或窗口更新(Wait for idle or window update)

2、uiautomator 选择器

选择器是在当前窗口中标识特定的UI对象。可理解为:UiObject对象

目前,在uiautomator中支持以下属性选择器:

下面依次进行解读:

2.1、text选择器(支持在uiautomator中Text属性不为空的元素)

例如:

安卓 uiautomator设备测试 和选择器~Python详解-Python 技术分享 Java技术分享 Python 爬虫技术_微信公众号:zeropython—昊天博客

具体用法如下:

总之:要尽可能的使用选择器唯一确定一个被选择对象(UiObject)

除了可以进行选择UiObject对象以外,我们亦可以使用选择器设置某些元素的值,如下:

安卓 uiautomator设备测试 和选择器~Python详解-Python 技术分享 Java技术分享 Python 爬虫技术_微信公众号:zeropython—昊天博客

textContains,textMaches,textStartsWith 分别代表:包含,正则表达式,以XXX开头等

例如:

d(text="Name").set_text("John")

d(textContains="ame").set_text("John")

d(textStartsWith="Nam").set_text("John")

2.2、className,classNameMatches 类选择器 及 description, descriptionContains, descriptionMatches, descriptionStartsWith 描述选择器 及 packageName, packageNameMatches 包选择器 及 resourceId, resourceIdMatches ResId选择器用法和text选择器类似,都可以多属性选择器结合在一起使用。

安卓 uiautomator设备测试 和选择器~Python详解-Python 技术分享 Java技术分享 Python 爬虫技术_微信公众号:zeropython—昊天博客

示例代码如下:

在此,说明下child、sibling选择器和index选择器及instance选择器(严格讲instance不是选择器,仅仅只是在输出多个结果的情况下,可以通过索引(下标)进行选择)

首先说明child选择器,sibling选择器:分别可理解为:子选择器(可嵌套),兄弟姐妹选择器

安卓 uiautomator设备测试 和选择器~Python详解-Python 技术分享 Java技术分享 Python 爬虫技术_微信公众号:zeropython—昊天博客

如上图右边部分,从上到下层次分为四层,最后一层的两个元素可理解为兄弟姐妹,在此,我们如果要选择最下面的那个元素就可以用到child选择器及sibling选择器,当然,本人不建议使用孩子,兄妹选择器,如果能用其他方法实现,建议用其他方法:

在此,我写的方法如下:<没有具体验证,仅仅只是演示>

2.3、index选择器及instance,比较容易混淆的两个,一个是选择器,一个代表索引,如下:

index选择器对应uiautomator的index属性,如下:

安卓 uiautomator设备测试 和选择器~Python详解-Python 技术分享 Java技术分享 Python 爬虫技术_微信公众号:zeropython—昊天博客

其用法和text选择器大同小异,不过在此需要指出的是,有些窗体中index取值会发生改变,因此,能不用index选择器的,尽可能不用!

instance 的用法:当你的选择器返回的结果不是指向唯一元素时(两个或者多个),你可以通过instance进行选择。

贺晓聪原文:

Multiple instances

Sometimes the screen may contain multiple views with the same e.g. text, then you will have to use "instance" properties in selector like below:

However, uiautomator provides list like methods to use it.

2.4、获取选定的UI对象状态及其信息(Get the selected ui object status and its information)

检测特定的UI对象是否存在(Check if the specific ui object exists)

两种写法,如下:

检索特定UI对象的信息(Retrieve the info of the specific ui object)

(结果为列表List),如下:

设置/清除字段或编辑文本

执行单击特定的UI对象

长时间点击特定的ui对象,双击?

将UI对象拖动到另一点

滑动UI对象

滑动分为四个方向:left ,right,top ,bottom 即:左滑动 右滑动 上滑动 及向下滑动

Two point gesture from one point to another

Two point gesture on the specific ui object

Supports two gestures:

  • In, from edge to center
  • Out, from center to edge

3 point gesture

等到特定的UI对象出现或消失

在具体的UI对象执行甩(滚动)Perform scroll on the specific ui object(scrollable)

Possible properties:

  • horiz or vert
  • forward or backward or toBeginning or toEnd

Perform scroll on the specific ui object(scrollable)-在具体的UI对象执行甩(滚动)

Possible properties:

  • horiz or vert
  • forward or backward or toBeginning or toEnd, or to

HTTPX 基础教程-新乡seo|网站优化,网站建设_微信公众号:zeropython—昊天博客