Skip to content

It’s awesome to see many of you using Google’s Espresso (or Espresso v2.0) on Bitbar Testing. We do get some questions every now and then about the ability to take screenshots during the test so I’ll share this brief example to showcase an example function call that can be used as a part of your test.

Screen Shot 2015-05-13 at 10.52.13 AM

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 a 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 Bitbar Testing Results view -> Screenshots widget. If you want to configure the path where screenshots are written this can be done during Step #4: Advanced options when starting a new test run on Bitbar Testing:

Screen Shot 2015-05-13 at 12.01.24 PM

All right, let’s look at the code example for a simple screenshot taking a function with Java:

import java.io.File;
import java.io.FileOutputStream;
import android.graphics.Bitmap;
import android.view.View;
import android.os.Environment;
public static void takeScreenshot(String name, Activity activity)
{
    // In Bitbar Testing, taken screenshots are always stored
    // under /test-screenshots/ folder and this ensures those screenshots
    // be shown under Test Results
    String path =
        Environment.getExternalStorageDirectory() + "/test-screenshots/" + name + ”.png”;
    View scrView = activity.getWindow().getDecorView().getRootView();
    scrView.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(scrView.getDrawingCache());
    scrView.setDrawingCacheEnabled(false);
    OutputStream out = null;
    File imageFile = new File(path);
    try {
        out = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.flush();
    } catch (FileNotFoundException e) {
        // exception
    } catch (IOException e) {
        // exception
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (Exception exc) {
        }
    }
}

Beyond getting this working and avoiding FileNotFoundException each and every time you call this function, you should also check that the 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 class name references to your Espresso test:

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


Note!
 If you use the same name for the 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.

Screen Shot 2015-05-13 at 10.36.06 AM

In order to understand the full instructions on how to use Bitbar devices with Espresso v1.x or Espresso v2.0 please check out these step-by-step instructions. Also, you can now run your tests directly from Android Studio using Bitbar 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.

Happy Espresso-Testing!

Ville-Veikko Helppi

Mobile Testing Product Expert