Take a screenshot on Android Espresso Test Failure

This tutorial will show you how to add a test rule to your Espresso Android automation, when a test fails it takes a screenshot.

  1. Create a new folder in androidTest/utility/screenshot
  2. Add these two files EspressoScreenshot.kt and ScreenshotTestRule.kt to the folder (shown below).
  3. Add write permission to Manifest, to save the screenshot file in the device.

4. Add the ScreenshoTestRule to your test.

5. Force one of your tests to fail, and run the test suite. Done, if one test fails you can check the Android device /sdcard/screenshots/ to see a screenshot of the device during the test failure.

Create the below files:



Finally, add the screenshot rule to your test class:

How to get the screenshot from the device(locally):

If you run on a local device the screenshot gets save in the device: /sdcard/screenshots/ you can use the below Android debug bridge(ADB) command to pull the screenshots from the device:

How to get the screenshot from the device(Flank/Firebase TestLab):

If you run your test in Firebase TestLab with flank, and a test fails at the end of the test run you will see a “Screenshots” tab on Firebase console test results.


Check out the project flank and run the test_app. Shows how to take a screenshot of test failure. If you run this example on firebase testlab it will automatically show your screenshots on the firebase testlab test results tab. https://github.com/TestArmada/flank/tree/master/test_app/app/src/androidTest/kotlin/com/example/test_app

flank documentation

software engineer, read, swim, travel. apps: https://goo.gl/rDuuQw https://goo.gl/15GvfE

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store