详解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中对列表排序实例
Jan 04 Python
python获取指定网页上所有超链接的方法
Apr 04 Python
python 转换 Javascript %u 字符串为python unicode的代码
Sep 06 Python
教你用Python写安卓游戏外挂
Jan 11 Python
python 通过字符串调用对象属性或方法的实例讲解
Apr 21 Python
python3实现点餐系统
Jan 24 Python
Python TCP通信客户端服务端代码实例
Nov 21 Python
手把手教你安装Windows版本的Tensorflow
Mar 26 Python
不到20行实现Python代码即可制作精美证件照
Apr 24 Python
Python GUI之tkinter窗口视窗教程大集合(推荐)
Oct 20 Python
Python爬虫新手入门之初学lxml库
Dec 20 Python
Python 数据科学 Matplotlib图库详解
Jul 07 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调用mysql数据 dbclass类
2011/05/07 PHP
PHP中获取内网用户MAC地址(WINDOWS/linux)的实现代码
2011/08/11 PHP
使用JSON实现数据的跨域传输的php代码
2011/12/20 PHP
PHP加密解密类实例分析
2015/04/20 PHP
php 猴子摘桃的算法
2017/06/20 PHP
PHP对称加密算法(DES/AES)类的实现代码
2017/11/14 PHP
php+redis实现商城秒杀功能
2020/11/19 PHP
php生成复杂验证码(倾斜,正弦干扰线,黏贴,旋转)
2018/03/12 PHP
javascript 树形导航菜单实例代码
2013/08/13 Javascript
Javascript实现页面跳转的几种方式分享
2013/10/26 Javascript
jQuery实现点击文本框弹出热门标签的提示效果
2013/11/17 Javascript
Nginx上传文件全部缓存解决方案
2015/08/17 Javascript
javascript仿京东导航左侧分类导航下拉菜单效果
2020/11/25 Javascript
JavaScript数据操作_浅谈原始值和引用值的操作本质
2016/08/23 Javascript
微信小程序 登陆流程详细介绍
2017/01/17 Javascript
利用Javascript开发一个二维周视图日历
2017/12/14 Javascript
js实现简单模态框实例
2018/11/16 Javascript
Vue axios全局拦截 get请求、post请求、配置请求的实例代码
2018/11/28 Javascript
微信小程序实现日历签到
2020/09/21 Javascript
vue+Element-ui实现分页效果
2020/11/15 Javascript
vue 递归组件的简单使用示例
2021/01/14 Vue.js
vue.js实现点击图标放大离开时缩小的代码
2021/01/27 Vue.js
用Python编写一个基于终端的实现翻译的脚本
2015/04/24 Python
Django实现全文检索的方法(支持中文)
2018/05/14 Python
python实现淘宝秒杀脚本
2020/06/23 Python
5款Python程序员高频使用开发工具推荐
2019/04/10 Python
pycharm双击无响应(打不开问题解决办法)
2020/01/10 Python
浅谈three.js中的needsUpdate的应用
2012/11/12 HTML / CSS
蔻驰意大利官网:COACH意大利
2019/01/16 全球购物
中国梦演讲稿教师篇
2014/04/23 职场文书
公共场所标语
2014/06/30 职场文书
创先争优个人承诺书
2014/08/30 职场文书
万能检讨书2000字
2014/10/17 职场文书
政工师工作总结2015
2015/05/26 职场文书
监护人证明
2015/06/19 职场文书
超越Nginx的Web服务器caddy优雅用法
2022/06/21 Servers