Turn your manual testers into automation experts! Request a DemoStart testRigor Free

How to improve testability of Jetpack Compose apps for UiAutomator

Jetpack Compose (https://developer.android.com/jetpack/compose) is Android’s recommended modern toolkit for building native UI. It simplifies and accelerates UI development on Android with less code. At the same time, testing a UI created with Jetpack Compose is different from testing a “classic” View-based UI. In Compose, only some composables (the building blocks for creating an app in JetPack Compose) emit UI into the UI hierarchy. Compose provides a set of testing APIs to work with elements, which are arranged in a semantic tree and is primarily used for accessibility. It is up to developers to decide what and how much to expose. By default, UI elements are accessible from the UiAutomator framework only by some descriptors (as displayed text). Let’s take a look at the Jetsurvey sample app (https://github.com/android/compose-samples/tree/main/Jetsurvey) with Appium Inspector (https://github.com/appium/appium-inspector).

Jetpack-Compose-apps-for-UiAutomator-Img-1

Elements of the “Sign in as guest” button do not contain any identifiers to avoid XPath when interacting with it. To solve this problem, Compose offers the use of the following steps:

  • Add ‘Modifier.testTag‘ with a unique identifier to the desired composite,
  • Enable ‘testTagAsResourceId‘ in the hierarchy with the required composite.
Jetpack-Compose-apps-for-UiAutomator-Img-2

Let’s take a look in Appium Inspector at the changes after that.

Jetpack-Compose-apps-for-UiAutomator-Img-3

The element now has an extra attribute “resource-id” with our unique value “sign_in_guest_button“. We can now locate it and interact with it easily from any test automation tool.

Related Articles

How to execute test cases in parallel in testRigor?

Parallel execution plays a significant role in End-to-End (E2E) testing for several reasons, mainly time constraints and thorough ...

How to reference elements by UI position using testRigor?

When referring to an element in the DOM, testRigor never relies on unstable XPaths, and it uses testRigor locators instead. ...

How to test QR codes using testRigor?

QR codes have become a game-changer for software applications. Users can access websites, download content, or even make secure ...