In a Compose UI test, why does onNodeWithTag miss a test tag on a Text inside a clickable row, and what does useUnmergedTree change?
answer
- two trees, one default
- clickable merges its descendants
- text merges, test tags do not
- finder flag defaults to false
- printToLog on onRoot(useUnmergedTree = true)
basics
~20 sA clickable row merges its descendants, so in the default merged tree the child Text vanishes into the row; its text merges up, its test tag never does. useUnmergedTree = true searches the tree where the child is still its own node.
solid answer
~40 sA clickable row or a `Button` sets `mergeDescendants`, so in the **merged** semantics tree its children disappear and their properties are merged into the row's node. Text and content descriptions are concatenated into the row, but the `TestTag` property's merge policy keeps only the parent's own value, so a tag on the inner `Text` is gone and `onNodeWithTag("voice-part-label")` finds nothing. Finders default to `useUnmergedTree = false`; passing `true` searches the unmerged tree, where the label is still a separate node with its tag and text. I'd keep acting on the merged row, which is what accessibility services see, and use the unmerged tree only to inspect a child. `onRoot(useUnmergedTree = true).printToLog("TAG")` shows both shapes.
code
plaintext · 18 linesMerged tree (default), abridged:
Node #1 at (...)px
|-Node #2 at (...)px, Tag: 'row-alto-tue'
Text = '[Alto, Tue 19:00]'
Actions = [GetTextLayoutResult, OnClick]
MergeDescendants = 'true'
Unmerged tree (useUnmergedTree = true), abridged:
Node #1 at (...)px
|-Node #2 at (...)px, Tag: 'row-alto-tue'
Actions = [OnClick]
MergeDescendants = 'true'
|-Node #3 at (...)px, Tag: 'voice-part-label'
| Text = '[Alto]'
| Actions = [GetTextLayoutResult]
|-Node #4 at (...)px
Text = '[Tue 19:00]'
Actions = [GetTextLayoutResult]go deeper
Recall that clickable components merge their children and that finders read the merged tree unless useUnmergedTree is true.
Explain per-property merge policies: text and content descriptions append to the parent, test tags never merge, so a child's tag disappears.
Diagnose a failing finder from printToLog output of both trees, and decide whether to retag the merging node or read the unmerged tree.
Set a team convention for where tags go, on the merging node or on children, so tests stay readable and match what accessibility users get.
## Two views of one semantics tree Compose keeps one semantics tree but lets tests read it in two shapes: - The **unmerged tree** has a node for every composable that contributes semantics: the row, each `Text` inside it, each icon. - The **merged tree** simplifies it. A node marked with `mergeDescendants = true` absorbs its descendants: in the merged tree those descendants disappear, and their properties are merged into the parent's configuration. A descendant that is itself marked `mergeDescendants` stays as its own node. Buttons and other components a user activates as one thing, such as clickable rows and form fields, set this flag. The ui library's own documentation says that for most purposes, especially accessibility and testing accessibility, the merged tree is the one to use. Compose test finders follow that advice: every finder has a `useUnmergedTree` parameter that defaults to `false`. ## How each property merges Merging is per property, and each semantics property declares its own merge policy: | Property | Merge policy | What the merged row shows | |---|---|---| | `Text` | child values are appended to the parent's list | `Text = '[Alto, Tue 19:00]'` | | `ContentDescription` | appended the same way | all descriptions in one list | | `TestTag` | the parent's value is kept, the child's is dropped | only the row's own tag, if it has one | The `TestTag` rule is deliberate: the ui source comments that tags are never merged so that internal test tags do not leak to parents. That is the whole answer to the classic interview puzzle. Put `Modifier.testTag("voice-part-label")` on a `Text` inside a clickable rehearsal row, and in the merged tree the `Text` node no longer exists while its tag was not copied to the row, so `onNodeWithTag("voice-part-label")` matches nothing and the next action or assertion throws an `AssertionError`. The failure message adds a hint when the unmerged tree does contain a match, which points straight at the cause. The same merging explains a second surprise. `onNodeWithText("Alto")` *does* find the row, because the row's merged `Text` list contains an item equal to "Alto". But `assertTextEquals("Alto")` on that node fails: `assertTextEquals` requires the exact list of values, so it must be `assertTextEquals("Alto", "Tue 19:00")`. ## What useUnmergedTree = true changes 1. The finder searches the unmerged tree, where the label is its own node with its own `TestTag` and `Text`. 2. Assertions on that node see only the child's properties, so `assertTextEquals("Alto")` passes there. 3. Nothing about the app changes; it is purely which shape of the tree the test reads. ## Choosing which tree to use - Act on the merged node for clicks and most assertions; it is the element a TalkBack user focuses and activates, so the test checks what they get. - Use the unmerged tree when the test must inspect one part of a merged component, such as the label or a badge inside a row, or when debugging. - Prefer moving the tag to the merging node (the row) over tagging its children, if the test only needs the row. - Do not treat `useUnmergedTree = true` as a fix for a finder that matches several nodes; it usually makes ambiguity worse, because more nodes exist in the unmerged tree. ## clearAndSetSemantics, the stronger version `Modifier.clearAndSetSemantics { ... }` goes further than merging. In the merged tree it clears the semantic information of the node's descendants and keeps only what its block sets, which is how a card can expose one clean description instead of a dozen tiny parts. In the unmerged tree nothing is actually cleared: the node is only marked, so tests and debugging can still inspect the children. A tag or text on such a child is therefore reachable only with `useUnmergedTree = true`, and `printToLog` marks the clearing node with `ClearAndSetSemantics = 'true'`. If a test cannot find text that is plainly visible on screen, a clearing modifier above it is one of the first things to look for. ## Reading the tree when a finder fails `composeTestRule.onRoot().printToLog("RehearsalTree")` prints the merged tree to logcat, and `onRoot(useUnmergedTree = true).printToLog(...)` prints the unmerged one. Each line shows a node id, its bounds, `Tag: '...'` when it has a tag, its properties, its `Actions = [...]` and `MergeDescendants = 'true'` on merging nodes. Comparing the two printouts shows at a glance which children were absorbed and which properties survived the merge. ## Common misreadings - "The child's tag is merged into the row" is false for `TestTag`, though true for text. - "Finders search both trees" is false; they search the merged tree unless told otherwise. - "The unmerged tree is what accessibility uses" is backwards; accessibility reads the merged tree.
- Why does onNodeWithText("Alto") find the row while onNodeWithTag("voice-part-label") does not?Because the two properties merge differently. `Text` uses an appending merge policy, so the row's merged node carries `[Alto, Tue 19:00]` and `hasText("Alto")` matches any item in that list. `TestTag` keeps the parent's value and drops the child's, so no node in the merged tree carries the label's tag.
- How would you make the row easy to find without reading the unmerged tree?Put `Modifier.testTag` on the row itself, the node that merges, so the tag survives in the merged tree; or find the row by its user-visible text with `onNode(hasText("Alto") and hasClickAction())`. Reserve `useUnmergedTree = true` for assertions about one part of the row.
A sealed parcel: the courier sees one box whose shipping label lists every item packed inside, because the packing list is copied to the outside, but the stickers on the individual items stay inside the box. Opening the box (the unmerged tree) is the only way to read an item's sticker.
- A child's test tag is merged into its clickable parent like its text
- Compose finders search the merged and unmerged trees by default
- The unmerged tree is what TalkBack reads
- assertTextEquals("Alto") passes on a merged row that also shows a time
- useUnmergedTree = true fixes a finder that matches several nodes