Component - Compose UI testing
This UI component demonstrates how to test a simple Compose UI screen with a text and a button. The test checks if the text updates correctly when the button is clicked.
This UI component demonstrates how to test a simple Compose UI screen with a text and a button. The test checks if the text updates correctly when the button is clicked.
Scaffold ├── Column │ ├── Text │ └── Button
createComposeRule() in Compose UI testing?createComposeRule()onNodeWithText().findViewByText, getElementByText, and selectNode do not exist in Compose testing.composeTestRule.setContent {
Text("Welcome")
}
composeTestRule.onNodeWithText("Welcome").assertExists()
What will happen when this test runs?Text composable with the string "Welcome".assertExists() checks that the node with text "Welcome" is present, which it is.composeTestRule.setContent {
Button(onClick = {}) {
Text("Click Me")
}
}
composeTestRule.onNodeWithTag("button").performClick()Modifier.testTag("button") applied, which is missing.onNodeWithTag("item_3") to find the node by its unique test tag.assertIsDisplayed() confirms the node is visible, and performClick() simulates a click.assertExists() alone doesn't check visibility.Pass the test and this pattern moves unknown