Interoperability with ComposeTestRule
A ComposeTestRule is a TestRule that allows you to test and control composables and applications using Compose. You can read more about Testing your Compose layout here.
The Testify Compose extension provides a default ComposeRule
, or you can specify your own.
To specify your own ComposeRule
instance, pass the instance to the composeTestRule
argument on the ComposableScreenshotRule
constructor.
You have access to the ComposeRule
instance through the setComposeActions()
method. The ComposeRule
is provided as the first argument, composeTestRule
.
Example
- ComposableScreenshotRule
- ComposableScreenshotScenarioRule
class ComposableScreenshotTest {
@get:Rule
val rule = ComposableScreenshotRule(composeTestRule = createAndroidComposeRule(ComposableTestActivity::class.java))
@ScreenshotInstrumentation
@Test
fun default() {
rule
.setCompose {
var text by remember { mutableStateOf("") }
TextField(
value = text,
onValueChange = { text = it },
modifier = Modifier.testTag("field")
)
}
.setComposeActions { composeTestRule ->
composeTestRule.onNodeWithTag("field").performTextInput("testify")
}
.assertSame()
}
}
class ComposableScreenshotTest {
@get:Rule
val rule = ComposableScreenshotRule(composeTestRule = createAndroidComposeRule(ComposableTestActivity::class.java))
@ScreenshotInstrumentation
@Test
fun default() {
launchComposableTestActivity().use { scenario ->
rule
.withScenario(scenario)
.setCompose {
var text by remember { mutableStateOf("") }
TextField(
value = text,
onValueChange = { text = it },
modifier = Modifier.testTag("field")
)
}
.setComposeActions { composeTestRule ->
composeTestRule.onNodeWithTag("field").performTextInput("testify")
}
.assertSame()
}
}
}