2
# Copyright (c) 2010 Canonical
4
# Written by Gustavo Niemeyer <gustavo@niemeyer.net>
6
# This file is part of the Xpresser GUI automation library.
8
# Xpresser is free software; you can redistribute it and/or modify
9
# it under the terms of the GNU Lesser General Public License version 3,
10
# as published by the Free Software Foundation.
12
# Xpresser is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Lesser General Public License for more details.
17
# You should have received a copy of the GNU Lesser General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
22
from xpresser import xutils
23
from xpresser.image import Image
24
from xpresser.errors import XpresserError
25
from xpresser.imagedir import ImageDir
26
from xpresser.imagematch import ImageMatch
27
from xpresser.opencvfinder import OpenCVFinder
30
class ImageNotFound(XpresserError):
31
"""Exception raised when a request to find an image doesn't succeed."""
34
class Xpresser(object):
37
self._imagedir = ImageDir()
38
self._imagefinder = OpenCVFinder()
40
def load_images(self, path):
41
self._imagedir.load(path)
43
def get_image(self, name):
44
return self._imagedir.get(name)
46
def _compute_focus_point(self, args):
47
if (len(args) == 2 and
48
isinstance(args[0], (int, long)) and
49
isinstance(args[1], (int, long))):
52
if type(args[0]) == ImageMatch:
55
match = self.find(args[0])
56
return match.focus_point
58
def click(self, *args):
59
"""Click on the position specified by the provided arguments.
61
The following examples show valid ways of specifying the position:
63
xp.click("image-name")
67
xutils.click(*self._compute_focus_point(args))
69
def right_click(self, *args):
70
"""Right-click on the position specified by the provided arguments.
72
The following examples show valid ways of specifying the position:
74
xp.right_click("image-name")
75
xp.right_click(image_match)
78
xutils.right_click(*self._compute_focus_point(args))
80
def double_click(self, *args):
81
'''Double clicks over the position specified by arguments
83
The following examples show valid ways of specifying te position:
84
xp.double_click("image-name")
85
xp.double_click(image_match)
88
xutils.double_click(*self._compute_focus_point(args))
90
def hover(self, *args):
91
"""Hover over the position specified by the provided arguments.
93
The following examples show valid ways of specifying the position:
95
xp.hover("image-name")
99
xutils.hover(*self._compute_focus_point(args))
101
def find(self, image, timeout=10):
102
"""Given an image or an image name, find it on the screen.
104
@param image: Image or image name to be searched for.
105
@return: An ImageMatch instance, or None.
107
if isinstance(image, basestring):
108
image = self._imagedir.get(image)
109
wait_until = time.time() + timeout
110
while time.time() < wait_until:
111
screenshot_image = xutils.take_screenshot()
112
match = self._imagefinder.find(screenshot_image, image)
113
if match is not None:
115
raise ImageNotFound(image)
117
def wait(self, image, timeout=30):
118
"""Wait for an image to show up in the screen up to C{timeout} seconds.
120
@param image: Image or image name to be searched for.
121
@return: An ImageMatch instance, or None.
123
self.find(image, timeout)
125
def type(self, string):
126
"""Enter the string provided as if it was typed via the keyboard.