该方是基于uiautomator2如下版本进行验证的:

PS C:\windows\system32> pip show uiautomator2
Name: uiautomator2
Version: 1.2.2
Summary: Python Wrapper for Android UiAutomator2 test tool
Home-page: https://github.com/codeskyblue/uiautomator2
Author: codeskyblue
Author-email: codeskyblue@gmail.com
License: MIT
Location: c:\program files\python36\lib\site-packages
Requires: six, progress, whichcraft, logzero, lxml, adbutils, retry, Pillow, requests, humanize
Required-by: weditor, atx

  下面贴出githup上关于该方法的使用

 Watcher
You can register watchers to perform some actions when a selector does not find a match. Register Watcher When a selector can not find a match, uiautomator2 will run all registered watchers. Click target when conditions match
d.watcher("AUTO_FC_WHEN_ANR").when(text="ANR").when(text="Wait") \
.click(text="Force Close")
# d.watcher(name) ## creates a new named watcher.
# .when(condition) ## the UiSelector condition of the watcher.
# .click(target) ## perform click action on the target UiSelector.
There is also a trick about click. You can use click without arguments. d.watcher("ALERT").when(text="OK").click()
# Same as
d.watcher("ALERT").when(text="OK").click(text="OK")
Press key when a condition becomes true
d.watcher("AUTO_FC_WHEN_ANR").when(text="ANR").when(text="Wait") \
.press("back", "home")
# d.watcher(name) ## creates a new named watcher.
# .when(condition) ## the UiSelector condition of the watcher.
# .press(<keyname>, ..., <keyname>.() ## press keys one by one in sequence.
Check if the named watcher triggered A watcher is triggered, which means the watcher was run and all its conditions matched. d.watcher("watcher_name").triggered
# true in case of the specified watcher triggered, else false
Remove a named watcher # remove the watcher
d.watcher("watcher_name").remove()
List all watchers d.watchers
# a list of all registered watchers
Check for any triggered watcher d.watchers.triggered
# true in case of any watcher triggered
Reset all triggered watchers # reset all triggered watchers, after that, d.watchers.triggered will be false.
d.watchers.reset()
Remove watchers # remove all registered watchers
d.watchers.remove()
# remove the named watcher, same as d.watcher("watcher_name").remove()
d.watchers.remove("watcher_name")
Force to run all watchers # force to run all registered watchers
d.watchers.run()

注:里面涉及的watcher_name可以自定义,可以做到见名知意即可

watcher的使用是要先注册(第9行至20行均是注册watcher的方法),然后激活watcher(第56行),注意这个激活方法只是一个瞬时激活,就是说使用之后即销毁,不会一直存于后台。那这样的话在实际的使用场景中怎么使用这个功能呢,下面看一段脚本 1 # -*- coding:utf-8 -*-


import uiautomator2 as u2
import time d = u2.connect()
cfg = MTBFConfig()
package = cfg.getstr("Admit", "pkg", "config")
PACKAGELIST = package.split(",")
print(PACKAGELIST)
d.watcher("‎‏‎‎‎‎‎‏‎‏‏‏‎‎‎‎‎‏‎‎‏‎‎‎‎‏‏‏‏‏‏‏‏‎‎‎‎‎‎‎‎‎‏‎‎‎‏‎‎‏‏‎‏‎‏‎‎‎‏‏‏‎‏‏‏‎‏‏‎‎‎‏‏‏‏‏‏‏‎‎‏‎‎‏‏‎‎‎‏‎‎‎‏‏‏‎ALLOW‎‏‎‎‏‎").when(text="‎‏‎‎‎‎‎‏‎‏‏‏‎‎‎‎‎‏‎‎‏‎‎‎‎‏‏‏‏‏‏‏‏‎‎‎‎‎‎‎‎‎‏‎‎‎‏‎‎‏‏‎‏‎‏‎‎‎‏‏‏‎‏‏‏‎‏‏‎‎‎‏‏‏‏‏‏‏‎‎‏‎‎‏‏‎‎‎‏‎‎‎‏‏‏‎ALLOW‎‏‎‎‏‎").click(text="‎‏‎‎‎‎‎‏‎‏‏‏‎‎‎‎‎‏‎‎‏‎‎‎‎‏‏‏‏‏‏‏‏‎‎‎‎‎‎‎‎‎‏‎‎‎‏‎‎‏‏‎‏‎‏‎‎‎‏‏‏‎‏‏‏‎‏‏‎‎‎‏‏‏‏‏‏‏‎‎‏‎‎‏‏‎‎‎‏‎‎‎‏‏‏‎ALLOW‎‏‎‎‏‎")
#d.watchers.run()
print(d.watchers) time.sleep(2)
pkglen = len(PACKAGELIST)
print(("There are %d package for test") %pkglen) class Admit(object): def main(self):
for i in range(pkglen):
k = 0
for j in range(5):
if d.info['currentPackageName'] != PACKAGELIST[i]:
d.app_start(PACKAGELIST[i])
print(PACKAGELIST[i])
time.sleep(1)
k += 1
if k == 3:
print("Can not enter "+ str(PACKAGELIST[i]))
return False
if PACKAGELIST[i] == 'com.google.android.contacts':
print("hello")
if d(description = "Open navigation drawer").exists(timeout = 5):
d(description = "Open navigation drawer").click() if d(text = "Settings").exists(timeout = 5):
d(text = "Settings").click() if d(resourceId="android:id/title", text = "Import").exists(timeout=5):
d(resourceId="android:id/title", text = "Import").click()
time.sleep(3) if d(resourceId = "android:id/button1", text = "OK").exists(timeout = 5):
d(resourceId = "android:id/button1", text = "OK").click()
time.sleep(1)
d.watchers.run() //在上面OK点击之后会弹出一个权限访问的许可,所以这个时候需要激活一次watcher把弹框关掉,以便不影响后续测试,所以就一个原则,哪里可能会有弹框就在哪里激活watcher
if __name__=="__main__":
ad = Admit()
ad.main()

