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 Create Tests Using Live Mode in testRigor?

testRigor offers a unique capability known as the ‘Live Mode‘ to create and validate test cases. Using it, you can ...

How to Automate Web Testing with testRigor

Web testing plays a crucial role in the software development lifecycle, allowing web applications to function correctly, provide ...
On our website, we utilize cookies to ensure that your browsing experience is tailored to your preferences and needs. By clicking "Accept," you agree to the use of all cookies. Learn more.
Cookie settings
Privacy Overview
This site utilizes cookies to enhance your browsing experience. Among these, essential cookies are stored on your browser as they are necessary for ...
Read more
Strictly Necessary CookiesAlways Enabled
Essential cookies are crucial for the proper functioning and security of the website.
Non-NecessaryEnabled
Cookies that are not essential for the website's functionality but are employed to gather additional data. You can choose to opt out by using this toggle switch. These cookies gather data for analytics and performance tracking purposes.