Skip to content
This repository has been archived by the owner on Mar 1, 2019. It is now read-only.

Commit

Permalink
watch object add func:do
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Mar 18, 2016
1 parent b24f64d commit e3b750c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,16 @@ except atx.ImageNotFoundError:
print('Image not found')

# watcher, trigger when screenshot is called
def foo(event):
print 'It happens', event
d.click(*event.pos)

timeout = 50 # 50s
with d.watch('enter game', timeout) as w:
w.on('enter-game').click()
w.on('inside.png').quit()
w.on(text='Login').quit() # UI Component
w.on('outside.png').do(foo)

# click by UI component
d(text='Enter').click()
Expand Down
38 changes: 31 additions & 7 deletions atx/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import threading
import warnings
import logging
import functools
import xml.dom.minidom

import cv2
Expand Down Expand Up @@ -105,7 +106,8 @@ class Watcher(object):
ACTION_TOUCH = 1 <<0
ACTION_QUIT = 1 <<1

Event = collections.namedtuple('WatchEvent', ['selector', 'actions'])
Handler = collections.namedtuple('Handler', ['selector', 'action'])
Event = collections.namedtuple('Event', ['selector', 'pos'])

def __init__(self, device, name=None, timeout=None):
self._events = []
Expand Down Expand Up @@ -138,11 +140,32 @@ def touch(self):
return self.click()

def click(self):
self._events.append(Watcher.Event(self._stored_selector, Watcher.ACTION_CLICK))
self._events.append(Watcher.Handler(self._stored_selector, Watcher.ACTION_CLICK))
return self

def quit(self):
self._events.append(Watcher.Event(self._stored_selector, Watcher.ACTION_QUIT))
self._events.append(Watcher.Handler(self._stored_selector, Watcher.ACTION_QUIT))

def do(self, func):
"""Trigger with function call
Args:
func: function which will called when object found. For example.
def foo(event):
print event.pos # (x, y) position
w.on('kitty.png').do(foo)
Returns:
Watcher object
Raises:
SyntaxError
"""
if not callable(func):
raise SyntaxError("%s should be a function" % func)
self._events.append(Watcher.Handler(self._stored_selector, func))
return self

def __enter__(self):
return self
Expand Down Expand Up @@ -172,11 +195,12 @@ def _hook(self, screen):
if pos is None:
continue

if evt.actions & Watcher.ACTION_CLICK:
log.debug('trigger watch click: %s', pos)
if callable(evt.action):
evt.action(Watcher.Event(evt.selector, pos))
elif evt.action == Watcher.ACTION_CLICK:
log.info('trigger watch click: %s', pos)
self._dev.click(*pos)

if evt.actions & Watcher.ACTION_QUIT:
elif evt.action == Watcher.ACTION_QUIT:
self._run = False

def _run_watch(self):
Expand Down
9 changes: 7 additions & 2 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ def touch():
if __name__ == '__main__':
log = logging.getLogger('atx')
log.setLevel(logging.DEBUG)

def foo(evt):
print 'good', evt
d.click(*evt.pos)

with d.watch('simulator', 10) as w:
w.on(atx.Pattern("mmm.png", offset=(-79, -13))).click()
w.on(atx.Pattern("mmm.png", offset=(-79, -13))).do(foo).quit()
# # stop_app()
#print 'inside'
#screenshot()
print d.dump_nodes()
# print d.dump_nodes()
# w.on('setting.png', atx.Watcher.ACTION_TOUCH)
# w.on('common.png', atx.Watcher.ACTION_TOUCH)

Expand Down

0 comments on commit e3b750c

Please sign in to comment.