Simon Stahl

02/02/2022, 12:24 AM
Hi. I wonder if there is a way to retrieve a semantic object within a test and assert its values.
Say I have a composable with a text like this:
val ItemKey = SemanticsPropertyKey<Item>("Item")
var SemanticsPropertyReceiver.item by ItemKey

data class Item(val id: Int, val name: String)

    modifier = Modifier.semantics {
        this.testTag = "test"
        this.item = Item(99, "TestXXX")
    text = "My text"
And now in my test, I want to retrieve the
object and assert its values
composeTestRule.onNodeWithTag("test").assert(???, 99)
Anyone has a tip for how to do that?
There are other SemanticsMatchers for other things, and you can write your own assertions - just look at how the ones in the stdlib are implemented

Simon Stahl

02/02/2022, 10:29 PM
Nice, that's easier than I thought for once 🙂 Thanks for the help, I appreciate it
For reference if someone else is looking for this. you can create your own assert helper method. for above example it could look like this
fun SemanticsNodeInteraction.assertItem(description: String, check: (Item) -> Boolean) {
    assert(SemanticsMatcher(description) {
and then i can write my tests like this
composeTestRule.onNodeWithTag("test").assertItem("Checking id") { == 99