python uiautomator2 watcher的使用方法的更多相关文章

  1. 用 Python 排序数据的多种方法

    用 Python 排序数据的多种方法 目录 [Python HOWTOs系列]排序 Python 列表有内置就地排序的方法 list.sort(),此外还有一个内置的 sorted() 函数将一个可迭 ...

  2. sqlalchemy mark-deleted 和 python 多继承下的方法解析顺序 MRO

    sqlalchemy mark-deleted 和 python 多继承下的方法解析顺序 MRO 今天在弄一个 sqlalchemy 的数据库基类的时候,遇到了跟多继承相关的一个小问题,因此顺便看了一 ...

  3. python子类调用父类的方法

    python子类调用父类的方法 python和其他面向对象语言类似,每个类可以拥有一个或者多个父类,它们从父类那里继承了属性和方法.如果一个方法在子类的实例中被调用,或者一个属性在子类的实例中被访问, ...

  4. paip.编程语言方法重载实现的原理及python,php,js中实现方法重载

    paip.编程语言方法重载实现的原理及python,php,js中实现方法重载 有些语言,在方法的重载上,形式上不支持函数重载,但可以通过模拟实现.. 主要原理:根据参数个数进行重载,或者使用默认值 ...

  5. python常用数据类型内置方法介绍

    熟练掌握python常用数据类型内置方法是每个初学者必须具备的内功. 下面介绍了python常用的集中数据类型及其方法,点开源代码,其中对主要方法都进行了中文注释. 一.整型 a = 100 a.xx ...

  6. Python使用MySQL数据库的方法以及一个实例

    使用环境:Windows+python3.4+MySQL5.5+Navicat 一.创建连接 1.准备工作,想要使用Python操作MySQL,首先需要安装MySQL-Python的包,在Python ...

  7. python中List的sort方法的用法

    python列表排序 简单记一下python中List的sort方法(或者sorted内建函数)的用法. 关键字: python列表排序 python字典排序 sorted List的元素可以是各种东 ...

  8. python字符串内容替换的方法(转载)

    python字符串内容替换的方法 时间:2016-03-10 06:30:46来源:网络 导读:python字符串内容替换的方法,包括单个字符替换,使用re正则匹配进行字符串模式查找与替换的方法.   ...

  9. 【转】python中List的sort方法(或者sorted内建函数)的用法

    原始出处:http://gaopenghigh.iteye.com/blog/1483864 python列表排序 简单记一下python中List的sort方法(或者sorted内建函数)的用法. ...

随机推荐

  1. Day 08 可变与不可变对象/列表与字典内置方法

    目录 可变对象与不可变对象 可变对象 不可变对象 列表的内置方法 字典内置方法 可变对象与不可变对象 可变对象 对象指向的内存中的值会改变,当更改这个变量的时候,还是指向原来内存中的值,并且在原来的内 ...

  2. 【CSS】357- 坚定地使用 CSS Custom Properties

    自定义属性(Custom Properties)是一个很有魅力的 CSS 新特性,现代浏览器广泛 支持.但是遇到那些不支持 CSS Custom Properties 的老掉牙浏览器我们该怎么办?等着 ...

  3. robotframework配置邮箱服务器

    1.登录邮箱以腾讯企业邮箱为例:开启smtp服务并获得邮箱的客户端授权码 用户名:18890260218@163.com 客户端授权码:admin123 2.进入系统管理-->GO to plu ...

  4. display:none和visibility:hidden两者的区别

    display与元素的隐藏 如果给一个元素设置了display: none,那么该元素以及它的所有后代元素都会隐藏,它是前端开发人员使用频率最高的一种隐藏方式.隐藏后的元素无法点击,无法使用屏幕阅读器 ...

  5. webpack 环境搭建

    Webpack环境搭建 一.安装node 1.node官网下载node并安装----node里面内置了npm所以用在安装npm了 2.命令行输入node -v查看node是否安装成功 二.全局安装we ...

  6. vue响应式数据变化

    vue响应式数据变化 话不多说,先上代码: //拷贝一份数组原型,防止修改所有数组类型变量的原型方法 let arrayProto = Array.prototype;// 数组原型上的方法 let ...

  7. 《Java基础知识》Java常量的申明和使用

    常量就是从程序开始运行到结束都不变的量.在 Java 程序设计中,使用关键字“final”来声明一个常量,例如下面的程序代码. 这里的 x 是一个常量,但是是在某个方法内的常量,也可以称为成员常量(作 ...

  8. 利用sklearn对多分类的每个类别进行指标评价

      今天晚上,笔者接到客户的一个需要,那就是:对多分类结果的每个类别进行指标评价,也就是需要输出每个类型的精确率(precision),召回率(recall)以及F1值(F1-score).   对于 ...

  9. RAID磁盘阵列介绍

    磁盘阵列 RAID介绍 一.简介: 磁盘阵列(Redundant Arrays of Independent Drives,RAID),有“独立磁盘构成的具有冗余能力的阵列”之意. 最初是由加利福尼亚 ...

  10. java读取地址数据文件

    在工作中遇到读取地址文件数据: 1.读取本地文件数据(如:D:\data.txt) //适用于读取绝对地址文件 public String getData(String path) { String ...