Visual evidence can shorten an Android UI test failure from guesswork to one image. In an Espresso run, a screenshot captured after the target screen is visible preserves the text, layout, and state that the assertion reached on the device.
Current AndroidX Test screenshot code should capture a matched view with ViewActions.captureToBitmap and save the bitmap with writeToTestStorage. That path replaces the deprecated runner Screenshot API and lets the test runner export the image as instrumentation output instead of leaving it only in device storage.
Start from an app module that already runs instrumented tests with AndroidJUnitRunner. Capturing the root view after an Espresso assertion keeps synchronization in the test path, and the exported .png under the module's build/outputs tree confirms that CI can upload the image.
Steps to capture screenshots in Espresso tests:
- Add the current AndroidX Test dependencies to the app module's app/build.gradle.kts file.
- build.gradle.kts
dependencies { androidTestImplementation( "androidx.test:core:1.7.0" ) androidTestImplementation( "androidx.test.ext:junit:1.3.0" ) androidTestImplementation( "androidx.test.espresso:espresso-core:3.7.0" ) }
Keep these coordinates in the project's version catalog if the project already centralizes dependency versions.
- Create an instrumented test that waits for the target state and writes the root view bitmap to test storage.
- CheckoutScreenshotTest.kt
@Test fun captureReceiptScreen() { onView(withText("Receipt")).check(matches(isDisplayed())) onView(isRoot()).perform( captureToBitmap { bitmap -> bitmap.writeToTestStorage("checkout_receipt") ) ) }
Use the snippet inside an AndroidJUnit4 instrumented test class with the imports for writeToTestStorage and ViewActions.captureToBitmap. writeToTestStorage appends .png to the supplied base name.
- Run the connected Android test task for the module and variant.
$ ./gradlew :app:connectedDebugAndroidTest Starting 1 tests on Pixel_8_API_35 CheckoutScreenshotTest > captureReceiptScreen PASSED BUILD SUCCESSFUL in 42s
Replace :app and Debug with the module and variant used by the project. The device or emulator must be online before the connected test task starts.
- Find the exported screenshot in the module build output.
$ find app/build/outputs -name 'checkout_receipt*' -print app/build/outputs/connected_android_test_additional_output/ ##### snipped ##### checkout_receipt.png
Managed-device and connected-device tasks can use different output directory names, so search under the module's build/outputs directory before hard-coding a CI artifact path.
- Verify that the exported file is a PNG image from the directory printed by the previous command.
$ file checkout_receipt.png checkout_receipt.png: PNG image data, 1080 x 2400 ##### snipped #####
Use the matched path in the CI artifact upload step so the screenshot stays attached to the test run that produced it.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.