autopilot.input - Generate keyboard, mouse, and touch input events

Autopilot unified input system.

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:

  • Keyboard - traditional keyboard devices.

  • Mouse - traditional mouse devices (Currently only avaialble on the

    desktop).

  • Touch - single point-of-contact touch device.

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.
class autopilot.input.Keyboard[source]

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.

static create(preferred_backend='')[source]

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:

  • X11 - Generate keyboard events using the X11 client
    libraries.
  • UInput - Use UInput kernel-level device driver.
  • OSK - Use the graphical On Screen Keyboard as a backend.
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.
focused_type(*args, **kwds)[source]

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
press(keys, delay=0.2)[source]

Send key press events only.

Parameters:
  • keys – Keys you want pressed.
  • delay – The delay (in Seconds) after pressing the keys before returning control to the caller.
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.

release(keys, delay=0.2)[source]

Send key release events only.

Parameters:
  • keys – Keys you want released.
  • delay – The delay (in Seconds) after releasing the keys before returning control to the caller.
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(keys, delay=0.2)[source]

Press and release all items in ‘keys’.

This is the same as calling ‘press(keys);release(keys)’.

Parameters:
  • keys – Keys you want pressed and released.
  • delay – The delay (in Seconds) after pressing and releasing each key.

Example:

press_and_release('Alt+F2')

presses both the ‘Alt’ and ‘F2’ keys, and then releases both keys.

type(string, delay=0.1)[source]

Simulate a user typing a string of text.

Parameters:
  • string – The string to text to type.
  • delay – The delay (in Seconds) after pressing and releasing each key. Note that the default value here is shorter than for the press, release and press_and_release methods.

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’).

on_test_end(*args)
on_test_start(*args)
class autopilot.input.Mouse[source]

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 = Mouse.create()
mouse.move(100, 50)
mouse.click()
static create(preferred_backend='')[source]

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:

  • X11 - Generate mouse events using the X11 client libraries.
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.
x

Mouse position X coordinate.

y

Mouse position Y coordinate.

press(button=1)[source]

Press mouse button at current mouse location.

release(button=1)[source]

Releases mouse button at current mouse location.

click(button=1, press_duration=0.1)[source]

Click mouse at current location.

click_object(object_proxy, button=1, press_duration=0.1)[source]

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.
move(x, y, animate=True, rate=10, time_between_events=0.01)[source]

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.

move_to_object(object_proxy)[source]

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.
position()[source]

Returns the current position of the mouse pointer.

Returns:(x,y) tuple
drag(x1, y1, x2, y2, rate=10, time_between_events=0.01)[source]

Perform a press, move and release.

This is to keep a common API between Mouse and Finger as long as possible.

The pointer will be dragged from the starting point to the ending point with multiple moves. The number of moves, and thus the time that it will take to complete the drag can be altered with the rate parameter.

Parameters:
  • x1 – The point on the x axis where the drag will start from.
  • y1 – The point on the y axis where the drag will starts from.
  • x2 – The point on the x axis where the drag will end at.
  • y2 – The point on the y axis where the drag will end at.
  • rate – The number of pixels the mouse will be moved per iteration. Default is 10 pixels. A higher rate will make the drag faster, and lower rate will make it slower.
  • time_between_events – The number of seconds that the drag will wait between iterations.
on_test_end(*args)
on_test_start(*args)
class autopilot.input.Touch[source]

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.

static create(preferred_backend='')[source]

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:

  • UInput - Use UInput kernel-level device driver.
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.
pressed

Return True if this touch is currently in use (i.e.- pressed on the ‘screen’).

tap(x, y)[source]

Click (or ‘tap’) at given x,y coordinates.

tap_object(object)[source]

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.
press(x, y)[source]

Press and hold at the given x,y coordinates.

move(x, y)[source]

Move the pointer coords to (x,y).

Note

The touch ‘finger’ must be pressed for a call to this method to be successful. (see press for further details on touch presses.)

Raises:RuntimeError if called and the touch ‘finger’ isn’t pressed.
release()[source]

Release a previously pressed finger

drag(x1, y1, x2, y2, rate=10, time_between_events=0.01)[source]

Perform a drag gesture.

The finger will be dragged from the starting point to the ending point with multiple moves. The number of moves, and thus the time that it will take to complete the drag can be altered with the rate parameter.

Parameters:
  • x1 – The point on the x axis where the drag will start from.
  • y1 – The point on the y axis where the drag will starts from.
  • x2 – The point on the x axis where the drag will end at.
  • y2 – The point on the y axis where the drag will end at.
  • rate – The number of pixels the finger will be moved per iteration. Default is 10 pixels. A higher rate will make the drag faster, and lower rate will make it slower.
  • time_between_events – The number of seconds that the drag will wait between iterations.
Raises:
  • RuntimeError – if the finger is already pressed.
  • RuntimeError – if no more finger slots are available.
class autopilot.input.Pointer(device)[source]

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.

x

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.

y

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(button=1)[source]

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.

release(button=1)[source]

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.

click(button=1, press_duration=0.1)[source]

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.

move(x, y)[source]

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.

click_object(object_proxy, button=1, press_duration=0.1)[source]

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.

move_to_object(object_proxy)[source]

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.
position()[source]

Returns the current position of the pointer.

Returns:(x,y) tuple
drag(x1, y1, x2, y2, rate=10, time_between_events=0.01)[source]

Perform a press, move and release.

This is to keep a common API between Mouse and Finger as long as possible.

The pointer will be dragged from the starting point to the ending point with multiple moves. The number of moves, and thus the time that it will take to complete the drag can be altered with the rate parameter.

Parameters:
  • x1 – The point on the x axis where the drag will start from.
  • y1 – The point on the y axis where the drag will starts from.
  • x2 – The point on the x axis where the drag will end at.
  • y2 – The point on the y axis where the drag will end at.
  • rate – The number of pixels the mouse will be moved per iteration. Default is 10 pixels. A higher rate will make the drag faster, and lower rate will make it slower.
  • time_between_events – The number of seconds that the drag will wait between iterations.