Loading...
Loading...
Use this skill to build precise Compose UI test queries by composing `SemanticsMatcher` predicates with `infix and`, `infix or`, and `operator not`, plus the prebuilt filter library (`hasText`, `hasClickAction`, `isEnabled`, `isOn`, `hasTestTag`, `hasContentDescription`, `hasParent`, `hasAnyAncestor`, `hasAnyChild`, `hasAnySibling`, `hasAnyDescendant`, `hasImeAction`, `hasScrollToKeyAction`, `hasScrollToNodeAction`, `isDialog`, `isPopup`, `isRoot`, `isFocused`, `isEditable`, `isHeading`). Covers `SemanticsMatcher.expectValue` / `keyIsDefined` / `keyNotDefined` for custom semantics keys. Use when the developer asks how to find "an enabled button with text Submit", how to combine matchers, how to filter by a custom `SemanticsPropertyKey`, how to write a hierarchical predicate, or mentions `hasParent`, `hasAnyAncestor`, `expectValue`. If the developer wants one matcher instead of three chained assertions, use this skill.
npx skill4agent add skydoves/android-testing-skills composing-semantics-matchersSemanticsMatcher(SemanticsNode) -> BooleanSemanticsMatcher.kt:26andornothasTexthasClickActionnotSemanticsPropertyKeyexpectValuekeyIsDefinedhasParenthasAnyAncestorhasAnyChildhasAnySiblinghasAnyDescendantassertIs*../finding-nodes-by-tag-text-content/SKILL.md../traversing-the-semantics-tree/SKILL.md../../assertions/asserting-node-state-and-text/SKILL.mdandroidx.compose.ui:ui-test../../setup/configuring-test-dependencies/SKILL.mdModifier.semantics { … }Modifier.semantics { customKey = value }commonMain/.../Filters.kt| Signal | Matchers | File:line |
|---|---|---|
| Enabled / disabled | | |
| Toggle (Switch, Checkbox) | | |
| Selection (Tab, RadioButton) | | |
| Focus | | |
| Click action | | |
| Scroll action | | |
| Text actions (TextField) | | |
| Focus action | | |
| Content description | | |
| Text | | |
| A11y / state metadata | | |
| Test tag | | |
| Hierarchical | | |
hasTextSemanticsProperties.TextSemanticsProperties.EditableTextSemanticsProperties.InputTextFilters.kt:229-258hasContentDescriptionFilters.kt:165-188andornotSemanticsMatcher.kt:60-74description(left) && (right)(left) || (right)NOT (inner)SemanticsMatcher.expectValueSemanticsPropertyKey<T>Modifier.semantics { customKey = value }SemanticsMatcher.expectValue(customKey, expectedValue)SemanticsMatcher.kt:33-37SemanticsMatcher.keyIsDefined(customKey)SemanticsMatcher.kt:40-42keyNotDefinedSemanticsMatcher.kt:45-47isOn()expectValue(SemanticsProperties.ToggleableState, On)Filters.kt:61-62onNodeonAllNodesonNode(matcher, useUnmergedTree=false)onAllNodes(matcher, useUnmergedTree=false)SemanticsNodeInteractionsProvider.ktonNodeWithTaghasAnyAncestorhasAnyDescendantonNode(hasText("Discard?") and hasAnyAncestor(isDialog()))onAllNodes(isDialog()).onFirst().onChildren().filter(hasText(...)).onFirst()// WRONG
@Test
fun submit_isEnabledClickable() {
rule.setContent { CheckoutScreen() }
rule.onNodeWithText("Submit")
.assertHasClickAction()
.assertIsEnabled()
}
// WRONG because: each assertion is a separate fetch + error path. Failure prints "no click
// action" without telling the developer the matched node is the *enabled-but-wrong* button on
// a different screen.// RIGHT
@Test
fun submit_isEnabledClickable() {
rule.setContent { CheckoutScreen() }
rule.onNode(hasText("Submit") and hasClickAction() and isEnabled())
.assertExists()
}(Text + InputText + EditableText contains 'Submit' (ignoreCase: false)) && (OnClick is defined) && (is enabled)// WRONG
rule.onAllNodes(isDialog())
.onFirst()
.onChildren()
.filterToOne(hasText("Discard?"))
.assertIsDisplayed()
// WRONG because: it implicitly assumes the Text is a direct child of the dialog root.
// AlertDialog wraps its body in Column/Surface, so the Text is a descendant, not a child.// RIGHT
rule.onNode(hasText("Discard?") and hasAnyAncestor(isDialog()))
.assertIsDisplayed()// "An enabled, non-toggleable Button (so: not a Switch or Checkbox) with text Submit"
rule.onNode(hasText("Submit") and isEnabled() and !isToggleable())
.assertHasClickAction()!matcheroperator fun not()SemanticsMatcher.kt:72-74// production:
val PriorityKey = SemanticsPropertyKey<Int>("Priority")
var SemanticsPropertyReceiver.priority by PriorityKey
@Composable
fun TaskRow(task: Task) {
Row(modifier = Modifier
.testTag(TaskRowTag)
.semantics { priority = task.priority }
) { /* … */ }
}
// test:
rule.onNode(hasTestTag(TaskRowTag) and SemanticsMatcher.expectValue(PriorityKey, 1))
.assertIsDisplayed()
// "this row has a priority assigned at all":
rule.onNode(hasTestTag(TaskRowTag) and SemanticsMatcher.keyIsDefined(PriorityKey))
.assertIsDisplayed()// "the editable TextField that handles Done as IME action"
rule.onNode(hasSetTextAction() and hasImeAction(ImeAction.Done))
.performTextInput("hello")
// "any LazyColumn child reachable by key — i.e. has IndexForKey on top of ScrollToIndex"
rule.onNode(hasScrollToKeyAction()).performScrollToKey(itemKey)hasScrollToKeyActionhasKey(ScrollToIndex) and hasKey(IndexForKey)Filters.kt:415-416hasScrollToNodeActionFilters.kt:419-425val matcher = hasText("Submit") and hasClickAction() and isEnabled()
println(matcher.description)
// (Text + InputText + EditableText contains 'Submit' (ignoreCase: false)) &&
// (OnClick is defined) && (is enabled)assert(matcher)Assertions.kt:254-267infix andinfix oroperator notassertIs*SemanticsMatcher.expectValue(...)SemanticsMatcher.keyIsDefined(...)node.config[CustomKey]hasAnyAncestor(isDialog())hasAnyDescendant(...)../traversing-the-semantics-tree/SKILL.mduseUnmergedTreefalseSemanticsMatcher("is enabled") { … }isEnabled()hasTestTag(SubmitTag) and isEnabled()onNode(matcher)SemanticsMatcher.expectValuekeyIsDefinednode.config[...]./gradlew :app:connectedDebugAndroidTest:app:testDebugUnitTestdescriptioncompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/SemanticsMatcher.ktclass SemanticsMatcherexpectValuekeyIsDefinedkeyNotDefinedinfix andinfix oroperator notcompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/Filters.ktcompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/Assertions.ktfun assert(matcher, messagePrefixOnError)compose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/SemanticsNodeInteractionsProvider.ktonNode(matcher, useUnmergedTree)onAllNodes(matcher, useUnmergedTree)