Semántica y etiquetas de prueba
Haga que la interfaz sea fácil de probar y accesible.
Semántica y etiquetas de prueba es una lección gratuita de Jetpack Compose Academy en CoddyKit. Esta es la lección 3 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Jetpack Compose Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Jetpack Compose Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
The Semantics Tree
Compose builds a semantics tree next to the visual one. It describes what each element means, and it is exactly what both tests and screen readers read.
Why Semantics Matter
If a node carries no meaning in the tree, finders cannot reach it and TalkBack cannot announce it. Good semantics make your app both testable and accessible. ♿
Content Descriptions Add Meaning
Icons and images carry no text, so give them a contentDescription. It feeds the semantics tree, letting tests find them and screen readers describe them.
Icon(Icons.Default.Add,
contentDescription = "Add item")When Text Is Not Unique
Sometimes many nodes share the same text and a text finder gets confused. You need a stable handle that does not depend on the visible label.
Introducing testTag
A testTag is an invisible identifier you attach with a Modifier. It gives a node a unique name for tests without affecting how it looks.
Box(Modifier.testTag("avatar"))Finding by Tag
Once tagged, locate the node with onNodeWithTag. This is rock-solid even when text changes across languages or states.
composeTestRule.onNodeWithTag("avatar")
.assertIsDisplayed()Quick Check
Several buttons share the label Done, so a text finder is ambiguous. What gives one button a unique, invisible handle for tests?
Tags vs Descriptions
Keep them separate: contentDescription serves real users, while testTag exists only for tests. Do not overload one to do the other's job.
The semantics Modifier
For full control, the semantics modifier lets you set custom properties, like marking a node as a heading for accessibility tools.
Modifier.semantics { heading() }Merging Descendants
Compose can merge a parent and its children into one node with mergeDescendants. That makes a whole row announce as a single, friendly unit.
Modifier.semantics(mergeDescendants = true) {}Print the Tree to Debug
Stuck on a missing node? Call printToLog on the root to dump the full semantics tree, then read exactly what is available to match.
rule.onRoot().printToLog("TREE")Design for Testability
Adding clear descriptions and tags as you build means your tests almost write themselves. Accessible apps and testable apps grow from the same habits.
Recap: Semantics & Tags
The semantics tree powers both testing and accessibility. Use contentDescription for users and testTag for tests to keep your UI easy to verify. 🌳
Preguntas frecuentes
¿La lección «Semántica y etiquetas de prueba» es gratis?
Sí — el texto completo de «Semántica y etiquetas de prueba» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Jetpack Compose Academy, actualiza a CoddyKit PRO. El curso de Jetpack Compose Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Semántica y etiquetas de prueba»?
Haga que la interfaz sea fácil de probar y accesible. Practicas Jetpack Compose Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Jetpack Compose Academy?
No se requiere experiencia previa. Jetpack Compose Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 3 de 4.
¿Cuánto tiempo toma la lección «Semántica y etiquetas de prueba»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Jetpack Compose Academy?
Sí. Cada lección de Jetpack Compose Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- createComposeRule y finders
- Aserciones y ejecución de acciones
- Semántica y etiquetas de prueba
- Probar ViewModels y Flows