python uiautomator2 watcher的使用方法
该方是基于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的使用方法的更多相关文章
- 用 Python 排序数据的多种方法
用 Python 排序数据的多种方法 目录 [Python HOWTOs系列]排序 Python 列表有内置就地排序的方法 list.sort(),此外还有一个内置的 sorted() 函数将一个可迭 ...
- sqlalchemy mark-deleted 和 python 多继承下的方法解析顺序 MRO
sqlalchemy mark-deleted 和 python 多继承下的方法解析顺序 MRO 今天在弄一个 sqlalchemy 的数据库基类的时候,遇到了跟多继承相关的一个小问题,因此顺便看了一 ...
- python子类调用父类的方法
python子类调用父类的方法 python和其他面向对象语言类似,每个类可以拥有一个或者多个父类,它们从父类那里继承了属性和方法.如果一个方法在子类的实例中被调用,或者一个属性在子类的实例中被访问, ...
- paip.编程语言方法重载实现的原理及python,php,js中实现方法重载
paip.编程语言方法重载实现的原理及python,php,js中实现方法重载 有些语言,在方法的重载上,形式上不支持函数重载,但可以通过模拟实现.. 主要原理:根据参数个数进行重载,或者使用默认值 ...
- python常用数据类型内置方法介绍
熟练掌握python常用数据类型内置方法是每个初学者必须具备的内功. 下面介绍了python常用的集中数据类型及其方法,点开源代码,其中对主要方法都进行了中文注释. 一.整型 a = 100 a.xx ...
- Python使用MySQL数据库的方法以及一个实例
使用环境:Windows+python3.4+MySQL5.5+Navicat 一.创建连接 1.准备工作,想要使用Python操作MySQL,首先需要安装MySQL-Python的包,在Python ...
- python中List的sort方法的用法
python列表排序 简单记一下python中List的sort方法(或者sorted内建函数)的用法. 关键字: python列表排序 python字典排序 sorted List的元素可以是各种东 ...
- python字符串内容替换的方法(转载)
python字符串内容替换的方法 时间:2016-03-10 06:30:46来源:网络 导读:python字符串内容替换的方法,包括单个字符替换,使用re正则匹配进行字符串模式查找与替换的方法. ...
- 【转】python中List的sort方法(或者sorted内建函数)的用法
原始出处:http://gaopenghigh.iteye.com/blog/1483864 python列表排序 简单记一下python中List的sort方法(或者sorted内建函数)的用法. ...
随机推荐
- HDU-3727 Jewel
Jimmy wants to make a special necklace for his girlfriend. He bought many beads with various sizes, ...
- XCode项目配置
此设置优先级在playersetting之上,如果为空或者格式不正确或者文件不存在将不会设置,请注意 一.设置面板 二.对应Xcode中设置 1.TeamID 登录苹果开发者网站,查看个人信息,就有 ...
- MDS 多活配置
CephFS 介绍及使用经验分享 阅读 1179 收藏 2 2019-01-14 原文链接:www.jianshu.com WebRTC SFU中发送数据包的丢失反馈juejin.im 目录 Ceph ...
- MooseFS 分布式存储
一.MooseFS介绍 MooseFS主要由管理服务器(master).元日志服务器(Metalogger).数据存储服务器(chunkserver)构成. 管理服务器:主要作用是管理数据存储服务器, ...
- isensor app kit 之 CF5642V2 OV5642 测试总结
. 总结; 使用官哥的cf5642c-v2时,需要将isensor app kit 上的iic上拉电阻去掉,否则可能导致寄存器初始化不成功,去掉即可,使用柴草电子的模组则不需要.
- art-template模板判断
1.添加模板 <script id="userinfo" type="text/template"> {{ if id == n ...
- 【JS】374- 重学 this 关键字
为什么要学习this关键字 1. 面试会问啊!总有一些面试官喜欢问你一段不可能这么写的代码.看一道经典且古老的面试题(学完本文后,文末会有一道更复杂的面试题等着你哦!) 代码如下: let a = 5 ...
- 【H5】344- 微信 H5 页面兼容性解决方案
点击上方"前端自习课"关注,学习起来~ 最近给公司微信公众号,写了微信h5业务页面,总结分享一下前端开发过程中的几个兼容性坑,项目直接拿的公司页面,所以下文涉及图片都模糊处理了. ...
- layim手机版嵌入app
感觉记忆力不行了,才做了就有点忘了.先简单做下记录... 遇到的问题 需要类似微信的语音发送功能 前端发送给后端,在mogodb中用二进制存储.后端取到数据发送给我的也是blob.前端拿到数据就变了. ...
- 【树莓派】制作启动SD卡
版权声明:本文为博主原创文章,转载请注明出处. https://www.cnblogs.com/YaoYing/ 下载烧写软件 烧写软件 将下载的压缩文件解压缩并安装到电脑上 下载树莓派镜像 树莓派系 ...