This doc is showing how Snippet UiAutomator implement the UiWatcher to handle unexpected object on the screen.
Support to click on object, press key code, or swipe on object.
Define what the UiWatcher should do when Selector cannot find a match and the specific object shows in screen.
ad.ui.watcher('ITSELF').when(text='Something Pop Up').click()
ad.ui.watcher('OTHER').when(text='Something Pop Up').click(text='Click Me')
ad.ui.watcher('COORDINATES').when(text='Something Pop Up').click(x=50, y=100)
# Press HOME key
ad.ui.watcher('KEYCODE').when(text='Something Pop Up').press(uiautomator.KeyEvent.KEYCODE_HOME)
# Press BACK key then HOME key
ad.ui.watcher('KEYCODES').when(text='Something Pop Up').press(
uiautomator.KeyEvent.KEYCODE_BACK,
uiautomator.KeyEvent.KEYCODE_HOME,
)
ad.ui.watcher('ITSELF').when(text='Something Pop Up').swipe(Direction.UP, percent=100, speed=1000)
ad.ui.watcher('OTHER').when(text='Something Pop Up').swipe(Direction.LEFT, percent=50, speed=1000, text='Swipe me')
Check whether an UiWatcher is triggered or not, which means the UiWatcher was run and its conditions matched.
>>> ad.ui.watcher('ITSELF').triggered
False # Default is False, True if it is triggered at least once.
>>> ad.ui.watchers.triggered
False # Default is False, True if any UiWatcher is triggered.
Get the names of all registered UiWatchers.
>>> ad.ui.watchers
['ITSELF', 'OTHER', 'KEYCODE', 'KEYCODES']
When you don't need a registered UiWatcher, you can remove it.
ad.ui.watcher('ITSELF').remove()
ad.ui.watchers.remove('ITSELF')
ad.ui.watchers.remove()
After an UiWatcher is triggered, its status will always be True
unless reset
it.
Important: When call this method, all UiWatchers will be reset, there is no way to recover.
ad.ui.watchers.reset()
Force all registered UiWatchers to run.
ad.ui.watchers.run()