This package provides input methods for various platforms. Autopilot aims to provide an appropriate implementation for the currently running system. For example, not all systems have an X11 stack running: on those systems, autopilot will instantiate input classes class that use something other than X11 to generate events (possibly UInput).
Test authors should instantiate the appropriate class using the create method on each class. Calling create() with no arguments will get an instance of the specified class that suits the current platform. In this case, autopilot will do it’s best to pick a suitable backend. Calling create with a backend name will result in that specific backend type being returned, or, if it cannot be created, an exception will be raised. For more information on creating backends, see Advanced Backend Picking
There are three basic input types available:
The Pointer class is a wrapper that unifies the API of the Mouse and Touch classes, which can be helpful if you want to write a test that can use either a mouse of a touch device. A common pattern is to use a Touch device when running on a mobile device, and a Mouse device when running on a desktop.
See also
Module autopilot.gestures Multitouch and gesture support for touch devices.
A simple keyboard device class.
The keyboard class is used to generate key events while in an autopilot test. This class should not be instantiated directly. To get an instance of the keyboard class, call create instead.
Get an instance of the Keyboard class.
For more infomration on picking specific backends, see Advanced Backend Picking
For details regarding backend limitations please see: Keyboard backend limitations
Warning
The OSK (On Screen Keyboard) backend option does not implement either press or release methods due to technical implementation details and will raise a NotImplementedError exception if used.
Parameters: | preferred_backend – A string containing a hint as to which backend you would like. Possible backends are:
|
---|---|
Raises : | RuntimeError if autopilot cannot instantate any of the possible backends. |
Raises : | RuntimeError if the preferred_backend is specified and is not one of the possible backends for this device class. |
Raises : | BackendException if the preferred_backend is set, but that backend could not be instantiated. |
Type into an input widget.
This context manager takes care of making sure a particular input_target UI control is selected before any text is entered.
Some backends extend this method to perform cleanup actions at the end of the context manager block. For example, the OSK backend dismisses the keyboard.
If the pointer argument is None (default) then either a Mouse or Touch pointer will be created based on the current platform.
An example of using the context manager (with an OSK backend):
from autopilot.input import Keyboard
text_area = self._launch_test_input_area()
keyboard = Keyboard.create('OSK')
with keyboard.focused_type(text_area) as kb:
kb.type("Hello World.")
self.assertThat(text_area.text, Equals("Hello World"))
# Upon leaving the context managers scope the keyboard is dismissed
# with a swipe
Send key press events only.
Parameters: |
|
---|---|
Raises : | NotImplementedError If called when using the OSK Backend. |
Warning
The OSK backend does not implement the press method and will raise a NotImplementedError if called.
Example:
>>> press('Alt+F2')
presses the ‘Alt’ and ‘F2’ keys, but does not release them.
Send key release events only.
Parameters: |
|
---|---|
Raises : | NotImplementedError If called when using the OSK Backend. |
Warning
The OSK backend does not implement the press method and will raise a NotImplementedError if called.
Example:
>>> release('Alt+F2')
releases the ‘Alt’ and ‘F2’ keys.
Press and release all items in ‘keys’.
This is the same as calling ‘press(keys);release(keys)’.
Parameters: |
|
---|
Example:
>>> press_and_release('Alt+F2')
presses both the ‘Alt’ and ‘F2’ keys, and then releases both keys.
Simulate a user typing a string of text.
Parameters: |
|
---|
Note
Only ‘normal’ keys can be typed with this method. Control characters (such as ‘Alt’ will be interpreted as an ‘A’, and ‘l’, and a ‘t’).
A simple mouse device class.
The mouse class is used to generate mouse events while in an autopilot test. This class should not be instantiated directly however. To get an instance of the mouse class, call create instead.
For example, to create a mouse object and click at (100,50):
>>> mouse = autopilot.input.Mouse.create()
>>> mouse.move(100, 50)
>>> mouse.click()
Get an instance of the Mouse class.
For more infomration on picking specific backends, see Advanced Backend Picking
Parameters: | preferred_backend – A string containing a hint as to which backend you would like. Possible backends are:
|
---|---|
Raises : | RuntimeError if autopilot cannot instantate any of the possible backends. |
Raises : | RuntimeError if the preferred_backend is specified and is not one of the possible backends for this device class. |
Raises : | BackendException if the preferred_backend is set, but that backend could not be instantiated. |
Click the center point of a given object.
It does this by looking for several attributes, in order. The first attribute found will be used. The attributes used are (in order):
- globalRect (x,y,w,h)
- center_x, center_y
- x, y, w, h
Raises : | ValueError if none of these attributes are found, or if an attribute is of an incorrect type. |
---|
Moves mouse to location (x,y).
Callers should avoid specifying the rate or time_between_events parameters unless they need a specific rate of movement.
Attempts to move the mouse to ‘object_proxy’s centre point.
It does this by looking for several attributes, in order. The first attribute found will be used. The attributes used are (in order):
- globalRect (x,y,w,h)
- center_x, center_y
- x, y, w, h
Raises : | ValueError if none of these attributes are found, or if an attribute is of an incorrect type. |
---|
Performs a press, move and release.
This is to keep a common API between Mouse and Finger as long as possible.
A simple touch driver class.
This class can be used for any touch events that require a single active touch at once. If you want to do complex gestures (including multi-touch gestures), look at the autopilot.gestures module.
Get an instance of the Touch class.
Parameters: | preferred_backend – A string containing a hint as to which backend you would like. If left blank, autopilot will pick a suitable backend for you. Specifying a backend will guarantee that either that backend is returned, or an exception is raised. possible backends are:
|
---|---|
Raises : | RuntimeError if autopilot cannot instantate any of the possible backends. |
Raises : | RuntimeError if the preferred_backend is specified and is not one of the possible backends for this device class. |
Raises : | BackendException if the preferred_backend is set, but that backend could not be instantiated. |
Tap the center point of a given object.
It does this by looking for several attributes, in order. The first attribute found will be used. The attributes used are (in order):
- globalRect (x,y,w,h)
- center_x, center_y
- x, y, w, h
Raises : | ValueError if none of these attributes are found, or if an attribute is of an incorrect type. |
---|
A wrapper class that represents a pointing device which can either be a mouse or a touch, and provides a unified API.
This class is useful if you want to run tests with either a mouse or a touch device, and want to write your tests to use a single API. Create this wrapper by passing it either a mouse or a touch device, like so:
pointer_device = Pointer(Mouse.create())
or, like so:
pointer_device = Pointer(Touch.create())
Warning
Some operations only make sense for certain devices. This class attempts to minimise the differences between the Mouse and Touch APIs, but there are still some operations that will cause exceptions to be raised. These are documented in the specific methods below.
Pointer X coordinate.
If the wrapped device is a Touch device, this will return the last known X coordinate, which may not be a sensible value.
Pointer Y coordinate.
If the wrapped device is a Touch device, this will return the last known Y coordinate, which may not be a sensible value.
Press the pointer at it’s current location.
If the wrapped device is a mouse, you may pass a button specification. If it is a touch device, passing anything other than 1 will raise a ValueError exception.
Releases the pointer at it’s current location.
If the wrapped device is a mouse, you may pass a button specification. If it is a touch device, passing anything other than 1 will raise a ValueError exception.
Press and release at the current pointer location.
If the wrapped device is a mouse, the button specification is used. If it is a touch device, passing anything other than 1 will raise a ValueError exception.
Moves the pointer to the specified coordinates.
If the wrapped device is a mouse, the mouse will animate to the specified coordinates. If the wrapped device is a touch device, this method will determine where the next press, release or click will occur.
Attempts to move the pointer to ‘object_proxy’s centre point. and click a button
It does this by looking for several attributes, in order. The first attribute found will be used. The attributes used are (in order):
- globalRect (x,y,w,h)
- center_x, center_y
- x, y, w, h
If the wrapped device is a mouse, the button specification is used. If it is a touch device, passing anything other than 1 will raise a ValueError exception.
Attempts to move the pointer to ‘object_proxy’s centre point.
It does this by looking for several attributes, in order. The first attribute found will be used. The attributes used are (in order):
- globalRect (x,y,w,h)
- center_x, center_y
- x, y, w, h
Raises : | ValueError if none of these attributes are found, or if an attribute is of an incorrect type. |
---|