当前位置: 首页 > 工具软件 > Gmail Watcher > 使用案例 >

python uiautomator2 watcher的使用方法

史阳晖
2023-12-01

该方是基于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()

 

转载于:https://www.cnblogs.com/aziji/p/11491703.html

 类似资料: