mreichelt / android-screenshots-in-jvm-tests

How to take screenshots in Android JVM (unit) tests using Robolectric

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Talk: Android Screenshots in JVM tests

How to take screenshots in Android JVM (unit) tests using Robolectric and Roborazzi.

This is a talk for the Android Testing Awesomeness event from GDG Rhein-Main. Please reach out to me on Mastodon or Twitter if you have questions or feedback!

To follow along:

  1. Open this project in Android Studio
  2. See tests in the unit test directory
  3. Run the tests individually (follow guide below)

CanvasTest

Renders a Bitmap by drawing on a Canvas and creates a snapshot as a PNG file.

  • This is a regular JVM unit test - no Android devices/emulators required!
  • This uses Robolectric native mode (introduced in Robolectric 4.10) - this uses a native Skia rendering engine from Android, which is fast and more close to what Android actually renders!
  • Note the annotations: @RunWith(RobolectricTestRunner::class) is needed to run this via Robolectric, @GraphicsMode(GraphicsMode.Mode.NATIVE) is needed to enable the new native rendering, and @Config(sdk = [33]) is needed because Robolectric 4.10.3 does not support 34 yet.
  • Bonus: set a breakpoint to get access to the bitmap variable, start test in debugger, and click on 'View Bitmap'!

ViewTest

Renders a simple TextView in an empty Activity.

  • The setUp method shows how to set up any activity class and control the lifecycle manually. The other tools will do this for you!
  • The image has a low resolution because the Robolectric default is mdpi. You can add a qualifiers = "xxxhdpi" parameter to the @Config to improve it.

ActivityTest

Renders the ClassicViewActivity (with a good old Android TextView) using Roborazzi.

  • It doesn't do anything at first - because Roborazzi has different modes you can activate:
    • record will create the (golden) files,
    • compare will compare with your golden files (and create diff images),
    • and verify will make the test fail when something is different.
  • To capture, run: ./gradlew :app:recordRoborazziDebug
    • or: add roborazzi.test.record=true to your gradle.properties, then run test
  • Note that this is using the Espresso testing framework. Yes, you can use Espresso in your JVM unit tests!
  • Robolectric comes with some nice qualifiers you can use, like RobolectricDeviceQualifiers.Pixel4

ComposeActivityTest

Renders an activity that uses Jetpack Compose.

  • all the finders, like onRoot(), work here as well!

ComposeTest

Renders Composables. Yay Compose!

  • no activity required - even cleaner code!
  • the last test is using captureRoboImage from robolectric-compose - no Rule required!

LottieTest

Renders a Lottie animation from unit tests - capturing multiple frames!

  • set images = 42 and see the generated images
  • I used this to debug a Lottie animation that was set up with custom translations using setTextDelegate, to see that the translations were actually being rendered.

Using Roborazzi to check your Compose upgrade

  • Run ./gradlew :app:recordRoborazziDebug to record
  • Upgrade Compose from 2023.08.00 to 2023.10.00
  • Run ./gradlew :app:verifyRoborazziDebug to verify upgrade

nowinandroid

To see more real-world examples using Robolectric & Roborazzi:

  1. Open https://github.com/android/nowinandroid in Android Studio
  2. Open ForYouScreenScreenshotTests
  3. Run ./gradlew :feature:foryou:recordRoborazziDebug to record the PNG files or ./gradlew :feature:foryou:compareRoborazziDebug

That's all folks!

Look at https://github.com/takahirom/roborazzi to see all the amazing other things it can do - like capturing single views, advanced CI stuff, recording GIFs and more!

If you liked this talk, please follow and ping me on Mastodon or Twitter. Thank you!

Bonus: I can wholeheartedly recommend the workshop Mastering screenshot testing libraries for Android by Sergio Sastre Flórez - he shows Roborazzi and more screenshot testing libraries in more detail!

About

How to take screenshots in Android JVM (unit) tests using Robolectric

License:Apache License 2.0


Languages

Language:Kotlin 100.0%