Skip to content

Device events

device.events: live accessibility events, as callbacks or one-shot waits. Each kind of event is its own class. Guide: Device events.

DeviceEvents

Live accessibility events from the device (Device.events): Android tells the driver when a toast shows, a window changes, a view is clicked or scrolled, or text or content changes, and the server streams the kinds a registration asks for. Each kind is its own event class, and the class is what you subscribe with, so a callback or a wait gets that class and its fields::

device.events.on_toast(lambda e: print(e.text))           # e: ToastShownEvent
device.events.wait_for(WindowChangedEvent, package="com.shop")

Events are hints, not proof: Android merges and drops them. React to one by reading the screen again (device.screen.element(...)); never assume the event alone says the screen is ready. Watching is read-only and changes nothing on the device.

Methods:

Name Description
register

Calls callback with every event of kind (an event class such as

on_toast

register(ToastShownEvent, callback): every toast shown.

on_window_change

register(WindowChangedEvent, callback): every window that appears, goes away or changes.

on_content_change

register(ContentChangedEvent, callback): every change inside a window (frequent).

on_click

register(ViewClickedEvent, callback): every clicked view.

on_scroll

register(ViewScrolledEvent, callback): every scroll.

on_text_change

register(TextChangedEvent, callback): every edit of an editable view's text.

expect

Subscribes now for the first event of kind that where accepts (every event

wait_for

Blocks for the first event of kind that where accepts, from now on, up to

register

register(kind: type[E], callback: Callable[[E], object], *, package: str | None = None) -> EventRegistration[E]

Calls callback with every event of kind (an event class such as ToastShownEvent) from now on, on a background thread, until the returned registration is closed or the device detached. When this returns the server is subscribed: every event an action started afterwards causes is delivered. package keeps only that app's.

Registrations are independent of each other; at most 64 per attached device. Use one as a context manager to close it, and to have its failures raised, at the end of a block::

with device.events.register(WindowChangedEvent, print):
    app.element(text("Settings")).tap()

on_toast

on_toast(callback: Callable[[ToastShownEvent], object], *, package: str | None = None) -> EventRegistration[ToastShownEvent]

register(ToastShownEvent, callback): every toast shown.

on_window_change

on_window_change(callback: Callable[[WindowChangedEvent], object], *, package: str | None = None) -> EventRegistration[WindowChangedEvent]

register(WindowChangedEvent, callback): every window that appears, goes away or changes.

on_content_change

on_content_change(callback: Callable[[ContentChangedEvent], object], *, package: str | None = None) -> EventRegistration[ContentChangedEvent]

register(ContentChangedEvent, callback): every change inside a window (frequent).

on_click

on_click(callback: Callable[[ViewClickedEvent], object], *, package: str | None = None) -> EventRegistration[ViewClickedEvent]

register(ViewClickedEvent, callback): every clicked view.

on_scroll

on_scroll(callback: Callable[[ViewScrolledEvent], object], *, package: str | None = None) -> EventRegistration[ViewScrolledEvent]

register(ViewScrolledEvent, callback): every scroll.

on_text_change

on_text_change(callback: Callable[[TextChangedEvent], object], *, package: str | None = None) -> EventRegistration[TextChangedEvent]

register(TextChangedEvent, callback): every edit of an editable view's text.

expect

expect(kind: type[E], *, where: Callable[[E], bool] | None = None, package: str | None = None) -> ExpectedEvent[E]

Subscribes now for the first event of kind that where accepts (every event when it is None), to wait for after an action — the event cannot slip past between the action and the wait::

with device.events.expect(ToastShownEvent, where=lambda e: "Saved" in e.text) as saved:
    app.element(text("Save")).tap()
    toast = saved.wait(timeout=5)

wait_for

wait_for(kind: type[E], *, where: Callable[[E], bool] | None = None, package: str | None = None, timeout: float | None = None) -> E

Blocks for the first event of kind that where accepts, from now on, up to timeout seconds (default: timeouts.wait). Only events that fire after the call count: to wait for what an action causes, use expect before the action.

EventRegistration

Bases: Generic[E]

A live registration made by DeviceEvents.register (or an on_* helper). Its callback runs on one background thread of its own, once per matching event, in the order the events fired, until close(), the device's detach, or a failure that ends it (error).

A callback that raises is logged and does not stop the registration; the first such exception is kept in callback_error. Used as a context manager, the registration is closed when the block exits, and if it ended with an error or a callback raised, the block re-raises that (unless the block itself raised)::

with device.events.on_toast(lambda e: toasts.append(e.text)):
    checkout.tap()

Methods:

Name Description
close

Stops delivery and waits up to timeout seconds for a callback in progress to

kind

kind: type[E]

The event class this registration receives.

package

package: str | None

The only package whose events it receives; None for every package.

active

active: bool

True until it is closed or ended (detach, error).

error

error: TapError | None

What ended the registration when it was not closed or detached: ServerError with RESOURCE_EXHAUSTED when events were lost (the callback fell more than 1024 events behind, or the device produced them faster than they could be sent), SESSION_UNUSABLE when the driver connection was lost. None otherwise.

callback_error

callback_error: BaseException | None

The first exception the callback raised; None when it never did.

close

close(timeout: float = 5.0) -> None

Stops delivery and waits up to timeout seconds for a callback in progress to return (not when called from the callback itself). Idempotent.

ExpectedEvent

Bases: Generic[E]

A one-shot wait made by DeviceEvents.expect: subscribed when it was created, so an event the next action causes cannot be missed. wait returns the first matching event. Close it (or use it as a context manager) when it is no longer needed.

Methods:

Name Description
wait

The first matching event, waiting up to timeout seconds (default: the device's

close

Unsubscribes; idempotent.

wait

wait(timeout: float | None = None) -> E

The first matching event, waiting up to timeout seconds (default: the device's timeouts.wait) from this call. Raises WaitTimeoutError when none arrived, the registration's error when events were lost or the driver connection was, the predicate's exception when where raised, and TapError when the device was detached first.

close

close() -> None

Unsubscribes; idempotent.

DeviceEvent

What every accessibility event has; each kind is a subclass (ToastShownEvent, WindowChangedEvent, ...). A hint that the screen changed, not the screen's state: Android merges and drops events, and an event holds no element. To act on one, read the screen again through a selector.

sequence

sequence: int

Increases by one per event the driver forwards; a gap only means other events did not match this registration.

uptime_ms

uptime_ms: int

Android uptime when the event fired (SystemClock.uptimeMillis), not the wall clock.

package_name

package_name: str | None

The package that raised it; None when Android names none.

ToastShownEvent

Bases: DeviceEvent

A toast was shown, as it happened. (ToastEvent is a different thing: an entry of Device.toast_history, which keeps the last toasts for reading later.)

text

text: str

The toast's message (at most 512 characters).

WindowChangedEvent

Bases: DeviceEvent

A window appeared, went away or changed: an activity, dialog, popup or pane.

class_name

class_name: str | None

The activity, dialog or view class; None when Android names none.

title

title: str | None

The window's title or the pane's text; None when there is none.

window_id

window_id: int | None

changes

changes: frozenset[WindowChange]

What changed; never empty. STATE alone for a new activity, dialog or pane; the other flags come from Android's window list changes (API 28+).

ContentChangedEvent

Bases: DeviceEvent

Content inside a window changed. Frequent: an animation can raise one per frame.

class_name

class_name: str | None

window_id

window_id: int | None

changes

changes: frozenset[ContentChange]

What changed; empty when Android did not say.

ViewClickedEvent

Bases: DeviceEvent

A view was clicked, by Tap or by anything else.

class_name

class_name: str | None

The clicked view's class (android.widget.Button).

label

label: str | None

The view's text; None when it has none.

window_id

window_id: int | None

ViewScrolledEvent

Bases: DeviceEvent

A view scrolled. The values a view does not report are None.

class_name

class_name: str | None

window_id

window_id: int | None

scroll_x

scroll_x: int | None

The scroll position in pixels; a list such as RecyclerView reports items instead.

scroll_y

scroll_y: int | None

max_scroll_x

max_scroll_x: int | None

max_scroll_y

max_scroll_y: int | None

delta_x

delta_x: int | None

How far this scroll moved horizontally, in pixels (API 28+).

delta_y

delta_y: int | None

from_index

from_index: int | None

For a list: the first visible item.

to_index

to_index: int | None

For a list: the last visible item.

item_count

item_count: int | None

TextChangedEvent

Bases: DeviceEvent

An editable view's text changed. text and before_text hold what was typed (at most 512 characters each); Android masks a password field's text itself. Android raises it for typed input, once per key (type_text); text set in one step (set_text) is a ContentChangedEvent with ContentChange.TEXT instead.

class_name

class_name: str | None

text

text: str

before_text

before_text: str

from_index

from_index: int | None

Where the change starts.

added_count

added_count: int | None

removed_count

removed_count: int | None

password

password: bool

window_id

window_id: int | None

WindowChange

Bases: Enum

What changed about a window (WindowChangedEvent.changes).

STATE

STATE

An activity, dialog, popup or pane appeared or changed state (TYPE_WINDOW_STATE_CHANGED).

ADDED

ADDED

REMOVED

REMOVED

TITLE

TITLE

BOUNDS

BOUNDS

LAYER

LAYER

ACTIVE

ACTIVE

FOCUSED

FOCUSED

ACCESSIBILITY_FOCUSED

ACCESSIBILITY_FOCUSED

PARENT

PARENT

CHILDREN

CHILDREN

PIP

PIP

Picture-in-picture changed.

ContentChange

Bases: Enum

What changed inside a window (ContentChangedEvent.changes).

SUBTREE

SUBTREE

Nodes were added, removed or rearranged.

TEXT

TEXT

CONTENT_DESCRIPTION

CONTENT_DESCRIPTION

STATE_DESCRIPTION

STATE_DESCRIPTION

PANE_TITLE

PANE_TITLE

PANE_APPEARED

PANE_APPEARED

PANE_DISAPPEARED

PANE_DISAPPEARED

DRAG_STARTED

DRAG_STARTED

DRAG_DROPPED

DRAG_DROPPED

DRAG_CANCELLED

DRAG_CANCELLED

CONTENT_INVALID

CONTENT_INVALID

ERROR

ERROR

ENABLED

ENABLED