Skip to content

Commit

Permalink
Added reference folder support as described in issue #15
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Suonto committed Apr 14, 2016
1 parent f8f9c95 commit e38cef5
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 16 deletions.
69 changes: 53 additions & 16 deletions src/ImageHorizonLibrary/recognition/_recognize_images.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from os import listdir
from os.path import abspath, isdir, isfile, join as path_join
from time import time
from contextlib import contextmanager
Expand All @@ -12,7 +13,7 @@

class _RecognizeImages(object):

def __normalize(self, path):
def __normalize(self, path, is_folder=False):
if (not self.reference_folder or
not isinstance(self.reference_folder, basestring) or
not isdir(self.reference_folder)):
Expand All @@ -21,11 +22,11 @@ def __normalize(self, path):
if (not path or not isinstance(path, basestring)):
raise InvalidImageException('"%s" is invalid image name.' % path)
path = unicode(path.lower().replace(' ', '_'))
if not path.endswith('.png'):
if not path.endswith('.png') and not is_folder:
path += '.png'
path = abspath(path_join(self.reference_folder, path))
if not isfile(path):
raise ImageNotFoundException(path)
if not isfile(path) and not (is_folder and isdir(path)):
raise InvalidImageException('Image path not found: "%s".' % path)
return path

def click_image(self, reference_image):
Expand Down Expand Up @@ -178,28 +179,64 @@ def locate(self, reference_image):
return self._locate(reference_image)

def wait_for(self, reference_image, timeout=10):
'''Tries to locate given image from the screen for given time.
'''Operates on a single image if ``reference_image`` is a file. If
``reference_image`` is a folder, operates on all images in the folder.
Fail if the image is not found on the screen after ``timeout`` has
expired.
For each reference image:
Tries to locate given image from the screen until ``timeout`` occurs.
Fail if the image or none of images in target folder is found.
See `Reference image names` for documentation for ``reference_image``.
``timeout`` is given in seconds.
Returns Python tuple ``(x, y)`` of the coordinates.
'''
stop_time = time() + int(timeout)
is_dir = False
try:
if isdir(self.__normalize(reference_image, is_folder=True)):
is_dir = True
except InvalidImageException:
pass
is_file = False
try:
if isfile(self.__normalize(reference_image)):
is_file = True
except InvalidImageException:
pass

reference_images = []
if is_file:
reference_images = [reference_image]
elif is_dir:
for f in listdir(self.__normalize(reference_image, is_folder=True)):
if not isfile(self.__normalize(path_join(reference_image, f))):
raise InvalidImageException(
self.__normalize(reference_image),
is_folder=True)
reference_images.append(path_join(reference_image, f))

def try_locate(ref_image):
stop_time = time() + int(timeout)
location = None
with self._suppress_keyword_on_failure():
while time() < stop_time:
try:
location = self._locate(ref_image, log_it=False)
break
except ImageNotFoundException:
pass
return location

location = None
with self._suppress_keyword_on_failure():
while time() < stop_time:
try:
location = self._locate(reference_image, log_it=False)
break
except ImageNotFoundException:
pass
for ref_image in reference_images:
location = try_locate(ref_image)
if location != None:
break
if location is None:
self._run_on_failure()
raise ImageNotFoundException(self.__normalize(reference_image))
raise ImageNotFoundException(self.__normalize(reference_image,
is_folder=True))
LOGGER.info('Image "%s" found at %r' % (reference_image, location))
return location
5 changes: 5 additions & 0 deletions tests/atest/mac_tests.robot
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Test open and close application
Wait for calculator_active
Terminate application

Test folder as reference image
Launch application open -a Calculator alias=My calculator
Wait for folder_calculator_active
Terminate application My calculator

Test calculator
Launch application open -a Calculator
Wait for calculator active
Expand Down
Binary file modified tests/atest/reference_images/mac/calculator_active.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e38cef5

Please sign in to comment.