详解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计算程序开始到程序结束的运行时间和程序运行的CPU时间
Nov 28 Python
分析用Python脚本关闭文件操作的机制
Jun 28 Python
python脚本爬取字体文件的实现方法
Apr 29 Python
Python生成器定义与简单用法实例分析
Apr 30 Python
Python快速查找list中相同部分的方法
Jun 27 Python
Python list列表中删除多个重复元素操作示例
Feb 27 Python
Python Subprocess模块原理及实例
Aug 26 Python
关于python导入模块import与常见的模块详解
Aug 28 Python
Python random模块制作简易的四位数验证码
Feb 01 Python
Python如何在循环内使用list.remove()
Jun 01 Python
python中导入 train_test_split提示错误的解决
Jun 19 Python
一篇文章搞懂python的转义字符及用法
Sep 03 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代码
2007/03/03 PHP
php $_SERVER[&quot;REQUEST_URI&quot;]获取值的通用解决方法
2010/06/21 PHP
php实现下载限制速度示例分享
2014/02/13 PHP
PHP日期函数date格式化UNIX时间的方法
2015/03/19 PHP
JavaScript isPrototypeOf和hasOwnProperty使用区别
2010/03/04 Javascript
jquery随机展示头像代码
2011/12/21 Javascript
jquery的ajax异步请求接收返回json数据实例
2014/06/16 Javascript
jquery提示效果实例分析
2014/11/25 Javascript
jQuery prototype冲突的2种解决方法(附demo示例下载)
2016/01/21 Javascript
JS正则表达式比较常见用法
2016/01/26 Javascript
详解js中call与apply关键字的作用
2016/11/21 Javascript
nodejs用gulp管理前端文件方法
2018/06/24 NodeJs
JavaScript实现的滚动公告特效【基于jQuery】
2019/07/10 jQuery
浅谈Webpack4 Tree Shaking 终极优化指南
2019/11/18 Javascript
微信小程序实现多选框功能的实例代码
2020/06/24 Javascript
vue+vant 上传图片需要注意的地方
2021/01/03 Vue.js
python实现带错误处理功能的远程文件读取方法
2015/04/29 Python
python实现数值积分的Simpson方法实例分析
2015/06/05 Python
python操作mysql数据库
2017/03/05 Python
使用Python生成XML的方法实例
2017/03/21 Python
python使用os.listdir和os.walk获得文件的路径的方法
2017/12/16 Python
python pandas 如何替换某列的一个值
2018/06/09 Python
使用Python实现从各个子文件夹中复制指定文件的方法
2018/10/25 Python
Django框架使用mysql视图操作示例
2019/05/15 Python
详解利用Python scipy.signal.filtfilt() 实现信号滤波
2019/06/05 Python
QML用PathView实现轮播图
2020/06/03 Python
Django vue前后端分离整合过程解析
2020/11/20 Python
澳大利亚窗帘商店:Curtain Wonderland
2019/12/01 全球购物
工程师自我评价怎么写
2013/09/19 职场文书
团日活动总结书格式
2014/05/08 职场文书
学校党员对照检查材料
2014/08/28 职场文书
工资收入证明样本(5篇)
2014/09/16 职场文书
升职自荐信怎么写
2015/03/05 职场文书
教师培训简讯
2015/07/20 职场文书
Win11怎么添加用户?Win11添加用户账户的方法
2022/07/15 数码科技
Windows server 2016服务器基本设置
2022/08/14 Servers