Function

assertTextEquals

Asserts that the node's list of text values contains exactly the given values and nothing else.

assertTextEqualsSample

@Sampled
fun assertTextEqualsSample() {
    composeTestRule.setContent {
        // Explicitly merging descendants to demonstrate list semantics
        Row(Modifier.semantics(mergeDescendants = true) { testTag = "textRow" }) {
            Text("Hello")
            Text("World")
        }
    }

    // The merged text list is: ["Hello", "World"]

    // We provide all items exactly as they appear in the merged list.
    // Order does not matter.
    composeTestRule.onNodeWithTag("textRow").assertTextEquals("World", "Hello")
}

assertTextEqualsWithInputTextSample

@Sampled
fun assertTextEqualsWithInputTextSample() {
    composeTestRule.setContent {
        // Explicitly merging descendants to demonstrate list semantics
        Row(Modifier.semantics(mergeDescendants = true) { testTag = "textRow" }) {
            Text("First Name:")
            Text("Compose")
        }

        // Simulating a password field where the visual text is masked,
        // but the raw user input is stored in InputText.
        Box(
            Modifier.semantics {
                text = AnnotatedString("(000) 123-4567")
                inputText = AnnotatedString("0001234567")
                testTag = "phoneField"
            }
        )
    }

    // The merged text list is: ["First Name:", "Compose"]

    // We provide all items exactly as they appear in the merged list.
    composeTestRule.onNodeWithTag("textRow").assertTextEquals("Compose", "First Name:")

    // By default, assertTextEquals evaluates Text (and EditableText),
    // but ignores InputText. The evaluated list is simply ["(000) 123-4567"].
    composeTestRule.onNodeWithTag("phoneField").assertTextEquals("(000) 123-4567")

    // We explicitly opt in to evaluating the InputText property.
    composeTestRule
        .onNodeWithTag("phoneField")
        .assertTextEquals("0001234567", "(000) 123-4567", includeInputText = true)
}
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: