详解python uiautomator2 watcher的使用方法
作者:aziji 发布时间:2022-03-04 07:31:23
标签:python,uiautomator2,watcher
该方是基于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
0
投稿
猜你喜欢
- __str__函数如果定义了该函数,当print当前实例化对象的时候,会返回该函数的return信息可用于定义当前类的描述信息用法:def
- 看代码吧~name = input('Name') height = input('Height(m):')
- 部署网站前查看一下系统是否已经安装CGI1、启动iis服务器,打开IIS服务器打开IIS服务器,点击网站,右击“添加网站”2、创建网站点击“
- //-------------------------------------------- // 删除千分点。 //-----------
- # -*- encoding: utf8 -*-import osimport sysimport ftplibclass FTPSync(
- RDPY 是基于 Twisted Python 实现的微软 RDP 远程桌面协议。RDPY 提供了如下 RDP 和 VNC 支持:●RDP
- python的smtplib提供了一种很方便的途径发送电子邮件。它对smtp协议进行了简单的封装。下面是一个利用smtplib,实现QQ邮箱
- 在项目中发现这样一个问题:sqlserver数据库编码为gbk,使用python3.4+pymssql 查询,中文乱码,经过一番思考问题解决
- 前言在进行图像处理时,经常会用到读取图片并显示出来这样的操作,所以本文总结了python中读取并显示图片的3种方式,分别基于opencv、m
- 今天使用python写了一个简单的爬虫,用来下载taptap网站的游戏截图。下面说下具体的实现方法。在搜索框中搜索“原神”打开浏览器的开发者
- 二元函数为y=x1^2+x2^2,x∈[-5,5]NIND=121; %初始种群的个数(Number of individual
- 我就废话不多说了,直接上代码吧!import turtleturtle.pensize(5)turtle.pencolor("ye
- 在 Go 语言中,map 是一种非常常见的数据类型,它可以用于快速地检索数据。Go 语言中的 map 与其他编程语言中的类似的数据类型相比,
- 在新旧版的torch中的傅里叶变换函数在定义和用法上存在不同,记录一下。1、旧版fft = torch.rfft(input, 2, nor
- 在用Matplotlib库绘制折线图的时候遇到一个问题,当定义一个x轴数组时,plot绘制折线图时,x轴并不会按照我们定义的数组的顺序去排列
- 1、模拟退火算法退火是金属从熔融状态缓慢冷却、最终达到能量最低的平衡态的过程。模拟退火算法基于优化问题求解过程与金属退火过程的相似性,以优化
- 前言:之前的文章我们已经开启了爬虫程序的exe之旅,但是我们最终实现的程序存在一个非常大的问题,当进行网络请求的时候,程序卡死,直到数据请求
- 包含Contains(s,substr string) bool判断s是否包含substr true:包含 false:不包含例子: &nb
- 一、背景:nginx 的log 不会自动按天备份,而且记录时间格式不统一,此程序专门解决这两个问题;二、windows 部署方式1.在 ng
- 简述由于某些原因,在使用 import 时,Python 找不到相应的模块。这时,解释器就会发牢骚 - ImportError。那么,Pyt