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.
Traversing the Semantics Tree — When a Single Finder Won't Reach the Node
The single-finder shortcuts (onNodeWithTag, etc.) cover ~95% of test queries. The remainder need tree navigation: "the parent of this Text", "the third child of this Row", "any sibling that is enabled". This skill maps those navigation operators, calls out the LazyColumn snapshot caveat, and shows when traversal is the wrong tool.
When to use this skill
The target node has no stable testTag and one cannot be added to production (third-party composable, dynamically generated children).
The test verifies structural relationships ("the second child of the row is the icon").
The developer chains .onChildren()[i].onChildAt(j) and wants to know whether that is the right shape.
The developer mentions onChildren, onChild, onParent, onSibling, onSiblings, onAncestors, filterToOne, onFirst, onLast, or [index].
A LazyColumn test misses items because they are off-screen.
When NOT to use this skill
A Modifier.testTag(...) could be added to the target node — adding a tag is almost always cleaner than a traversal chain. See ../finding-nodes-by-tag-text-content/SKILL.md.
The relationship is "anywhere above" / "anywhere below" — prefer the matcher-based hasAnyAncestor / hasAnyDescendant (see ../composing-semantics-matchers/SKILL.md).
The query is about a LazyColumn item by key — use performScrollToKey instead (see ../../actions/clicking-and-scrolling/SKILL.md, ../../patterns/testing-lazy-lists/SKILL.md).
Prerequisites
A working ComposeTestRule / ComposeUiTest. See ../../setup/configuring-test-dependencies/SKILL.md.
Familiarity with the merged vs unmerged tree distinction. See ../finding-nodes-by-tag-text-content/SKILL.md.
Workflow
1. Pick the navigator by relationship type. Each operator returns either a SemanticsNodeInteraction (single — fails on 0 or >1) or a SemanticsNodeInteractionCollection (plural — never fails on 0).
From a single node, go to …
API
Returns
File:line
parent
onParent()
single
Selectors.kt:36-42
exactly one child
onChild()
single
Selectors.kt:71-77
child at an index
onChildAt(index)
single
Selectors.kt:85
all currently-composed children
onChildren()
collection
Selectors.kt:53-59
exactly one sibling
onSibling()
single
Selectors.kt:119-125
all siblings
onSiblings()
collection
Selectors.kt:101-107
every ancestor up to root
onAncestors()
collection
Selectors.kt:140-146
From a collection, narrow to …
API
Returns
File:line
first
onFirst() (= [0])
single
Selectors.kt:156-158
last
onLast()
single
Selectors.kt:168-170
nth
[index]
single
SemanticsNodeInteraction.kt:261-267
filter to a sub-collection
filter(matcher)
collection
Selectors.kt:178-186
filter to exactly one
filterToOne(matcher)
single
Selectors.kt:198-206
2. Do not look for onAncestor (singular) — it does not exist. The API surface ships only onAncestors() plural (Selectors.kt:140-146). It returns [parent, grandparent, …, root] in that order. To assert "the immediate parent satisfies X", use onParent().assert(matcherX) or the hasParent(matcherX) predicate.
3. Remember the useUnmergedTree flag is sticky. Every navigator carries the same useUnmergedTree value as the source SemanticsNodeInteraction (SemanticsNodeInteraction.kt:42-49; each Selectors.kt constructor passes useUnmergedTree through unchanged). A finder created with onNodeWithTag(tag, useUnmergedTree = true) keeps the flag on across onChild/onParent/filter. Skydoves hot take #2: default merged, flip to unmerged only when targeting composition detail.
4. onChildren() is a snapshot at invocation time. It returns nodes "currently present in the semantic tree" (Selectors.kt:46-52). For a LazyColumn or LazyRow only the on-screen items appear. To reach an off-screen item, scroll first with performScrollToIndex / performScrollToKey / performScrollToNode (see ../../actions/clicking-and-scrolling/SKILL.md).
5. Re-finding by tag usually beats navigating. If the tree shape might change between Compose versions or under translation rotation, a stable tag on the target node is more durable than a [2].onChildAt(0) chain. Add the tag in production. Skydoves hot take #1.
6. Use filterToOne(matcher) when the count assertion is implicit.filterToOne throws on 0 or >1 matches at fetch time, the same way onNode(matcher) does (Selectors.kt:198-206). It is the collection-narrowing analogue of onNode(matcher).
Patterns
Pattern: prefer a tag over a deep [index] chain
// 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.
onChildAt(index) is exactly onChildren()[index] (Selectors.kt:85). Both fail if the index is out of range or if the resolved node count is not exactly 1 at the leaf.
Pattern: filterToOne instead of [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()
Pattern: assert the parent role from a known child
PREFERRED: rule.onNode(hasTestTag(ConfirmButtonTag) and hasAnyAncestor(isDialog())).assertExists() — same intent in one matcher. See ../composing-semantics-matchers/SKILL.md.
Pattern: LazyColumn — only on-screen children appear
// 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()
See ../../patterns/testing-lazy-lists/SKILL.md for the full LazyColumn workflow.
Pattern: onSibling() to assert "the row's other half"
onSibling() requires exactly one sibling (Selectors.kt:114-125). For multiple siblings use onSiblings() plus filterToOne(...).
Mandatory rules
MUST prefer adding a Modifier.testTag(...) to the target node over a multi-step traversal chain. Skydoves hot take #1.
MUST use filterToOne(matcher) over filter(matcher).onFirst() when the contract is "exactly one match"; the former throws on >1, the latter silently picks the first.
MUST NOT assume onChildren() returns the full data set for a LazyColumn / LazyRow — it returns the currently-composed snapshot only. Scroll first with performScrollToIndex / performScrollToKey.
MUST NOT look up a singular onAncestor — only onAncestors() plural exists. Use onParent() for the immediate parent or hasParent(matcher) / hasAnyAncestor(matcher) as predicates.
MUST remember the useUnmergedTree flag is sticky across onChild/onParent/filter. Skydoves hot take #2.
PREFERRED: for "anywhere above/below" relationships, replace traversal chains with hasAnyAncestor / hasAnyDescendant from ../composing-semantics-matchers/SKILL.md.
Verification
Every traversal chain has at most one navigation step, OR the deeper chain is justified by a comment naming the missing tag.
No onChildren()[i] chain spans a LazyColumn / LazyRow boundary without a preceding performScrollTo*.
No onAncestor (singular) usage — only onParent, onAncestors, or hasAnyAncestor.
filterToOne is used wherever the contract is "exactly one match"; filter().onFirst() is replaced.
useUnmergedTree = true on a chain root is intentional — there is a comment or matcher reason for it.
./gradlew :app:connectedDebugAndroidTest or :app:testDebugUnitTest passes.
compose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/SemanticsNodeInteraction.kt — collection [index] operator and the sticky useUnmergedTree field.
1---2name: traversing-the-semantics-tree3description: 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.4license: Apache-2.0. See LICENSE for complete terms.5---67# Traversing the Semantics Tree — When a Single Finder Won't Reach the Node89The single-finder shortcuts (`onNodeWithTag`, etc.) cover ~95% of test queries. The remainder need tree navigation: "the parent of this Text", "the third child of this Row", "any sibling that is enabled". This skill maps those navigation operators, calls out the `LazyColumn` snapshot caveat, and shows when traversal is the wrong tool.1011## When to use this skill1213- The target node has no stable `testTag` and one cannot be added to production (third-party composable, dynamically generated children).14- The test verifies structural relationships ("the second child of the row is the icon").15- The developer chains `.onChildren()[i].onChildAt(j)` and wants to know whether that is the right shape.16- The developer mentions `onChildren`, `onChild`, `onParent`, `onSibling`, `onSiblings`, `onAncestors`, `filterToOne`, `onFirst`, `onLast`, or `[index]`.17- A `LazyColumn` test misses items because they are off-screen.1819## When NOT to use this skill2021- A `Modifier.testTag(...)` could be added to the target node — adding a tag is almost always cleaner than a traversal chain. See `../finding-nodes-by-tag-text-content/SKILL.md`.22- The relationship is "anywhere above" / "anywhere below" — prefer the matcher-based `hasAnyAncestor` / `hasAnyDescendant` (see `../composing-semantics-matchers/SKILL.md`).23- The query is about a LazyColumn item by key — use `performScrollToKey` instead (see `../../actions/clicking-and-scrolling/SKILL.md`, `../../patterns/testing-lazy-lists/SKILL.md`).2425## Prerequisites2627- A working `ComposeTestRule` / `ComposeUiTest`. See `../../setup/configuring-test-dependencies/SKILL.md`.28- Familiarity with the merged vs unmerged tree distinction. See `../finding-nodes-by-tag-text-content/SKILL.md`.2930## Workflow3132- [ ] **1. Pick the navigator by relationship type.** Each operator returns either a `SemanticsNodeInteraction` (single — fails on 0 or >1) or a `SemanticsNodeInteractionCollection` (plural — never fails on 0).3334 | From a single node, go to … | API | Returns | File:line |35 |---|---|---|---|36 | parent | `onParent()` | single | `Selectors.kt:36-42` |37 | exactly one child | `onChild()` | single | `Selectors.kt:71-77` |38 | child at an index | `onChildAt(index)` | single | `Selectors.kt:85` |39 | all currently-composed children | `onChildren()` | collection | `Selectors.kt:53-59` |40 | exactly one sibling | `onSibling()` | single | `Selectors.kt:119-125` |41 | all siblings | `onSiblings()` | collection | `Selectors.kt:101-107` |42 | every ancestor up to root | `onAncestors()` | collection | `Selectors.kt:140-146` |4344 | From a collection, narrow to … | API | Returns | File:line |45 |---|---|---|---|46 | first | `onFirst()` (= `[0]`) | single | `Selectors.kt:156-158` |47 | last | `onLast()` | single | `Selectors.kt:168-170` |48 | nth | `[index]` | single | `SemanticsNodeInteraction.kt:261-267` |49 | filter to a sub-collection | `filter(matcher)` | collection | `Selectors.kt:178-186` |50 | filter to exactly one | `filterToOne(matcher)` | single | `Selectors.kt:198-206` |5152- [ ] **2. Do not look for `onAncestor` (singular) — it does not exist.** The API surface ships only `onAncestors()` plural (`Selectors.kt:140-146`). It returns `[parent, grandparent, …, root]` in that order. To assert "the immediate parent satisfies X", use `onParent().assert(matcherX)` or the `hasParent(matcherX)` predicate.5354- [ ] **3. Remember the `useUnmergedTree` flag is sticky.** Every navigator carries the same `useUnmergedTree` value as the source `SemanticsNodeInteraction` (`SemanticsNodeInteraction.kt:42-49`; each `Selectors.kt` constructor passes `useUnmergedTree` through unchanged). A finder created with `onNodeWithTag(tag, useUnmergedTree = true)` keeps the flag on across `onChild`/`onParent`/`filter`. Skydoves hot take #2: default merged, flip to unmerged only when targeting composition detail.5556- [ ] **4. `onChildren()` is a snapshot at invocation time.** It returns nodes "currently present in the semantic tree" (`Selectors.kt:46-52`). For a `LazyColumn` or `LazyRow` only the on-screen items appear. To reach an off-screen item, scroll first with `performScrollToIndex` / `performScrollToKey` / `performScrollToNode` (see `../../actions/clicking-and-scrolling/SKILL.md`).5758- [ ] **5. Re-finding by tag usually beats navigating.** If the tree shape might change between Compose versions or under translation rotation, a stable tag on the target node is more durable than a `[2].onChildAt(0)` chain. Add the tag in production. Skydoves hot take #1.5960- [ ] **6. Use `filterToOne(matcher)` when the count assertion is implicit.** `filterToOne` throws on 0 or >1 matches at fetch time, the same way `onNode(matcher)` does (`Selectors.kt:198-206`). It is the collection-narrowing analogue of `onNode(matcher)`.6162## Patterns6364### Pattern: prefer a tag over a deep `[index]` chain6566```kotlin67// WRONG68@Test69fun second_avatar_isVisible() {70 rule.setContent { ProfileGrid(profiles = profiles) }71 rule.onNodeWithTag("ProfileGrid")72 .onChildren()[2]73 .onChildren()[0]74 .assertIsDisplayed()75}76// WRONG because: layout shuffling (a header inserted, an extra wrapper, a Spacer added)77// silently changes which node is being asserted. The test passes for the wrong reason.78```7980```kotlin81// RIGHT82// production:83@Composable84fun ProfileGrid(profiles: List<Profile>) {85 LazyColumn(modifier = Modifier.testTag(ProfileGridTag)) {86 itemsIndexed(profiles) { index, profile ->87 Row(modifier = Modifier.testTag("$AvatarTagPrefix$index")) {88 Avatar(profile, modifier = Modifier.testTag("$AvatarImageTagPrefix$index"))89 }90 }91 }92}9394// test:95rule.onNodeWithTag("$AvatarImageTagPrefix${1}").assertIsDisplayed()96```9798### Pattern: traversal when no tag is available99100```kotlin101@Test102fun row_third_child_isIcon() {103 rule.setContent {104 Row(modifier = Modifier.testTag("toolbar")) {105 Text("Title")106 Spacer(Modifier.weight(1f))107 Icon(Icons.Default.Share, contentDescription = "Share")108 }109 }110111 rule.onNodeWithTag("toolbar")112 .onChildren()113 .assertCountEquals(3)114115 rule.onNodeWithTag("toolbar")116 .onChildAt(2)117 .assertContentDescriptionEquals("Share")118}119```120121`onChildAt(index)` is exactly `onChildren()[index]` (`Selectors.kt:85`). Both fail if the index is out of range or if the resolved node count is not exactly 1 at the leaf.122123### Pattern: `filterToOne` instead of `[i]`124125```kotlin126// WRONG127rule.onAllNodesWithTag(RowTag).onChildren().filter(hasClickAction())[0]128 .assertHasClickAction()129// WRONG because: indexing into a filtered collection silently passes when the filter returns130// many. The test asserts only "at least one is clickable", not "exactly one".131```132133```kotlin134// RIGHT135rule.onAllNodesWithTag(RowTag).onChildren()136 .filterToOne(hasClickAction())137 .assertHasClickAction()138```139140### Pattern: assert the parent role from a known child141142```kotlin143@Test144fun submitText_isInsideEnabledButton() {145 rule.setContent {146 Button(onClick = {}, modifier = Modifier.testTag("submit"), enabled = true) {147 Text("Submit", modifier = Modifier.testTag("submitLabel"))148 }149 }150151 rule.onNodeWithTag("submitLabel", useUnmergedTree = true)152 .onParent() // sticky: stays unmerged153 .assertIsEnabled()154 .assertHasClickAction()155}156```157158The `useUnmergedTree = true` set on the inner `Text` finder propagates to `onParent()` automatically — no need to repeat it.159160### Pattern: `onAncestors()` for the chain to root161162```kotlin163@Test164fun confirmButton_isInsideDialog() {165 rule.setContent { ConfirmDialog() }166167 rule.onNodeWithTag(ConfirmButtonTag)168 .onAncestors()169 .filterToOne(isDialog())170 .assertExists()171}172```173174PREFERRED: `rule.onNode(hasTestTag(ConfirmButtonTag) and hasAnyAncestor(isDialog())).assertExists()` — same intent in one matcher. See `../composing-semantics-matchers/SKILL.md`.175176### Pattern: LazyColumn — only on-screen children appear177178```kotlin179// WRONG180rule.onNodeWithTag(ListTag).onChildren().assertCountEquals(1000)181// WRONG because: onChildren() returns a snapshot of currently composed children. A LazyColumn182// only composes the visible viewport plus a small prefetch buffer, so the count is window-sized,183// not data-set-sized.184```185186```kotlin187// RIGHT — assert the visible count, OR scroll first then assert by tag188rule.onNodeWithTag(ListTag).performScrollToIndex(999)189rule.onNodeWithTag("$ItemTagPrefix${999}").assertIsDisplayed()190```191192See `../../patterns/testing-lazy-lists/SKILL.md` for the full LazyColumn workflow.193194### Pattern: `onSibling()` to assert "the row's other half"195196```kotlin197@Test198fun checkbox_label_isPresent() {199 rule.setContent {200 Row {201 Checkbox(checked = true, onCheckedChange = {},202 modifier = Modifier.testTag("agreeBox"))203 Text("I agree", modifier = Modifier.testTag("agreeLabel"))204 }205 }206207 rule.onNodeWithTag("agreeBox")208 .onSibling()209 .assertTextEquals("I agree")210}211```212213`onSibling()` requires exactly one sibling (`Selectors.kt:114-125`). For multiple siblings use `onSiblings()` plus `filterToOne(...)`.214215## Mandatory rules216217- **MUST** prefer adding a `Modifier.testTag(...)` to the target node over a multi-step traversal chain. Skydoves hot take #1.218- **MUST** use `filterToOne(matcher)` over `filter(matcher).onFirst()` when the contract is "exactly one match"; the former throws on >1, the latter silently picks the first.219- **MUST NOT** assume `onChildren()` returns the full data set for a `LazyColumn` / `LazyRow` — it returns the currently-composed snapshot only. Scroll first with `performScrollToIndex` / `performScrollToKey`.220- **MUST NOT** look up a singular `onAncestor` — only `onAncestors()` plural exists. Use `onParent()` for the immediate parent or `hasParent(matcher)` / `hasAnyAncestor(matcher)` as predicates.221- **MUST** remember the `useUnmergedTree` flag is sticky across `onChild`/`onParent`/`filter`. Skydoves hot take #2.222- **PREFERRED:** for "anywhere above/below" relationships, replace traversal chains with `hasAnyAncestor` / `hasAnyDescendant` from `../composing-semantics-matchers/SKILL.md`.223224## Verification225226- [ ] Every traversal chain has at most one navigation step, OR the deeper chain is justified by a comment naming the missing tag.227- [ ] No `onChildren()[i]` chain spans a `LazyColumn` / `LazyRow` boundary without a preceding `performScrollTo*`.228- [ ] No `onAncestor` (singular) usage — only `onParent`, `onAncestors`, or `hasAnyAncestor`.229- [ ] `filterToOne` is used wherever the contract is "exactly one match"; `filter().onFirst()` is replaced.230- [ ] `useUnmergedTree = true` on a chain root is intentional — there is a comment or matcher reason for it.231- [ ] `./gradlew :app:connectedDebugAndroidTest` or `:app:testDebugUnitTest` passes.232233## References234235- Compose testing overview: https://developer.android.com/develop/ui/compose/testing236- Compose testing cheat sheet: https://developer.android.com/develop/ui/compose/testing-cheatsheet237- Semantics in Compose: https://developer.android.com/develop/ui/compose/accessibility/semantics238- `compose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/Selectors.kt` — `onParent`, `onChildren`, `onChild`, `onChildAt`, `onSibling(s)`, `onAncestors`, `onFirst`, `onLast`, `filter`, `filterToOne`.239- `compose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/SemanticsNodeInteraction.kt` — collection `[index]` operator and the sticky `useUnmergedTree` field.240- `compose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/Filters.kt` — `hasParent`, `hasAnyChild`, `hasAnySibling`, `hasAnyAncestor`, `hasAnyDescendant` predicate alternatives to traversal.
Run npx skillmds@latest add skydoves/traversing-the-semantics-tree in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
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. It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free. This skill is licensed under Apache-2.
skydoves (@skydoves) published this skill. Their other Agent Skills are listed on their SkillMD profile.