Loading...
Loading...
Use this skill to locate Compose semantics nodes from a UI test using `onNodeWithTag`, `onNodeWithText`, `onNodeWithContentDescription`, `onAllNodes*`, and `onRoot`. Covers the count contract (single-node finders fail on 0 or >1 matches; collection finders never throw on 0), the merged-vs-unmerged tree distinction, and why tagging in production beats text-matching from tests. Use when the developer reports "no node matched", "multiple nodes matched", "tag not found", "useUnmergedTree", "Modifier.testTag", or asks how to find a Button/Text/Icon in a Compose test. If the developer mentions onNodeWithText, onNodeWithTag, onNodeWithContentDescription, onAllNodesWithTag, onRoot, or merged tree vs unmerged tree, use this skill.
npx skill4agent add skydoves/android-testing-skills finding-nodes-by-tag-text-contentuseUnmergedTreeSemanticsNodeInteractionButtonTextIconIconButtonSwitchonNodeWithTagonNodeWithTextonNodeWithContentDescriptiononAllNodesWith*onRootuseUnmergedTree../../assertions/asserting-node-state-and-text/SKILL.md../../assertions/asserting-bounds-and-dimensions/SKILL.md../composing-semantics-matchers/SKILL.md../traversing-the-semantics-tree/SKILL.md../../debug/printing-the-semantics-tree/SKILL.mdandroidx.compose.ui:ui-test-junit4../../setup/configuring-test-dependencies/SKILL.mdcreateComposeRuleComposeTestRuleComposeUiTest../../setup/choosing-test-rule-vs-runtest/SKILL.mdsetContent { … }onNodeWithTag(SomeTag)Modifier.testTag(SomeTag)const val SomeTag = "Some"androidx/material3onNodeWithTagonNodeWithTextonNodeWithContentDescriptioncommonMain/.../Finders.kt| Need to match by … | Finder | File:line |
|---|---|---|
| | |
| Visible/editable/input text | | |
| Accessibility content description | | |
| Custom predicate | | |
| Whole tree | | |
onNodeWith*@CheckResultonNode(hasTestTag(...))onNode(hasText(...))onNode(hasContentDescription(...))SemanticsNodeInteractionAssertionErrorSemanticsNodeInteraction.kt:150-191SemanticsNodeInteractionCollectionassertCountEquals(0)Assertions.kt:276-292useUnmergedTree = trueTextIconButtonTextuseUnmergedTree = trueuseUnmergedTree = trueonChildonParentfilterSemanticsNodeInteraction.kt:42-49SemanticsNodeInteraction.kt:184-205getNodesInUnmergedTreeuseUnmergedTree = trueonNodeWithText("Sub", substring = true)"Submit""Subscribe"onAllNodesWithTextassertCountEquals// WRONG
@Test
fun submit_isEnabled_afterFormFilled() {
rule.setContent { CheckoutScreen() }
rule.onNodeWithText("Place order").assertIsEnabled()
}
// WRONG because: the button label changes with copy edits, and the test breaks the moment QA
// asks for "Buy now". Also fails under any locale that is not the test default.// RIGHT
// production: ui/checkout/CheckoutScreen.kt
const val PlaceOrderButtonTag = "CheckoutScreen.PlaceOrderButton"
@Composable
fun CheckoutScreen() {
Button(modifier = Modifier.testTag(PlaceOrderButtonTag), onClick = { /* … */ }) {
Text(stringResource(R.string.place_order))
}
}
// test:
@Test
fun submit_isEnabled_afterFormFilled() {
rule.setContent { CheckoutScreen() }
rule.onNodeWithTag(PlaceOrderButtonTag).assertIsEnabled()
}// WRONG
@Test
fun greeting_appearsTwice() {
rule.setContent { GreetingList(listOf("Hello", "Hello")) }
// Returns the first match silently; never proves "twice".
rule.onNodeWithText("Hello").assertIsDisplayed()
}
// WRONG because: onNode* throws on >1 matches; if the duplicate count drifts to 1 the test
// passes for the wrong reason, and if it drifts to 3 the test fails with an opaque count error.// RIGHT
@Test
fun greeting_appearsTwice() {
rule.setContent { GreetingList(listOf("Hello", "Hello")) }
rule.onAllNodesWithText("Hello").assertCountEquals(2)
}// WRONG
@Test
fun button_text_isShown() {
rule.setContent {
Button(onClick = {}, modifier = Modifier.testTag("submit")) {
Text("Submit", modifier = Modifier.testTag("submitLabel"))
}
}
rule.onNodeWithTag("submitLabel").assertIsDisplayed()
}
// WRONG because: in the merged tree the Text is rolled into the Button. The finder sees zero
// nodes and the auto-attached unmerged hint will surface in the error message — flip the flag.// RIGHT
@Test
fun button_text_isShown() {
rule.setContent { /* same content */ }
rule.onNodeWithTag("submitLabel", useUnmergedTree = true).assertIsDisplayed()
}// WRONG
rule.onNodeWithText("hello").assertIsDisplayed()
// WRONG because: matches only the exact string. UI showing "Hello, world" or "HELLO" produces
// a "no node matched" error.// RIGHT — when intent really is "contains 'Hello'":
rule.onNodeWithText("Hello", substring = true, ignoreCase = true).assertIsDisplayed()
// RIGHTER — prefer a tag and assert the text once:
rule.onNodeWithTag(GreetingTag).assertTextContains("Hello", substring = true, ignoreCase = true)// production:
IconButton(onClick = onShare, modifier = Modifier.testTag(ShareButtonTag)) {
Icon(Icons.Default.Share, contentDescription = stringResource(R.string.share))
}
// test:
rule.onNodeWithTag(ShareButtonTag).assertIsDisplayed() // PREFERRED
rule.onNodeWithContentDescription("Share").assertIsDisplayed() // acceptable for accessibility-focused testsonNodeWithContentDescriptionuseUnmergedTree = trueonRoot()rule.onRoot().printToLog("tree") // dump merged
rule.onRoot(useUnmergedTree = true).printToLog("unmerged")../../debug/printing-the-semantics-tree/SKILL.mdprintToLogprintToStringonNodeWithTagonNodeWithTextonNodeWithContentDescriptionModifier.testTag(...)useUnmergedTree = falsetrueonAllNodesWith*assertCountEqualsonNodeWith*onNode(...).onChild().onChildAt(2).onChild()../traversing-the-semantics-tree/SKILL.mduseUnmergedTree = trueonNodeWithTagconst valuseUnmergedTree = true./gradlew :app:connectedDebugAndroidTest:app:testDebugUnitTestcompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/Finders.ktonNodeWithTagTextContentDescriptiononRootonAllNodesWith*compose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/SemanticsNodeInteractionsProvider.ktonNode(matcher, useUnmergedTree)onAllNodes(matcher, useUnmergedTree)compose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/SemanticsNodeInteraction.ktfetchOneOrThrowgetNodesInUnmergedTreecompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/Filters.kthasTestTaghasTexthasContentDescription