Skip to content

Instantly share code, notes, and snippets.

@eevajonnapanula
Created June 3, 2024 03:41
Show Gist options
  • Save eevajonnapanula/b1fc376d09fa08504a0fc65ddc7b01ef to your computer and use it in GitHub Desktop.
Save eevajonnapanula/b1fc376d09fa08504a0fc65ddc7b01ef to your computer and use it in GitHub Desktop.
class ClickableTest {
@get:Rule
val composeTestRule = createComposeRule()
@Test
fun hasRoleNameValue() {
composeTestRule.setContent {
ModifiersExampleTheme {
ClickableScreen()
}
}
val clickableElement =
composeTestRule.onNode(hasTestTag("clickable"))
// Assert accessible name
clickableElement.assertTextEquals("Bookmark this item")
// Assert role
clickableElement.assert(
SemanticsMatcher("has correct role") {
it.config.getOrNull(SemanticsProperties.Role) == Role.Button
},
)
// Assert state description
clickableElement.assertStateDescription("Not bookmarked")
clickableElement.performClick()
clickableElement.assertStateDescription("Bookmarked")
}
private fun SemanticsNodeInteraction.assertStateDescription(
stateDescription: String
) =
assert(
SemanticsMatcher("has correct state description") {
it.config.getOrNull(SemanticsProperties.StateDescription) == stateDescription
},
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment