Take screenshots in instrumentation tests

Firebase Test Lab includes a library that you can use to take screenshots when running instrumentation tests, such as tests written using the Espresso test framework. To add this capability to your test, download the cloudtestingscreenshotter_lib.aar library.

The ability to take screenshots is already incorporated into the test APK, app-debug-test-unaligned.apk, for the NotePad sample app, and screenshots are also captured when you run Robo test. The following instructions tell you how to add the screenshot library to your app and how to call that library from your test.

After your test has run, you can review the screenshots in Android Studio or in the Firebase console.

Add the screenshot library to your project

  1. In Android Studio, open the Project view and right-click your project name. Then, click New > Directory.

  2. In the New Directory dialog, type aars. This creates an aars directory at the root of your test project (as a peer directory to the app folder).

  3. Copy cloudtestingscreenshotter_lib.aar and paste it into the aars folder.

  4. In your app's root-level (project-level) build.gradle file, add a reference to the aars folder in every repositories block:

    repositories {
        jcenter()
        flatDir {
            dirs '../aars'
        }
    }
    ...
  5. In your module's top-level directory (for the NotePad example app, this is the app directory), open the build.gradle file and add a dependency to cloudtestingscreenshotter_lib.aar to the top-level dependencies block:

    dependencies {
        // Cloud testing
        androidTestCompile (name:'cloudtestingscreenshotter_lib', ext:'aar')
        // Other dependencies go here
        }
    
  6. In your AndroidManifest.xml file, specify system permissions for your app by adding the following lines within the <manifest> tag. If you're testing on Android 10 (API level 29) or higher, omit the WRITE_EXTERNAL_STORAGE permission (your app does not require this permission in order to read and write screenshots to the device).

    <manifest ... >
       <!-- WRITE_EXTERNAL_STORAGE is not needed on Android 10 (API level 29) or higher. -->
       <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
       <uses-permission android:name="android.permission.INTERNET"/>
       ...
    </manifest>
    

Take a screenshot from your test

At any point in your test where you want to take a screenshot, call the ScreenShotter.takeScreenshot method from the cloudtestingscreenshotter_lib library, where the first argument is a label that you use to identify the screenshot (main_screen_2 is used in the following example):

Java

ScreenShotter.takeScreenshot("main_screen_2", this /* activity */);

Kotlin+KTX

ScreenShotter.takeScreenshot("main_screen_2", this /* activity */)

View screenshots

After your test has completed, you can compare any screenshots taken during testing by selecting an element in the test results tree and then clicking the View Screenshots View Screenshots option.

Screenshot comparison screen

You can select and compare screenshots from different configurations as follows:

Task Action
Switch between test executions. Use the drop-down menu in the top-left corner.

test case menu
Switch between screenshots within a test execution. Use the arrows in the top-right corner.

screenshot switcher
Add additional screenshot comparison panels to the current view. Click Compare.

Compare
Choose a different test dimension (device type, orientation, locale, etc.). Select a new dimension member from the list at the bottom of the screenshot.

Next step

Download the NotePad sample app.