Fork me on GitHub

On-demand Screenshots during Android Integration Tests

Visual design is important to Android applications, and though devising tests for design is hard, you 'can' make visual checks much easier by capturing screenshots from your device during your normal testing. The android-maven-plugin includes support for taking screenshots whenever requested to do so by your instrumentation test code. This functionality is supplied by GitHub’s Android Screenshot library, which uses ddmlib to capture screenshots.

The screenshot capture speed is about 1 frame per second, so you can’t get full motion video, but all screenshots captured for a particular test run are also compressed into an animated gif. This gif can be posted to your build radiator or dev chatroom on each build, to give quick visual feedback on the current state of the app.


Getting a screenshot is simple - you just invoke poseForScreenshot() when you want one :

import static;

    public void testAppearance() {
        Instrumentation instrumentation = getInstrumentation();

        sleep(500); // robotium provides neater ways of waiting for the activity to initialise


The poseForScreenshot() method is a small dependency you need to add to your integration-test project, available from the Central Repository as the [ android-screenshot-celebrity] library.

The rest of the test is just setting up the activity so it’s ready to have it’s screenshot taken - ie startActivitySync() and then waiting a short period for the activity to be ready. The screenshots will be placed in a 'screenshots' folder under your instrumentation test target directory, for example:


They’re named sequentially by default (0000.png, 0001.png, etc), but you can set an explicit name for the shot using poseForScreenshotNamed("foobar") if you like. In addition, for each android device used in the tests, an animated-gif will be created showing all screenshots taken.


The morseflash example project has an example test that takes screenshots and there’s also an example robotium test in the open-source [] android app by GitHub.