Loading...
Loading...
Use this skill to diagnose "no node matched", "found N nodes", and "useUnmergedTree" failures by dumping the actual semantics tree with `printToLog(tag, maxDepth)` and `printToString(maxDepth)`. Covers the output grammar (`Node
npx skill4agent add skydoves/android-testing-skills printing-the-semantics-treeonNodeWithTagThread.sleepprintToLogprintToString"Failed: assertExists. Reason: Expected exactly '1' node but could not find any node that satisfies: …""Reason: Expected exactly '1' node but found 'N' nodes that satisfy: …""…were found in the unmerged tree. If you really wanted to match against merged tree, use useUnmergedTree = true."SemanticsNodeThread.sleep../../synchronization/synchronizing-with-idle/SKILL.md../../synchronization/testing-animations-deterministically/SKILL.md../../finders/finding-nodes-by-tag-text-content/SKILL.md../enabling-accessibility-checks/SKILL.mdandroidx.compose.ui:ui-testandroidTestImplementationtestImplementationcommonMainComposeTestRuleComposeUiTestrule.setContent { … }rule.onRoot().printToLog("DEBUG")fun SemanticsNodeInteraction.printToLog(tag: String, maxDepth: Int = Int.MAX_VALUE)compose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/Output.kt:62-69adb logcat -s DEBUGrule.onRoot(useUnmergedTree = true).printToLog("DEBUG")IconButtonOutput.kt:135-296Node #<id> at (l=<L>, t=<T>, r=<R>, b=<B>)px, Tag: '<tag>'
<Sorted ConfigKey> = '<value>'
...
[<FlagKey1>, <FlagKey2>] // keys whose value is Unit
Actions = [<ActionKey1>, ...] // AccessibilityActions and Function values
MergeDescendants = 'true' // when isMergingSemanticsOfDescendants
ClearAndSetSemantics = 'true' // when isClearingSemantics
|-Node #<childId> at (...)px // children indented with " |-"Node #Tag: '<testTag>'AccessibilityActionFunction<*>Actions = [keyName, …]UnitDisabledIsTraversalGroup[Disabled, IsTraversalGroup, …]MergeDescendants = 'true'ClearAndSetSemantics = 'true'onNode(...)compose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/SemanticsNodeInteraction.kt:184-194useUnmergedTree = true../../finders/finding-nodes-by-tag-text-content/SKILL.mdfetchSemanticsNodefetchSemanticsNodesSemanticsNodeval node = rule.onNodeWithTag("title").fetchSemanticsNode()
assertThat(node.config[SemanticsProperties.Text]).isNotEmpty()
val all = rule.onAllNodesWithTag("row").fetchSemanticsNodes(
atLeastOneRootRequired = true,
errorMessageOnFail = "expected at least one row",
)fun fetchSemanticsNode(errorMessageOnFail: String? = null): SemanticsNodefun fetchSemanticsNodes(atLeastOneRootRequired: Boolean = true, errorMessageOnFail: String? = null): List<SemanticsNode>maxDepthrule.onAllNodesWithTag("row").printToLog("DEBUG", maxDepth = Int.MAX_VALUE)Output.kt:83-116val tree = rule.onRoot(useUnmergedTree = true).printToString()
println(tree) // or attach to a test reportThread.sleep// WRONG
@Test
fun appears() {
rule.setContent { MyScreen() }
Thread.sleep(2000) // hide the symptom
rule.onNodeWithTag("submit").assertIsDisplayed() // still fails
}
// WRONG because: Thread.sleep desyncs from MainTestClock and does not advance composition.
// If the node is missing, more wall time will not produce it. The cause is structural.
// Skydoves directive #7: Thread.sleep is a smell.// RIGHT
@Test
fun appears() {
rule.setContent { MyScreen() }
rule.onRoot(useUnmergedTree = true).printToLog("DEBUG") // see what's actually present
rule.onNodeWithTag("submit").assertIsDisplayed()
}// WRONG — the test ignores the framework hint
@Test
fun iconExists() {
rule.setContent { Button(onClick = {}) { Icon(Icons.Default.Add, modifier = Modifier.testTag("add-icon")) } }
rule.onNodeWithTag("add-icon").assertExists()
// AssertionError: ... 0 matches.
// These nodes were found in the unmerged tree:
// Node #4 ... Tag: 'add-icon'
// If you really wanted to match against merged tree, use useUnmergedTree = true.
}
// WRONG because: a Button merges descendants. The Icon's tag is collapsed into the Button
// node in the merged tree. The error message names the fix in plain English.// RIGHT
@Test
fun iconExists() {
rule.setContent { Button(onClick = {}) { Icon(Icons.Default.Add, modifier = Modifier.testTag("add-icon")) } }
rule.onNodeWithTag("add-icon", useUnmergedTree = true).assertExists()
}// WRONG
rule.onNodeWithTag("row").assertIsDisplayed()
// AssertionError: Reason: Expected exactly '1' node but found '3' nodes that satisfy:
// (TestTag = 'row')// RIGHT — dump first, then narrow the matcher
rule.onAllNodesWithTag("row").printToLog("DEBUG", maxDepth = Int.MAX_VALUE)
// Read the dump to find a stable disambiguator (e.g. parent tag, contentDescription),
// then narrow:
rule.onAllNodesWithTag("row")
.filterToOne(hasAnyAncestor(hasTestTag("section-completed")))
.assertIsDisplayed()fetchSemanticsNode// RIGHT
@Test
fun rowConfigContainsCustomKey() {
rule.setContent { /* … */ }
val node = rule.onNodeWithTag("row").fetchSemanticsNode("missing 'row'")
val custom: String? = node.config.getOrNull(MyCustomKey)
assertThat(custom).isEqualTo("expected")
}rule.onRoot().printToLog("DEBUG")maxDepthInt.MAX_VALUEOutput.kt:43, 65"…were found in the unmerged tree. If you really wanted to match against merged tree, use useUnmergedTree = true."useUnmergedTree = trueSemanticsNodeInteraction.kt:184-194Thread.sleep(…)maxDepthprintToLogSemanticsNodeInteractionCollection0Output.kt:85, 112useUnmergedTree = trueuseUnmergedTree = falsefetchSemanticsNode(errorMessageOnFail = "…")printToLog("DEBUG")Thread.sleepuseUnmergedTree = truefilterToOnehasAnyAncestortestTagfetchSemanticsNode(errorMessageOnFail = "…")printToLogprintToStringcompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/Output.kt:41-116compose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/Output.kt:135-296compose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/SemanticsNodeInteraction.kt:178-194fetchSemanticsNode(s)compose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/SemanticsNodeInteraction.kt