Interface

TestFailureHandler

Handles Compose UI test failures for custom diagnostics or artifact processing.

testFailureHandlerSample

@Sampled
fun testFailureHandlerSample() {
    val customFailureHandler = TestFailureHandler { context ->
        val storage = PlatformTestStorageRegistry.getInstance()

        context.artifacts.forEach { artifact ->
            when (artifact.type) {
                FailureArtifact.Type.Screenshot -> {
                    // Example: Read the screenshot bytes to upload to a custom dashboard
                    // val inputStream = storage.openInputFile(artifact.fileName)
                }
                FailureArtifact.Type.UiHierarchy -> {
                    // Example: Get the URI to share or process further
                    // val uri = storage.getOutputFileUri(artifact.fileName)
                }
            }
        }
    }

    val testConfig =
        ComposeUiTestConfig(
            failurePolicy =
                TestFailurePolicy(
                    screenshotCaptureMode = CaptureMode.Enabled,
                    uiHierarchyCaptureMode = CaptureMode.Enabled,
                    failureHandlers = listOf(customFailureHandler),
                )
        )

    runComposeUiTest(config = testConfig) {
        setContent { /* Your Compose UI here */ }

        // If this assertion fails, the framework will:
        // 1. Take a screenshot
        // 2. Dump the UI hierarchy
        // 3. Call customFailureHandler.onTestFailed
        onNodeWithTag("non_existent_button").assertExists()
    }
}
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: