python uiautomator2 watcher to use

The verification party is based on the following uiautomator2 Version:

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: [email protected]
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

  Posted below about using this method on 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()

Note: watcher_name involved inside can be customized, can be done to see the name EENOW

watcher use is first registered (lines 9-20 row is a method of registering watcher), then activate watcher (line 56), note that this is just a transient activation activation method, that is that is destroyed after use, it would not have been He kept in the background. If that happens in the actual use how to use this feature scenes it, look at the following piece of script  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 = " the OK " ) .exists (= timeout. 5 ):
 47                      D (The resourceId = " Android: ID / Button1 " , text = " the OK " ) .click ()
 48                      the time.sleep (. 1 )
 49                      d.watchers.run () // after clicking OK will pop up a top access permission, so this time needs to be activated once watcher box to turn off the bomb, so as not to affect subsequent tests, so a principle, wherever possible will be playing in the frame where the activation Watcher
 50  
51 is              
52 is  
53 is  IF  the __name__ == " __main__ " :
 54 is      AD = Admit ()
 55     ad.main()

 

Guess you like

Origin www.cnblogs.com/aziji/p/11491703.html