Semantics
Stay organized with collections
Save and categorize content based on your preferences.
UI tests in Compose use semantics to interact with the UI hierarchy.
Semantics, as the name implies, give meaning to a piece of UI. In this context,
a "piece of UI" (or element) can mean anything from a single composable to a
full screen. The semantics tree is generated alongside the UI hierarchy and
describes the hierarchy.
You can learn more about semantics generally in Semantics in Compose.
Figure 1. A typical UI hierarchy and its semantics tree.
The semantics framework is primarily used for accessibility, so tests take
advantage of the information exposed by semantics about the UI hierarchy.
Developers decide what and how much to expose.
Figure 2. A typical button containing an icon and text.
For example, given a button like this that consists of an icon and a text
element, the default semantics tree only contains the text label "Like". This is
because some composables, such as Text
, already expose some properties to the
semantics tree. You can add properties to the semantics tree by using a
Modifier
.
MyButton(
modifier = Modifier.semantics { contentDescription = "Add to favorites" }
)
Additional Resources
- Test apps on Android: The main Android testing
landing page provides a broader view of testing fundamentals and techniques.
- Fundamentals of testing: Learn more
about the core concepts behind testing an Android app.
- Local tests: You can run some tests
locally, on your own workstation.
- Instrumented tests: It is good
practice to also run instrumented tests. That is, tests that run directly
on-device.
- Continuous integration:
Continuous integration lets you integrate your tests into your deployment
pipeline.
- Test different screen sizes: With
some many devices available to users, you should test for different screen
sizes.
- Espresso: While intended for View-based
UIs, Espresso knowledge can still be helpful for some aspects of Compose
testing.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-08-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-26 UTC."],[],[],null,["# Semantics\n\nUI tests in Compose use *semantics* to interact with the UI hierarchy.\nSemantics, as the name implies, give meaning to a piece of UI. In this context,\na \"piece of UI\" (or element) can mean anything from a single composable to a\nfull screen. The *semantics tree* is generated alongside the UI hierarchy and\ndescribes the hierarchy.\n\nYou can learn more about semantics generally in [Semantics in Compose](/develop/ui/compose/accessibility/semantics).\n\n**Figure 1.** A typical UI hierarchy and its semantics tree.\n\nThe semantics framework is primarily used for accessibility, so tests take\nadvantage of the information exposed by semantics about the UI hierarchy.\nDevelopers decide what and how much to expose.\n\n**Figure 2.** A typical button containing an icon and text.\n\nFor example, given a button like this that consists of an icon and a text\nelement, the default semantics tree only contains the text label \"Like\". This is\nbecause some composables, such as `Text`, already expose some properties to the\nsemantics tree. You can add properties to the semantics tree by using a\n`Modifier`. \n\n MyButton(\n modifier = Modifier.semantics { contentDescription = \"Add to favorites\" }\n )\n\nAdditional Resources\n--------------------\n\n- **[Test apps on Android](/training/testing)**: The main Android testing landing page provides a broader view of testing fundamentals and techniques.\n- **[Fundamentals of testing](/training/testing/fundamentals):** Learn more about the core concepts behind testing an Android app.\n- **[Local tests](/training/testing/local-tests):** You can run some tests locally, on your own workstation.\n- **[Instrumented tests](/training/testing/instrumented-tests):** It is good practice to also run instrumented tests. That is, tests that run directly on-device.\n- **[Continuous integration](/training/testing/continuous-integration):** Continuous integration lets you integrate your tests into your deployment pipeline.\n- **[Test different screen sizes](/training/testing/different-screens):** With some many devices available to users, you should test for different screen sizes.\n- **[Espresso](/training/testing/espresso)**: While intended for View-based UIs, Espresso knowledge can still be helpful for some aspects of Compose testing."]]