Skip to content

Instantly share code, notes, and snippets.

@RussellCollins
Last active August 29, 2015 14:23
Show Gist options
  • Save RussellCollins/3a047ab0a23f789a2baf to your computer and use it in GitHub Desktop.
Save RussellCollins/3a047ab0a23f789a2baf to your computer and use it in GitHub Desktop.
package com.testexample.RunListeners
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.UiDevice;
import org.junit.runner.Description;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import java.io.File;
import java.IOException;
/**
* Use this RunListener when you want to capture screenshots on test failures for UI tests.
* To specify one or more RunListeners to observe the test run: -e listener com.foo.Listener,com.foo.Listener2
*
*/
public class ExampleScreenshotOnUITestFailureListener extends RunListener {
Description desc;
public void testStarted (Description description) { desc = description; }
public void testFailure (Failure failure) throws IOException {
// build filename from class and method using .png as the file type
String name = String.format("%s.%s.png",
desc.getClassName(),
desc.getMethodName());
// my tests all use String OUTPUT_DIR = "/sdcard/test_output_files/" for screenshots and other artifacts
File file = new File(OUTPUT_DIR, name);
// take the screenshot already
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).takeScreenshot(file);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment