Last active
August 29, 2015 14:23
-
-
Save RussellCollins/3a047ab0a23f789a2baf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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