Jetpack Compose Testing Cheat Sheet
Master finders, matchers, assertions, and test rules for clean UI automation. #JetpackComposeDev
Finders
Find and target UI nodes in the Compose semantics tree.
// Find by visible text
onNodeWithText("Login")
// Find by test tag
onNodeWithTag("ProfileCard")
// Find by accessibility/content description
onNodeWithContentDescription("Menu")
// Find all nodes with that text (collection)
onAllNodesWithText("Item")
// Root of the semantics tree
onRoot()
Matchers
Check node properties before interaction.
// Exact text match
hasText("Submit")
// Node is clickable
hasClickAction()
// Node is visible on screen
isDisplayed()
// Node is enabled (not disabled)
isEnabled()
// Node is selected (e.g., toggle)
isSelected()
// Accessibility text match
hasContentDescription("Avatar")
Hierarchical & Selectors
Navigate parent / child / sibling relations and filter nodes.
// Match a node whose parent has the given text
hasParent(hasText("Container"))
// Match nodes that have a child with click action
hasAnyChild(hasClickAction())
// Move to the parent node for further operations
onParent()
// Get all children of current node
onChildren()
// Select a sibling node
onSibling()
// Filter nodes by another matcher
filter { matcher -> ... }
Assertions
Verify the expected UI state before or after actions.
// Ensure node exists in the tree
assertExists()
// Ensure node is actually visible on screen
assertIsDisplayed()
// Ensure node is enabled
assertIsEnabled()
// Ensure node is hidden / not currently visible
assertIsNotDisplayed()
// Exact text assertion
assertTextEquals("Welcome")
// Assert node exposes a click action
assertHasClickAction()
Bounds & Collections
Layout checks and multi-node assertions.
// Assert exact width in dp
assertWidthIsEqualTo(120.dp)
// Assert minimum height in dp
assertHeightIsAtLeast(48.dp)
// On a collection, assert every node passes given assertion
assertAll { it.assertIsDisplayed() }
// Assert collection size equals expected
assertCountEquals(3)
Actions
Simulate user input and keyboard actions.
// Simulate a tap
performClick()
// Scroll until node is visible
performScrollTo()
// Type text into a text field
performTextInput("Hello")
// Replace the text content
performTextReplacement("New")
// Trigger IME action (Done/Send)
performImeAction()
Touch Input (Gestures)
Use performTouchInput to simulate realistic gestures.
// Gesture helpers inside performTouchInput { ... }
click()
doubleClick()
longClick()
swipeUp()
swipeWithVelocity()
pinch() // multi-touch pinch
Touch Input (Partial Control)
Fine-grained control while building gestures manually.
// Low-level sequence to construct a gesture
down() // start touch
move() // move pointer
moveBy() // move relative
up() // release
cancel() // cancel gesture
ComposeTestRule
Use for pure Compose screens (no Activity). Control clocks and set content.
// Declare rule and load composable content
@get:Rule
val rule = createComposeRule()
// Load UI
rule.setContent { MyScreen() }
// Find and click
rule.onNodeWithText("OK").performClick()
// Control animations and frames
rule.mainClock.autoAdvance = false
rule.mainClock.advanceTimeBy(500)
AndroidComposeTestRule
Use for Activity-hosted tests and integration scenarios.
// Use createAndroidComposeRule for activity-level tests
@get:Rule
val rule = createAndroidComposeRule>MainActivity<()
// Access activity for integration-specific operations
rule.activity
rule.activityRule
// Optionally override UI content
rule.setContent { MyApp() }
Debug Tools
Inspect nodes and capture visual output while authoring tests.
// Dump the semantics tree to log
printToLog()
// Return a textual tree for inspection
printToString()
// Capture a screenshot (ImageBitmap)
captureToImage()
Best Practices
Practical tips to improve test stability and reliability.
- Use testTag for consistent node lookups (avoid relying only on text).
- Disable or control animations for deterministic tests (mainClock).
- Call assertExists() before performing actions to fail fast and give clearer errors.
- Prefer stable keys in Lazy layouts to avoid list flakiness.
- Keep tests focused and avoid heavy setup in the UI thread.
Thank You - Follow Boltuix
Daily Jetpack Compose insights - testing, layouts, Material 3, and KMP.


#JetpackCompose #ComposeTesting #AndroidDev #Kotlin #AppDadz #Boltuix













