详解python uiautomator2 watcher的使用方法

作者:aziji 时间:2022-03-04 07:31:23 

该方是基于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的使用方法,网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

来源:https://www.cnblogs.com/aziji/archive/2019/09/09/11491703.html

标签:python,uiautomator2,watcher
0
投稿

猜你喜欢

  • 在Django的session中使用User对象的方法

    2022-04-23 01:20:07
  • PHP中__LINE__,__FILE__,__DIR__等常用魔术常量实例讲解

    2023-06-13 23:42:32
  • Django读取Mysql数据并显示在前端的实例

    2023-11-09 17:36:49
  • conda创建环境、安装包、删除环境步骤详细记录

    2022-07-03 07:06:51
  • windows系统下让mysql支持federated的storage engine

    2010-01-20 11:16:00
  • Flask实现跨域请求的处理方法

    2022-03-18 08:19:07
  • 解析python高级异常和运算符重载

    2021-06-17 00:56:41
  • Golang正整数指定规则排序算法问题分析

    2023-07-12 09:12:03
  • 拓扑排序Python实现的过程

    2021-10-23 13:57:44
  • Python3爬虫爬取英雄联盟高清桌面壁纸功能示例【基于Scrapy框架】

    2023-03-07 19:19:59
  • python用来获得图片exif信息的库实例分析

    2022-07-16 12:29:38
  • 三种不同方式连接MySQL数据库的方法及示例

    2010-06-11 13:37:00
  • numpy 声明空数组详解

    2022-09-20 15:29:44
  • 一些关于SQL2005+ASP.NET2.0的问题

    2007-09-23 13:01:00
  • SQL Server连接失败错误及解决

    2008-01-28 21:09:00
  • Python实现文件按照日期命名的方法

    2022-10-25 19:40:09
  • 变态输入框——再谈校验包容性(一)

    2009-10-10 13:23:00
  • python3爬取torrent种子链接实例

    2022-03-05 11:23:44
  • python分块读取大数据,避免内存不足的方法

    2022-09-30 13:05:17
  • 网页代码更清晰高效的一些经验

    2008-05-19 12:23:00
  • asp之家 网络编程 m.aspxhome.com