~chris.gagnon/xpresser/xpresser-with-emulate-click-and-drag

« back to all changes in this revision

Viewing changes to debian/python-xpresser/usr/share/pyshared/xpresser/xp.py

  • Committer: Chris Wayne
  • Date: 2012-07-05 15:56:48 UTC
  • Revision ID: chris.wayne@canonical.com-20120705155648-5kmc3gih12slvr7y
Adding debian/copyright

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# Copyright (c) 2010 Canonical
3
 
#
4
 
# Written by Gustavo Niemeyer <gustavo@niemeyer.net>
5
 
#
6
 
# This file is part of the Xpresser GUI automation library.
7
 
#
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.
11
 
#
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.
16
 
#
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/>.
19
 
#
20
 
import time
21
 
 
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
28
 
 
29
 
 
30
 
class ImageNotFound(XpresserError):
31
 
    """Exception raised when a request to find an image doesn't succeed."""
32
 
 
33
 
 
34
 
class Xpresser(object):
35
 
 
36
 
    def __init__(self):
37
 
        self._imagedir = ImageDir()
38
 
        self._imagefinder = OpenCVFinder()
39
 
 
40
 
    def load_images(self, path):
41
 
        self._imagedir.load(path)
42
 
 
43
 
    def get_image(self, name):
44
 
        return self._imagedir.get(name)
45
 
 
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))):
50
 
            return args
51
 
        elif len(args) == 1:
52
 
            if type(args[0]) == ImageMatch:
53
 
                match = args[0]
54
 
            else:
55
 
                match = self.find(args[0])
56
 
            return match.focus_point
57
 
 
58
 
    def click(self, *args):
59
 
        """Click on the position specified by the provided arguments.
60
 
 
61
 
        The following examples show valid ways of specifying the position:
62
 
        
63
 
            xp.click("image-name")
64
 
            xp.click(image_match)
65
 
            xp.click(x, y)
66
 
        """
67
 
        xutils.click(*self._compute_focus_point(args))
68
 
 
69
 
    def right_click(self, *args):
70
 
        """Right-click on the position specified by the provided arguments.
71
 
 
72
 
        The following examples show valid ways of specifying the position:
73
 
        
74
 
            xp.right_click("image-name")
75
 
            xp.right_click(image_match)
76
 
            xp.right_click(x, y)
77
 
        """
78
 
        xutils.right_click(*self._compute_focus_point(args))
79
 
 
80
 
    def double_click(self, *args):
81
 
        '''Double clicks over the position specified by arguments
82
 
 
83
 
        The following examples show valid ways of specifying te position:
84
 
             xp.double_click("image-name")
85
 
             xp.double_click(image_match)
86
 
             xp.double_click(x, y)
87
 
        '''
88
 
        xutils.double_click(*self._compute_focus_point(args))
89
 
            
90
 
    def hover(self, *args):
91
 
        """Hover over the position specified by the provided arguments.
92
 
 
93
 
        The following examples show valid ways of specifying the position:
94
 
        
95
 
            xp.hover("image-name")
96
 
            xp.hover(image_match)
97
 
            xp.hover(x, y)
98
 
        """
99
 
        xutils.hover(*self._compute_focus_point(args))
100
 
 
101
 
    def find(self, image, timeout=10):
102
 
        """Given an image or an image name, find it on the screen.
103
 
 
104
 
        @param image: Image or image name to be searched for.
105
 
        @return: An ImageMatch instance, or None.
106
 
        """
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:
114
 
                return match
115
 
        raise ImageNotFound(image)
116
 
 
117
 
    def wait(self, image, timeout=30):
118
 
        """Wait for an image to show up in the screen up to C{timeout} seconds.
119
 
 
120
 
        @param image: Image or image name to be searched for.
121
 
        @return: An ImageMatch instance, or None.
122
 
        """
123
 
        self.find(image, timeout)
124
 
 
125
 
    def type(self, string):
126
 
        """Enter the string provided as if it was typed via the keyboard.
127
 
        """
128
 
        xutils.type(string)