Loading...
Loading...
Use this skill to navigate from one Compose semantics node to its relatives via `onParent`, `onChildren`, `onChild`, `onChildAt`, `onSibling`, `onSiblings`, `onAncestors`, plus the collection helpers `onFirst`, `onLast`, `filter`, `filterToOne`, and the `[index]` operator. Covers when to traverse vs when to add a stable `testTag`, the LazyColumn/LazyRow caveat (only currently composed children appear), the absence of a singular `onAncestor`, and the sticky `useUnmergedTree` flag across navigation. Use when the developer mentions `onChildren`, `onChild`, `onParent`, `onSiblings`, `onAncestors`, `filterToOne`, `onFirst`, `onLast`, brittle child-index chains, or asks how to find the second child of a Row, the parent of a Text, or any sibling of a node. If the developer is dot-chaining navigation through a layout, use this skill.
npx skill4agent add skydoves/android-testing-skills traversing-the-semantics-treeonNodeWithTagLazyColumntestTag.onChildren()[i].onChildAt(j)onChildrenonChildonParentonSiblingonSiblingsonAncestorsfilterToOneonFirstonLast[index]LazyColumnModifier.testTag(...)../finding-nodes-by-tag-text-content/SKILL.mdhasAnyAncestorhasAnyDescendant../composing-semantics-matchers/SKILL.mdperformScrollToKey../../actions/clicking-and-scrolling/SKILL.md../../patterns/testing-lazy-lists/SKILL.mdComposeTestRuleComposeUiTest../../setup/configuring-test-dependencies/SKILL.md../finding-nodes-by-tag-text-content/SKILL.mdSemanticsNodeInteractionSemanticsNodeInteractionCollection| From a single node, go to … | API | Returns | File:line |
|---|---|---|---|
| parent | | single | |
| exactly one child | | single | |
| child at an index | | single | |
| all currently-composed children | | collection | |
| exactly one sibling | | single | |
| all siblings | | collection | |
| every ancestor up to root | | collection | |
| From a collection, narrow to … | API | Returns | File:line |
|---|---|---|---|
| first | | single | |
| last | | single | |
| nth | | single | |
| filter to a sub-collection | | collection | |
| filter to exactly one | | single | |
onAncestoronAncestors()Selectors.kt:140-146[parent, grandparent, …, root]onParent().assert(matcherX)hasParent(matcherX)useUnmergedTreeuseUnmergedTreeSemanticsNodeInteractionSemanticsNodeInteraction.kt:42-49Selectors.ktuseUnmergedTreeonNodeWithTag(tag, useUnmergedTree = true)onChildonParentfilteronChildren()Selectors.kt:46-52LazyColumnLazyRowperformScrollToIndexperformScrollToKeyperformScrollToNode../../actions/clicking-and-scrolling/SKILL.md[2].onChildAt(0)filterToOne(matcher)filterToOneonNode(matcher)Selectors.kt:198-206onNode(matcher)[index]// WRONG
@Test
fun second_avatar_isVisible() {
rule.setContent { ProfileGrid(profiles = profiles) }
rule.onNodeWithTag("ProfileGrid")
.onChildren()[2]
.onChildren()[0]
.assertIsDisplayed()
}
// WRONG because: layout shuffling (a header inserted, an extra wrapper, a Spacer added)
// silently changes which node is being asserted. The test passes for the wrong reason.// RIGHT
// production:
@Composable
fun ProfileGrid(profiles: List<Profile>) {
LazyColumn(modifier = Modifier.testTag(ProfileGridTag)) {
itemsIndexed(profiles) { index, profile ->
Row(modifier = Modifier.testTag("$AvatarTagPrefix$index")) {
Avatar(profile, modifier = Modifier.testTag("$AvatarImageTagPrefix$index"))
}
}
}
}
// test:
rule.onNodeWithTag("$AvatarImageTagPrefix${1}").assertIsDisplayed()@Test
fun row_third_child_isIcon() {
rule.setContent {
Row(modifier = Modifier.testTag("toolbar")) {
Text("Title")
Spacer(Modifier.weight(1f))
Icon(Icons.Default.Share, contentDescription = "Share")
}
}
rule.onNodeWithTag("toolbar")
.onChildren()
.assertCountEquals(3)
rule.onNodeWithTag("toolbar")
.onChildAt(2)
.assertContentDescriptionEquals("Share")
}onChildAt(index)onChildren()[index]Selectors.kt:85filterToOne[i]// WRONG
rule.onAllNodesWithTag(RowTag).onChildren().filter(hasClickAction())[0]
.assertHasClickAction()
// WRONG because: indexing into a filtered collection silently passes when the filter returns
// many. The test asserts only "at least one is clickable", not "exactly one".// RIGHT
rule.onAllNodesWithTag(RowTag).onChildren()
.filterToOne(hasClickAction())
.assertHasClickAction()@Test
fun submitText_isInsideEnabledButton() {
rule.setContent {
Button(onClick = {}, modifier = Modifier.testTag("submit"), enabled = true) {
Text("Submit", modifier = Modifier.testTag("submitLabel"))
}
}
rule.onNodeWithTag("submitLabel", useUnmergedTree = true)
.onParent() // sticky: stays unmerged
.assertIsEnabled()
.assertHasClickAction()
}useUnmergedTree = trueTextonParent()onAncestors()@Test
fun confirmButton_isInsideDialog() {
rule.setContent { ConfirmDialog() }
rule.onNodeWithTag(ConfirmButtonTag)
.onAncestors()
.filterToOne(isDialog())
.assertExists()
}rule.onNode(hasTestTag(ConfirmButtonTag) and hasAnyAncestor(isDialog())).assertExists()../composing-semantics-matchers/SKILL.md// WRONG
rule.onNodeWithTag(ListTag).onChildren().assertCountEquals(1000)
// WRONG because: onChildren() returns a snapshot of currently composed children. A LazyColumn
// only composes the visible viewport plus a small prefetch buffer, so the count is window-sized,
// not data-set-sized.// RIGHT — assert the visible count, OR scroll first then assert by tag
rule.onNodeWithTag(ListTag).performScrollToIndex(999)
rule.onNodeWithTag("$ItemTagPrefix${999}").assertIsDisplayed()../../patterns/testing-lazy-lists/SKILL.mdonSibling()@Test
fun checkbox_label_isPresent() {
rule.setContent {
Row {
Checkbox(checked = true, onCheckedChange = {},
modifier = Modifier.testTag("agreeBox"))
Text("I agree", modifier = Modifier.testTag("agreeLabel"))
}
}
rule.onNodeWithTag("agreeBox")
.onSibling()
.assertTextEquals("I agree")
}onSibling()Selectors.kt:114-125onSiblings()filterToOne(...)Modifier.testTag(...)filterToOne(matcher)filter(matcher).onFirst()onChildren()LazyColumnLazyRowperformScrollToIndexperformScrollToKeyonAncestoronAncestors()onParent()hasParent(matcher)hasAnyAncestor(matcher)useUnmergedTreeonChildonParentfilterhasAnyAncestorhasAnyDescendant../composing-semantics-matchers/SKILL.mdonChildren()[i]LazyColumnLazyRowperformScrollTo*onAncestoronParentonAncestorshasAnyAncestorfilterToOnefilter().onFirst()useUnmergedTree = true./gradlew :app:connectedDebugAndroidTest:app:testDebugUnitTestcompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/Selectors.ktonParentonChildrenonChildonChildAtonSibling(s)onAncestorsonFirstonLastfilterfilterToOnecompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/SemanticsNodeInteraction.kt[index]useUnmergedTreecompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/Filters.kthasParenthasAnyChildhasAnySiblinghasAnyAncestorhasAnyDescendant