详解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实现一次创建多级目录的方法
May 15 Python
Python变量和字符串详解
Apr 29 Python
python代码实现逻辑回归logistic原理
Aug 07 Python
Python利用PyExecJS库执行JS函数的案例分析
Dec 18 Python
如何实现更换Jupyter Notebook内核Python版本
May 18 Python
keras 权重保存和权重载入方式
May 21 Python
Win10用vscode打开anaconda环境中的python出错问题的解决
May 25 Python
PyCharm中关于安装第三方包的三个建议
Sep 17 Python
详解Python中list[::-1]的几种用法
Nov 16 Python
Jupyter notebook命令和编辑模式常用快捷键汇总
Nov 17 Python
Django路由层如何获取正确的url
Jul 15 Python
python小型的音频操作库mp3Play
Apr 24 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
destoon官方标签大全
2014/06/20 PHP
PHP实现图片不变型裁剪及图片按比例裁剪的方法
2016/01/14 PHP
PHP结合jquery ajax实现上传多张图片,并限制图片大小操作示例
2019/03/01 PHP
PHP中关于php.ini参数优化详解
2020/02/28 PHP
各浏览器对click方法的支持差异小结
2011/07/31 Javascript
在服务端(Page.Write)调用自定义的JS方法详解
2013/08/09 Javascript
JQUERY dialog的用法详细解析
2013/12/19 Javascript
JavaScript将一个数组插入到另一个数组的方法
2015/03/19 Javascript
Javascript中setTimeOut和setInterval的定时器用法
2015/06/12 Javascript
jquery实现的缩略图预览滑块实例
2015/06/25 Javascript
jQuery实现定位滚动条位置
2016/08/05 Javascript
简单理解vue中实例属性vm.$els
2016/12/01 Javascript
原生js实现节日时间倒计时功能
2017/01/18 Javascript
js实现用户输入的小写字母自动转大写字母的方法
2017/01/21 Javascript
spirngmvc js传递复杂json参数到controller的实例
2018/03/29 Javascript
微信小程序wepy框架笔记小结
2018/08/08 Javascript
vue.js实现点击图标放大离开时缩小的代码
2021/01/27 Vue.js
python基础教程之循环介绍
2014/08/29 Python
使用Python实现一个简单的项目监控
2015/03/31 Python
python决策树之CART分类回归树详解
2017/12/20 Python
使用Python检测文章抄袭及去重算法原理解析
2019/06/14 Python
python使用装饰器作日志处理的方法
2019/07/11 Python
python对常见数据类型的遍历解析
2019/08/27 Python
Python中pyecharts安装及安装失败的解决方法
2020/02/18 Python
python 中关于pycharm选择运行环境的问题
2020/10/31 Python
解决CSS3的opacity属性带来的层叠顺序问题
2016/05/09 HTML / CSS
html5 video标签屏蔽右键视频另存为的js代码
2013/11/12 HTML / CSS
详解如何使用rem或viewport进行移动端适配
2020/08/14 HTML / CSS
澳大利亚正品化妆品之家:Cosmetic Capital
2017/07/03 全球购物
Cecil Mode法国在线商店:女性时尚
2021/01/08 全球购物
数控专业毕业生求职信范文
2013/09/21 职场文书
学生宿舍管理制度
2014/01/30 职场文书
优秀应届毕业生推荐信
2014/02/18 职场文书
个人学习党的群众路线教育实践活动心得体会
2014/11/05 职场文书
2015年财政所工作总结
2015/04/25 职场文书
python实现ROA算子边缘检测算法
2021/04/05 Python