详解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根据开头和结尾字符串获取中间字符串的方法
Mar 26 Python
Python实现将不规范的英文名字首字母大写
Nov 15 Python
基于pandas将类别属性转化为数值属性的方法
Jul 25 Python
python 文本单词提取和词频统计的实例
Dec 22 Python
python celery分布式任务队列的使用详解
Jul 08 Python
Django REST框架创建一个简单的Api实例讲解
Nov 05 Python
Python调用graphviz绘制结构化图形网络示例
Nov 22 Python
win10下opencv-python特定版本手动安装与pip自动安装教程
Mar 05 Python
Flask缓存静态文件的具体方法
Aug 02 Python
浅谈Python中的函数(def)及参数传递操作
May 25 Python
给numpy.array增加维度的超简单方法
Jun 02 Python
Python实现简单得递归下降Parser
May 02 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
怎样在UNIX系统下安装php3
2006/10/09 PHP
php生成扇形比例图实例
2013/11/06 PHP
PHP SPL标准库中的常用函数介绍
2015/05/11 PHP
php实现多城市切换特效
2015/08/09 PHP
javascript,jquery闭包概念分析
2010/06/19 Javascript
Javascript this 的一些学习总结
2012/08/02 Javascript
js取得url地址参数实例
2013/02/22 Javascript
javascript实现文字图片上下滚动的具体实例
2013/06/28 Javascript
一款jquery特效编写的大度宽屏焦点图切换特效的实例代码
2013/08/05 Javascript
javascript常用函数归纳整理
2014/10/31 Javascript
JavaScript实现添加、查找、删除元素
2015/07/02 Javascript
jQuery实现的AJAX简单弹出层效果代码
2015/11/26 Javascript
Web前端开发工具——bower依赖包管理工具
2016/03/29 Javascript
深入浅析JavaScript中的arguments对象(强力推荐)
2016/06/03 Javascript
利用Angularjs实现幻灯片效果
2016/09/07 Javascript
javascript判断回文数详解及实现代码
2017/02/03 Javascript
BootStrap select2 动态改变值的方法
2017/02/10 Javascript
Vue渲染函数详解
2017/09/15 Javascript
nodejs初始化init的示例代码
2018/10/10 NodeJs
详解Node.JS模块 process
2020/08/31 Javascript
如何在 ant 的table中实现图片的渲染操作
2020/10/28 Javascript
python操作MySQL数据库具体方法
2013/10/28 Python
深入解析Python中的变量和赋值运算符
2015/10/12 Python
Python及PyCharm下载与安装教程
2017/11/18 Python
python的列表List求均值和中位数实例
2020/03/03 Python
荷兰男士时尚网上商店:Suitable
2017/12/25 全球购物
Jacadi Paris英国官网:法国童装品牌
2019/08/09 全球购物
机关单位动员会主持词
2014/03/20 职场文书
机械设计及其自动化专业求职信
2014/06/09 职场文书
社区志愿者服务心得体会
2016/01/22 职场文书
读《教育心理学》心得体会
2016/01/22 职场文书
《中国古代诗歌散文欣赏》高中语文教材
2019/08/20 职场文书
导游词之临安白水涧
2019/11/05 职场文书
MySQL 自定义变量的概念及特点
2021/05/13 MySQL
MySQL注入基础练习
2021/05/30 MySQL
HTML中link标签属性的具体用法
2023/05/07 HTML / CSS