This test shows a button labeled "Say Hello". When clicked, it shows the text "Hello!". The test clicks the button and checks if the greeting text appears.
Android Kotlin
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.*
import org.junit.Rule
import org.junit.Test
class MyComposeTest {
@get:Rule
val composeTestRule = createComposeRule()
@Composable
fun GreetingButton() {
var clicked = false
Button(onClick = { clicked = true }) {
Text("Say Hello")
}
if (clicked) {
Text("Hello!")
}
}
@Test
fun testGreetingButton() {
composeTestRule.setContent {
GreetingButton()
}
composeTestRule.onNodeWithText("Say Hello").assertIsDisplayed()
composeTestRule.onNodeWithText("Say Hello").performClick()
composeTestRule.onNodeWithText("Hello!").assertIsDisplayed()
}
}
OutputSuccess
Important Notes
Use onNodeWithText to find elements by visible text.
Use performClick() to simulate user taps.
Use assertIsDisplayed() to check if UI elements are visible.
Summary
Compose UI testing helps check your app's screens work as expected.
Use createComposeRule() to set up tests.
Find UI elements by text or tags, then perform actions or checks.
Practice
(1/5)
1. What is the main purpose of createComposeRule() in Compose UI testing?
easy
A. To create UI elements dynamically during runtime
B. To set up the environment for testing Compose UI components
C. To launch the app on a physical device
D. To generate random test data for UI tests
Solution
Step 1: Understand the role of createComposeRule()
This function sets up the Compose testing environment so you can test UI components safely and isolated.
Step 2: Differentiate from other options
It does not create UI elements dynamically, launch apps, or generate test data; it only prepares the test rule.
Final Answer:
To set up the environment for testing Compose UI components -> Option B
Quick Check:
Compose test setup = B [OK]
Hint: Remember: createComposeRule sets up Compose test environment [OK]
Common Mistakes:
Thinking it creates UI elements during app runtime
Confusing it with launching the app on devices
Assuming it generates test data automatically
2. Which of the following is the correct way to find a UI element by its text in Compose UI testing?
easy
A. composeTestRule.getElementByText("Hello")
B. composeTestRule.findViewByText("Hello")
C. composeTestRule.selectNode("Hello")
D. composeTestRule.onNodeWithText("Hello")
Solution
Step 1: Recall Compose testing API for text search
The correct method to find a node by text is onNodeWithText().
Step 2: Verify other options are invalid
Methods like findViewByText, getElementByText, and selectNode do not exist in Compose testing.
Final Answer:
composeTestRule.onNodeWithText("Hello") -> Option D
Quick Check:
Find by text = onNodeWithText [OK]
Hint: Use onNodeWithText to find text elements in Compose tests [OK]
Common Mistakes:
Using Android View testing methods instead of Compose methods
A. performClick() is not a valid Compose test function
B. The Button composable cannot be clicked in tests
C. The node is searched by tag but no tag was set on the Button
D. The Text inside Button should be searched instead of Button
Solution
Step 1: Check how the node is found
The test looks for a node with tag "button" but the Button has no tag set.
Step 2: Understand Compose testing tags
To find by tag, the Button must have Modifier.testTag("button") applied, which is missing.
Final Answer:
The node is searched by tag but no tag was set on the Button -> Option C
Quick Check:
Missing testTag causes node not found [OK]
Hint: Always add testTag modifier to find nodes by tag [OK]
Common Mistakes:
Assuming performClick works without correct node selection
Searching text inside Button instead of Button itself
Thinking performClick is invalid in Compose tests
5. You want to test a Compose screen with a list of items. Each item has a unique test tag like "item_1", "item_2", etc. Which approach correctly verifies that the item with tag "item_3" is displayed and clickable?
hard
A. composeTestRule.onNodeWithTag("item_3").assertIsDisplayed().performClick()
B. composeTestRule.onNodeWithText("item_3").assertIsDisplayed().performClick()
C. composeTestRule.onNodeWithTag("item_3").assertExists().performClick()
D. composeTestRule.onNodeWithContentDescription("item_3").assertIsDisplayed().performClick()
Solution
Step 1: Identify correct node selector for test tag
Use onNodeWithTag("item_3") to find the node by its unique test tag.
Step 2: Verify visibility and interaction
assertIsDisplayed() confirms the node is visible, and performClick() simulates a click.
Step 3: Eliminate incorrect options
Searching by text or content description is wrong here; assertExists() alone doesn't check visibility.
Final Answer:
composeTestRule.onNodeWithTag("item_3").assertIsDisplayed().performClick() -> Option A
Quick Check:
Tag + visible + click = composeTestRule.onNodeWithTag("item_3").assertIsDisplayed().performClick() [OK]
Hint: Use onNodeWithTag + assertIsDisplayed before performClick [OK]
Common Mistakes:
Using onNodeWithText instead of onNodeWithTag for tags
Skipping assertIsDisplayed before clicking
Confusing content description with test tag
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.