详解python uiautomator2 watcher的使用方法


Posted in Python onSeptember 09, 2019

该方是基于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高并发异步服务器核心库forkcore使用方法
Nov 26 Python
简单了解Python下用于监视文件系统的pyinotify包
Nov 13 Python
读取本地json文件,解析json(实例讲解)
Dec 06 Python
python3写爬取B站视频弹幕功能
Dec 22 Python
Python实现读取Properties配置文件的方法
Mar 29 Python
python八皇后问题的解决方法
Sep 27 Python
VSCode Python开发环境配置的详细步骤
Feb 22 Python
如何用C代码给Python写扩展库(Cython)
May 17 Python
python 获取等间隔的数组实例
Jul 04 Python
Python matplotlib生成图片背景透明的示例代码
Aug 30 Python
python代码如何实现余弦相似性计算
Feb 09 Python
Django跨域请求原理及实现代码
Nov 14 Python
一行Python代码制作动态二维码的实现
Sep 09 #Python
python制作英语翻译小工具代码实例
Sep 09 #Python
Python pandas.DataFrame 找出有空值的行
Sep 09 #Python
Python制作词云图代码实例
Sep 09 #Python
PyCharm2018 安装及破解方法实现步骤
Sep 09 #Python
浅谈pycharm使用及设置方法
Sep 09 #Python
在pycharm中配置Anaconda以及pip源配置详解
Sep 09 #Python
You might like
PHP flush 函数使用注意事项
2016/08/26 PHP
PHP实现基于图的深度优先遍历输出1,2,3...n的全排列功能
2017/11/10 PHP
php链式操作的实现方式分析
2019/08/12 PHP
js类中获取外部函数名的方法
2007/08/19 Javascript
jQuery 淡入淡出 png图在ie8下有黑色边框的解决方法
2013/03/05 Javascript
javascript字符串替换及字符串分割示例代码
2013/12/12 Javascript
Jquery下EasyUI组件中的DataGrid结果集清空方法
2014/01/06 Javascript
js解决select下拉选不中问题
2014/10/14 Javascript
js获取当前时间显示在页面上并每秒刷新
2014/12/24 Javascript
javascript中mouseover、mouseout使用详解
2015/07/19 Javascript
JS+DIV+CSS实现的经典标签切换效果代码
2015/09/14 Javascript
AngularJS 所有版本下载地址
2016/09/14 Javascript
浅谈js函数的多种定义方法与区别
2016/11/29 Javascript
js实现类bootstrap模态框动画
2017/02/07 Javascript
JS优化与惰性载入函数实例分析
2017/04/06 Javascript
原生javascript上传图片带进度条【实例分享】
2017/04/06 Javascript
react实现一个优雅的图片占位模块组件详解
2017/10/30 Javascript
微信用户访问小程序的登录过程详解
2019/09/20 Javascript
vue项目使用高德地图的定位及关键字搜索功能的实例代码(踩坑经验)
2020/03/07 Javascript
vue 遮罩层阻止默认滚动事件操作
2020/07/28 Javascript
Vue Router中应用中间件的方法
2020/08/06 Javascript
python抓取最新博客内容并生成Rss
2015/05/17 Python
Python实现TCP/IP协议下的端口转发及重定向示例
2016/06/14 Python
Python基础教程之浅拷贝和深拷贝实例详解
2017/07/15 Python
tensorflow 限制显存大小的实现
2020/02/03 Python
Python基于pip实现离线打包过程详解
2020/05/15 Python
你不知道的葡萄干处理法、橙蜜处理法、二氧化碳酵母法
2021/03/17 冲泡冲煮
粉红色的鲸鱼:Vineyard Vines
2018/02/17 全球购物
无谷物狗粮:Pooch & Mutt
2018/05/23 全球购物
英国家居用品和床上用品零售商:P&B Home
2020/01/16 全球购物
红旗团支部事迹材料
2014/01/27 职场文书
学习党的群众路线教育实践活动心得体会
2014/03/01 职场文书
汽车维修求职信
2014/06/15 职场文书
刚学完怎么用Python实现定时任务,转头就跑去撩妹!
2021/06/05 Python
能用CSS实现的就不要麻烦JavaScript了
2021/10/05 HTML / CSS
Java中的Kafka为什么性能这么快及4大核心详析
2022/09/23 Java/Android