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上關於該方法的使用

 1 Watcher
 2 You can register watchers to perform some actions when a selector does not find a match.
 3 
 4 Register Watcher
 5 
 6 When a selector can not find a match, uiautomator2 will run all registered watchers.
 7 
 8 Click target when conditions match
 9 d.watcher("AUTO_FC_WHEN_ANR").when(text="ANR").when(text="Wait") \
10                              .click(text="Force Close")
11 # d.watcher(name) ## creates a new named watcher.
12 #  .when(condition)  ## the UiSelector condition of the watcher.
13 #  .click(target)  ## perform click action on the target UiSelector.
14 There is also a trick about click. You can use click without arguments.
15 
16 d.watcher("ALERT").when(text="OK").click()
17 # Same as
18 d.watcher("ALERT").when(text="OK").click(text="OK")
19 Press key when a condition becomes true
20 d.watcher("AUTO_FC_WHEN_ANR").when(text="ANR").when(text="Wait") \
21                              .press("back", "home")
22 # d.watcher(name) ## creates a new named watcher.
23 #  .when(condition)  ## the UiSelector condition of the watcher.
24 #  .press(<keyname>, ..., <keyname>.()  ## press keys one by one in sequence.
25 Check if the named watcher triggered
26 
27 A watcher is triggered, which means the watcher was run and all its conditions matched.
28 
29 d.watcher("watcher_name").triggered
30 # true in case of the specified watcher triggered, else false
31 Remove a named watcher
32 
33 # remove the watcher
34 d.watcher("watcher_name").remove()
35 List all watchers
36 
37 d.watchers
38 # a list of all registered watchers
39 Check for any triggered watcher
40 
41 d.watchers.triggered
42 #  true in case of any watcher triggered
43 Reset all triggered watchers
44 
45 # reset all triggered watchers, after that, d.watchers.triggered will be false.
46 d.watchers.reset()
47 Remove watchers
48 
49 # remove all registered watchers
50 d.watchers.remove()
51 # remove the named watcher, same as d.watcher("watcher_name").remove()
52 d.watchers.remove("watcher_name")
53 Force to run all watchers
54 
55 # force to run all registered watchers
56 d.watchers.run()

注:里面涉及的watcher_name可以自定義,可以做到見名知意即可

watcher的使用是要先注冊(第9行至20行均是注冊watcher的方法),然后激活watcher(第56行),注意這個激活方法只是一個瞬時激活,就是說使用之后即銷毀,不會一直存於后台。那這樣的話在實際的使用場景中怎么使用這個功能呢,下面看一段腳本 1 # -*- coding:utf-8 -*-

 2 
 3 import uiautomator2 as u2  4 import time  5 
 6 
 7 d = u2.connect()  8 cfg = MTBFConfig()  9 package = cfg.getstr("Admit", "pkg", "config") 10 PACKAGELIST = package.split(",") 11 print(PACKAGELIST) 12 d.watcher("‎‏‎‎‎‎‎‏‎‏‏‏‎‎‎‎‎‏‎‎‏‎‎‎‎‏‏‏‏‏‏‏‏‎‎‎‎‎‎‎‎‎‏‎‎‎‏‎‎‏‏‎‏‎‏‎‎‎‏‏‏‎‏‏‏‎‏‏‎‎‎‏‏‏‏‏‏‏‎‎‏‎‎‏‏‎‎‎‏‎‎‎‏‏‏‎ALLOW‎‏‎‎‏‎").when(text="‎‏‎‎‎‎‎‏‎‏‏‏‎‎‎‎‎‏‎‎‏‎‎‎‎‏‏‏‏‏‏‏‏‎‎‎‎‎‎‎‎‎‏‎‎‎‏‎‎‏‏‎‏‎‏‎‎‎‏‏‏‎‏‏‏‎‏‏‎‎‎‏‏‏‏‏‏‏‎‎‏‎‎‏‏‎‎‎‏‎‎‎‏‏‏‎ALLOW‎‏‎‎‏‎").click(text="‎‏‎‎‎‎‎‏‎‏‏‏‎‎‎‎‎‏‎‎‏‎‎‎‎‏‏‏‏‏‏‏‏‎‎‎‎‎‎‎‎‎‏‎‎‎‏‎‎‏‏‎‏‎‏‎‎‎‏‏‏‎‏‏‏‎‏‏‎‎‎‏‏‏‏‏‏‏‎‎‏‎‎‏‏‎‎‎‏‎‎‎‏‏‏‎ALLOW‎‏‎‎‏‎") 13 #d.watchers.run()
14 print(d.watchers) 15 
16 time.sleep(2) 17 pkglen = len(PACKAGELIST) 18 print(("There are %d package for test") %pkglen) 19 
20 class Admit(object): 21 
22     def main(self): 23         for i in range(pkglen): 24             k = 0 25             for j in range(5): 26                 if d.info['currentPackageName'] != PACKAGELIST[i]: 27  d.app_start(PACKAGELIST[i]) 28                     print(PACKAGELIST[i]) 29                     time.sleep(1) 30                     k += 1
31                 if k == 3: 32                     print("Can not enter "+ str(PACKAGELIST[i])) 33                     return False 34             if PACKAGELIST[i] == 'com.google.android.contacts': 35                 print("hello") 36                 if d(description = "Open navigation drawer").exists(timeout = 5): 37                     d(description = "Open navigation drawer").click() 38                     
39                 if d(text = "Settings").exists(timeout = 5): 40                     d(text = "Settings").click() 41                     
42                 if d(resourceId="android:id/title", text = "Import").exists(timeout=5): 43                     d(resourceId="android:id/title", text = "Import").click() 44                     time.sleep(3) 45 
46                 if d(resourceId = "android:id/button1", text = "OK").exists(timeout = 5): 47                     d(resourceId = "android:id/button1", text = "OK").click() 48                     time.sleep(1) 49  d.watchers.run() //在上面OK點擊之后會彈出一個權限訪問的許可,所以這個時候需要激活一次watcher把彈框關掉,以便不影響后續測試,所以就一個原則,哪里可能會有彈框就在哪里激活watcher 50 
51             
52 
53 if __name__=="__main__": 54     ad = Admit() 55     ad.main()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM