Support Center

Taking Screenshots with Espresso v1.x/2.0

Last Updated: Oct 19, 2016 09:23AM CEST
As Espresso v1.x or v2.0 doesn't have internal function call dedicated for screenshot capturing the user must use or implement something for themselves. We'll take a look at basic example of this in this blog. Also, it recommended that if you are starting from the scratch and/or you don't have prior experience with Espresso v1.x/Espresso v2.0 please check out the installation/setup instructions first.

NOTE! Remember that if you implement your own screenshot taking functions you should store the captured screenshots under /sdcard/test-screenshots/ to ensure those get shown in Testdroid Cloud Results view -> Screenshots widget.

import android.view.View;
import android.os.Environment;

public static void takeScreenshot(String name, Activity activity)

    // In Testdroid Cloud, taken screenshots are always stored
    // under /test-screenshots/ folder and this ensures those screenshots
    // be shown under Test Results
    String path =
        Environment.getExternalStorageDirectory().getAbsolutePath() + "/test-screenshots/" + name;

    View scrView = activity.getWindow().getDecorView().getRootView();
    Bitmap bitmap = Bitmap.createBitmap(scrView.getDrawingCache());

    OutputStream out = null;
    File imageFile = new File(path);

    try {
        out = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
    } catch (FileNotFoundException e) {
        // exception
    } catch (IOException e) {
        // exception
    } finally {

        try {
            if (out != null) {

        } catch (Exception exc) {


In addition to get this working and avoid FileNotFoundException each and every time you call this function, you should check that Android manifest file includes the following line:

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

To call the function you just add the following line with proper classname reference to your Espresso test:

<classname>.takeScreenshot("screenshot-001", getActivity());

Note! If you use the same name for file to be written (captured screenshot) the older one gets rewritten. Make sure whenever you call this function you have a new name for the captured screenshot file.
In order to understand the full instructions how to use Testdroid Cloud devices with Espresso v1.x or Espresso v2.0 please check out those step-by-step instructions. Also, you can now run your tests directly from Android Studio using Testdroid Gradle plugin.   This awesome plugin eliminates the need for any manual effort in order to build and get your APK up and running on physical devices.

Contact Us
seconds ago
a minute ago
minutes ago
an hour ago
hours ago
a day ago
days ago
Invalid characters found