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 |
on_toast |
|
on_window_change |
|
on_content_change |
|
on_click |
|
on_scroll |
|
on_text_change |
|
expect |
Subscribes now for the first event of |
wait_for |
Blocks for the first event of |
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 |
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 |
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.
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.
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.)
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.
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
¶
ViewClickedEvent
¶
Bases: DeviceEvent
A view was clicked, by Tap or by anything else.
ViewScrolledEvent
¶
Bases: DeviceEvent
A view scrolled. The values a view does not report are None.
scroll_x
¶
scroll_x: int | None
The scroll position in pixels; a list such as RecyclerView reports items instead.
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.
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).
ContentChange
¶
Bases: Enum
What changed inside a window (ContentChangedEvent.changes).