https://kotlinlang.org logo
#compose
Title
# compose
j

Joram Visser

03/01/2024, 5:30 PM
I’m playing around with the common API for UI testing and I am wondering how to approach the tags for testing you can set on modifiers. It seems by far the easiest way to correctly identify a node. Without it I am writing assertions and actions like:
Copy code
onNodeWithText("Load").assertDoesNotExist()
                onRoot().onChild().onChildAt(0).assertHasClickAction()
                onRoot().onChild().onChildAt(0).performClick()
                onNodeWithText("Load").assertExists()
Which is not really expressive and sensitive to changes. Often composables are a little bit more complicated then this example in the blog post:
Copy code
@Composable
fun MyInfoComposable(info: String, modifier: Modifier) {
    Text(modifier = modifier, text = "[IFNO] $info")
}
With this simple example it is nice to set the modifier test tag from the test like
Copy code
setContent {
            MyInfoComposable("Important things!", modifier = Modifier.testTag("info"))
        }
But I don’t see how that would work for a Composable consisting out of multiple other Composables. What would be a good approach for these more complex Composables? Add
.testTag("tests-are-interested-in-this")
to the production code?
2 Views