Hey all. I’m a little confused as to how to do ce...
# compose
m
Hey all. I’m a little confused as to how to do certain things to test compose layouts. I see the rules available in the test module, however a typical test would use ID values and validate the contents of the view (such as it’s visibility, it’s content, etc..). Based on the documentation it doesn’t look like you can do that. Selecting nodes based on their text content seems problematic at best (which is why we avoid in current espresso tests). I’m also trying to figure out how to do robolectric testing using FragmentScenario, since we would typically find views of the fragment object that’s being spun up. I’m wondering if layoutId on modifier is the way to do this?
b
The replacement for finding a view by ID for testing purposes would be to set a testTag semantic property (https://developer.android.com/reference/kotlin/androidx/compose/ui/semantics/package-summary#testtag)
Copy code
@Composable
fun TitleText(title: String) {
  Text(
    text = title,
    modifier = Modifier.semantics {
      testTag = "Title"
    }
}
Then in your test:
Copy code
onNode(hasTag("Title").assert( ... )
These docs are a pretty good intro to testing with Compose: https://developer.android.com/jetpack/compose/testing#matchers
m
I’ve been reading that, but it only mentions testTag once, and it’s buried in a code snippet with no explanation. And even that just says “hasTestTag(“Drummer”)”