详解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执行shell获取硬件参数写入mysql的方法
Dec 29 Python
Python实现设置windows桌面壁纸代码分享
Mar 28 Python
Python NumPy库安装使用笔记
May 18 Python
Python实现简单HTML表格解析的方法
Jun 15 Python
快速排序的算法思想及Python版快速排序的实现示例
Jul 02 Python
python 提取tuple类型值中json格式的key值方法
Dec 31 Python
python遍历文件目录、批量处理同类文件
Aug 31 Python
win10环境下配置vscode python开发环境的教程详解
Oct 16 Python
通过实例简单了解Python中yield的作用
Dec 11 Python
Python装饰器用法与知识点小结
Mar 09 Python
python 实现表情识别
Nov 21 Python
python 基于DDT实现数据驱动测试
Feb 18 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
人大复印资料处理程序_补充篇
2006/10/09 PHP
PHP操作XML作为数据库的类
2010/12/19 PHP
php采用curl访问域名返回405 method not allowed提示的解决方法
2014/06/26 PHP
PHP中addslashes与mysql_escape_string的区别分析
2016/04/25 PHP
基于PHP常用文件函数和目录函数整理
2017/08/17 PHP
jQuery 使用手册(三)
2009/09/23 Javascript
jQuery获取样式中的背景颜色属性值/颜色值
2012/12/17 Javascript
jQuery 中国省市两级联动选择附图
2014/05/14 Javascript
javascript的document.referrer浏览器支持、失效情况总结
2014/07/18 Javascript
基于jQuery实现淡入淡出效果轮播图
2020/07/31 Javascript
巧用Vue.js+Vuex制作专门收藏微信公众号的app
2016/11/03 Javascript
jQuery弹出层插件popShow(改进版)用法示例
2017/01/23 Javascript
JS异步宏队列与微队列原理区别详解
2020/07/02 Javascript
django框架CSRF防护原理与用法分析
2019/07/22 Python
Python爬取智联招聘数据分析师岗位相关信息的方法
2019/08/13 Python
详解python 中in 的 用法
2019/12/12 Python
使用Bazel编译TensorBoard教程
2020/02/15 Python
PyQt5 界面显示无响应的实现
2020/03/26 Python
Python实现一个论文下载器的过程
2021/01/18 Python
Python tkinter之ComboBox(下拉框)的使用简介
2021/02/05 Python
德国体育用品网上商店:SC24.com
2016/08/01 全球购物
伊莱克斯(Electrolux)俄罗斯网上商店:瑞典家用电器品牌
2021/01/23 全球购物
Ruby如何创建一个线程
2013/03/10 面试题
高中体育教学反思
2014/01/24 职场文书
海飞丝广告词
2014/03/20 职场文书
2014年秋季开学寄语
2014/08/02 职场文书
班级活动总结格式
2014/08/30 职场文书
学校施工安全责任书
2015/01/29 职场文书
武夷山导游词
2015/02/03 职场文书
琅琊山导游词
2015/02/05 职场文书
材料员岗位职责
2015/02/10 职场文书
小学生勤俭节约倡议书
2015/04/29 职场文书
生日赠语
2015/06/23 职场文书
中秋节英文祝福语句(14句)
2019/09/11 职场文书
Python 如何将integer转化为罗马数(3999以内)
2021/06/05 Python
各国货币符号大全
2022/02/17 杂记