該方是基于uiautomator2如下版本進(jìn)行驗(yàn)證的:
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上關(guān)于該方法的使用
?
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(
, ...,
.() ## 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可以自定義,可以做到見(jiàn)名知意即可
watcher的使用是要先注冊(cè)(第9行至20行均是注冊(cè)watcher的方法),然后激活watcher(第56行),注意這個(gè)激活方法只是一個(gè)瞬時(shí)激活,就是說(shuō)使用之后即銷(xiāo)毀,不會(huì)一直存于后臺(tái)。那這樣的話在實(shí)際的使用場(chǎng)景中怎么使用這個(gè)功能呢,下面看一段腳本 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點(diǎn)擊之后會(huì)彈出一個(gè)權(quán)限訪問(wèn)的許可,所以這個(gè)時(shí)候需要激活一次watcher把彈框關(guān)掉,以便不影響后續(xù)測(cè)試,所以就一個(gè)原則,哪里可能會(huì)有彈框就在哪里激活watcher
if __name__=="__main__":
ad = Admit()
ad.main()
總結(jié)
以上所述是小編給大家介紹的python uiautomator2 watcher的使用方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

