Understanding semantics
Unlike the traditional view system, Jetpack Compose does not use references to identify individual UI elements. Please remember that such references are needed in an imperative approach to modify the component tree during runtime. But this is not how Compose works—instead, we declare how the UI should look based on state. Yet, to test if a particular composable looks and behaves as expected, we need to find it among all other children of a Compose hierarchy.
This is where the semantics tree comes into play. As the name implies, semantics give meaning to a UI element or element hierarchies. The semantics tree is generated alongside the UI hierarchy, which it describes using attributes such as Role
, Text
, and Actions
. It is used for accessibility and testing.
In the previous section, I showed you a simple test case that checks if a button text matches a given string. Here is another test case. testLetterAfterButtonClickIsB()
performs a click on the...