createComposeRule e Localizadores
Configure um teste do Compose e localize nós.
createComposeRule e Localizadores é uma aula grátis de Jetpack Compose Academy no CoddyKit. Esta é a aula 1 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Jetpack Compose Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Jetpack Compose Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em 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. 🎉
Perguntas Frequentes
A aula “createComposeRule e Localizadores” é grátis?
Sim — o texto completo de “createComposeRule e Localizadores” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Jetpack Compose Academy, atualize para CoddyKit PRO. O curso de Jetpack Compose Academy inclui 4 aulas no total.
O que vou aprender em “createComposeRule e Localizadores”?
Configure um teste do Compose e localize nós. Você pratica Jetpack Compose Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Jetpack Compose Academy?
Nenhuma experiência prévia é necessária. Jetpack Compose Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 1 de 4.
Quanto tempo leva a aula “createComposeRule e Localizadores”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Jetpack Compose Academy?
Sim. Cada aula de Jetpack Compose Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- createComposeRule e Localizadores
- Verificações e Execução de Ações
- Semântica e Etiquetas de Teste
- Testando ViewModels e Flows