Skip to content
ManjunathaThimmaiah edited this page Sep 20, 2023 · 8 revisions

Using UIAutomator2

To benefit from UIAutomator2 test capabilities (like interacting with other apps or system elements), you must start the test server with: start_test_server_in_background(with_uiautomator: true)

Then you will be able to use the command perform_action('method', {parameters}) which includes the following actions.

Method Parameter Function
uiautomator_touch_text Text Tap on element with text
uiautomator_touch_partial_text Text Tap on element containing text
uiautomator_drag_coordinates x origin, x destination, y origin, y destination, (optional) steps Drag on the given coordinates
pull_notification none Open the notification shutter
clear_notifications none Clear notifications and close the shutter
uiautomator_text_dump none Dumps text from elements in the UI hierarchy
uiautomator_ui_dump none Dumps elements in the UI hierarchy
wifi 'on' or 'off' Switch device wifi on or off
wait_for_idle_sync timeout in seconds Wait for idle state

And the more advanced command uiautomator_execute which takes 4 arguments.

perform_action('uiautomator_execute', 'strategy', 'locator', element index, 'action')

This will return true, and the result of the query if succeeds or an exception if the strategy, or action are invalid. If an element is not found, it will return false.

Where strategy and action can be:

strategy description
class class type of ui element
res element resource
desc ui element description
descContains ui element description contains
descEndsWith ui element description ends with
descStartWith ui element description starts with text
text ui element with text
textContains ui element contains text
textEndsWith ui element ends with text
textStartWith ui element starts with text
pkg package name from application of ui element
action description
click tap on element
longClick long tap on element
getText get text from element
getContentDescription get description
getClassName get class name
getResourceName get resource name
getVisibleBounds get coords for visible bounds of element
getVisibleCenter get coords for visible center of element
getApplicationPackage get package name of application
getChildCount number of children elements
clear clear text from element
isCheckable can the element be checked?
isChecked is the element checked?
isClickable is the element clickable?
isEnabled is the element enabled?
isFocusable can the element be focused?
isFocused is the element focused?
isLongClickable can the element be long clicked?
isScrollable is the element scrollable?
isSelected is the element selected?

For entering text into the input text field can be performed with:

perform_action('uiautomator_set_text', 'strategy', 'locator', element index, 'input text')

input text is the text value which needs to be into the field.

Scrolling to the element can be performed by Horizontally and vertically with the following commands.

For vertical scrolling the command is:

perform_action('uiautomator_vertically_scroll_to_element', 'strategy', 'locator', 'container_strategy', 'container_locator', 'max_scrolls')

For horizontal scrolling the command is:

perform_action('uiautomator_horizontally_scroll_to_element', 'strategy', 'locator', 'container_strategy', 'container_locator', 'max_scrolls')

Where strategy and parentstrategy can be:

strategy description
class class type of ui element
res element resource
desc ui element description
descContains ui element description contains
descEndsWith ui element description ends with
descStartWith ui element description starts with text
text ui element with text
textContains ui element contains text
textEndsWith ui element ends with text
textStartWith ui element starts with text
pkg package name from application of ui element

max_scrolls is a integer number and It is maxiumum number of times the screen can be scrolled.

container_strategy and container_locator If screen layout contains multiple container and the scrolling to be peformed on a specific container then parent strategy and locator should be provided and for the single container the arugments can be ignored.

Additional to the above scrolling method, below Scrolling can be performed in forward or backward direction for both vertical and horizontal.

For vertical scrolling FORWARD/BACKWARD the command is:

perform_action('uiautomator_vertically_scroll_forward_or_backward_to_element', 'direction', 'strategy', 'locator', 'max_scrolls')

perform_action('uiautomator_vertically_scroll_forward_or_backward_to_element', 'FORWARD', 'strategy', 'locator', 'max_scrolls')

perform_action('uiautomator_vertically_scroll_forward_or_backward_to_element', 'BACKWARD', 'strategy', 'locator', 'max_scrolls')

For horizontal scrolling the command is:

perform_action('uiautomator_horizontally_scroll_forward_or_backward_to_element', 'FORWARD', 'strategy', 'locator', 'max_scrolls')

perform_action('uiautomator_horizontally_scroll_forward_or_backward_to_element', 'BACKWARD', 'strategy', 'locator', 'max_scrolls')