Instrumentation tests usage
These examples use pedrovgs/Shot. It also works with any other on-device screenshot testing library that supports ActivityScenarios, like
Facebook screenshot-tests-for-android
Dropbox Dropshots
Other custom screenshot testing solution based on ActivityScenarios/FragmentScenarios.
You can find more complete examples with Shot, Dropshots, Roborazzi and Android-Testify in the Android screenshot testing playground repo.
Activity
The simplest way is to use the ActivityScenarioForActivityRule, to avoid the need for closing the ActivityScenario.
@get:Rule
val screenshotRule =
activityScenarioForActivityRule<MyActivity>(
config = ActivityConfigItem(
orientation = Orientation.LANDSCAPE,
uiMode = UiMode.NIGHT,
fontSize = FontSize.LARGEST,
systemLocale = "en",
displaySize = DisplaySize.LARGEST,
fontWeight = FontWeight.BOLD,
)
)
@Test
fun snapActivityTest() {
compareScreenshot(
activity = screenshotRule.activity,
name = "your_unique_screenshot_name",
)
}In case you don't want to/cannot use the rule, you can use ActivityScenarioConfigurator.ForActivity() directly in the test.
Apart from that, this would be equivalent
Fragment
The simplest way is to use the fragmentScenarioConfiguratorRule
In case you don't want to/cannot use the rule, you can use the plain FragmentScenarioConfigurator. This would be its equivalent:
Android View
The simplest way is to use the ActivityScenarioForViewRule, to avoid the need for closing the ActivityScenario.
In case you don't want to/cannot use the rule, you can use ActivityScenarioConfigurator.ForView(). This would be its equivalent:
Jetpack Compose
The simplest way is to use the ActivityScenarioForComposableRule, to avoid the need for:
calling createEmptyComposeRule()
closing the ActivityScenario.
In case you don't want to/cannot use the rule, you can use ActivityScenarioConfigurator.ForComposable() together with createEmptyComposeRule(). This would be its equivalent:
With Dropshots
If you are using a screenshot library that cannot take a ComposeTestRule as argument (e.g. Dropshots), you can still screenshot the Composable as follows:
or
Rendering elevation
Most screenshot testing libraries use Canvas under the hood with Bitmap.Config.ARGB_8888 as default for generating bitmaps (i.e. the screenshots) from the Activities/Fragments/ViewHolders/Views/Dialogs/Composables...
That's because Canvas is supported in all Android versions.
Nevertheless, such bitmaps generated using Canvas have some limitations, e.g. UI elements are rendered without considering elevation (e.g. without shadows). So, how to render screenshots with elevation?
Bitmap
Fortunately, most libraries let you pass a bitmap of the UI as an argument in their record/verify methods. In doing so, we can draw the views with elevation to a bitmap by usingPixelCopy.
Robolectric 4.10+ cannot render shadows or elevation with RNG even with PixelCopy, as stated in this issue
AndroidUiTestingUtils provides methods to easily generate bitmaps from the Activities/Fragments/ViewHolders/Views/Dialogs/Composables:
drawToBitmap(config = Bitmap.Config.ARGB_8888)-> usesCanvasunder the hooddrawToBitmapWithElevation(config = Bitmap.Config.ARGB_8888)-> usesPixelCopyunder the hood
and one extra to fully screenshot a scrollable Android View:
drawFullScrollableToBitmap(config = Bitmap.Config.ARGB_8888)-> usesCanvasunder the hood
Differences between Bitmaps generated via Canvas and Pixel Copy might be specially noticeable on API 31:

Using PixelCopy instead of Canvas comes with its own drawbacks though. In general, don't use PixelCopy to draw views that don't fit on the screen.
✅ Can render elements beyond the screen, e.g. long ScrollViews
❌ Cannot render elements beyond the screen, resizing them to fit in the window
if that's the case
❌ Ignores elevation of UI elements
✅ Renders elevation of UI elements
And using PixelCopy in your screenshot tests is as simple as this:
Last updated