Take Screenshots from Firebase Test Lab for Android Instrumentation Tests

Firebase Test Lab for Android 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, right-click your project name, and 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 then paste it into the aars folder.
  4. At the root of your test project, open build.gradle, and then add a reference to the aars folder to every repositories section in this file so that each section matches the following example:

    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 then add a dependency on cloudtestingscreenshotter_lib.aar to the top-level dependencies section as shown in the following example:

    dependencies {
        // Cloud testing
        androidTestCompile (name:'cloudtestingscreenshotter_lib', ext:'aar')
        // Other dependencies go here
        }
    

  6. Open AndroidManifest.xml, and then add the following lines between the <manifest> and </manifest> elements:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>

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):

ScreenShotter.takeScreenshot("main_screen_2", getActivity());

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.

Send feedback about...

Need help? Visit our support page.