0Pricing
Jetpack Compose Academy · Lección

createComposeRule y finders

Configure una prueba de Compose y localice nodos.

createComposeRule y finders es una lección gratuita de Jetpack Compose Academy en CoddyKit. Esta es la lección 1 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.

Why Test Your UI?

Manual testing is slow and easy to forget. A UI test launches your Composable and checks it automatically, catching breakages before your users ever do. 🧪

Meet the Compose Test Rule

Every Compose test starts with a test rule. It hosts your Composables in a controlled environment so you can render, inspect, and interact with them from test code.

createComposeRule

For pure UI with no Activity, createComposeRule gives you a lightweight rule. You attach it as a JUnit @get:Rule field in your test class.

@get:Rule
val composeTestRule = createComposeRule()

setContent in a Test

Use setContent on the rule to render the Composable you want to verify. This is your test's UI, just like in a real Activity.

composeTestRule.setContent {
    Greeting(name = "Ada")
}

Finders Locate Nodes

Once your UI is on screen, you need a finder to grab a specific element. Finders search the semantics tree and return a node you can assert on.

onNodeWithText

The friendliest finder is onNodeWithText. Give it the text shown on screen and it returns that exact node for you to check.

composeTestRule.onNodeWithText("Hello, Ada!")

onNodeWithContentDescription

For images and icons with no visible text, use onNodeWithContentDescription. It matches the accessibility label, which keeps tests and screen readers in sync.

composeTestRule.onNodeWithContentDescription("Profile photo")

One Node vs Many

onNode finds a single match and fails if there are several. When you expect a group of elements, reach for onAllNodes to get a collection instead.

composeTestRule.onAllNodesWithText("Item")

Quick Check

You want to locate an icon button that has no visible text. Which finder fits best?

Finders Are Lazy

A finder does not search immediately. It captures your intent, and the actual lookup happens when you call an assertion or action on it.

Helpful Failure Messages

When a finder matches nothing, the test fails with a clear message and even prints the current node tree. That makes debugging a missing element fast.

createAndroidComposeRule

Need a real Activity, resources, or navigation? Swap in createAndroidComposeRule, which launches an Activity and still exposes the same finder API.

@get:Rule
val rule = createAndroidComposeRule<MainActivity>()

Recap: Rules & Finders

You set up a compose rule, render with setContent, then locate elements with finders like onNodeWithText. That foundation powers every Compose test you write. 🎉

Preguntas frecuentes

¿La lección «createComposeRule y finders» es gratis?

Sí — el texto completo de «createComposeRule y finders» 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 «createComposeRule y finders»?

Configure una prueba de Compose y localice nodos. 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 1 de 4.

¿Cuánto tiempo toma la lección «createComposeRule y finders»?

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

  1. createComposeRule y finders
  2. Aserciones y ejecución de acciones
  3. Semántica y etiquetas de prueba
  4. Probar ViewModels y Flows
← Volver a Jetpack Compose Academy