Last active
May 17, 2017 16:12
-
-
Save edenman/7fdd32a4d59ccc01185b to your computer and use it in GitHub Desktop.
Espresso ViewAction for Spoon Screenshot
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
import android.app.Activity; | |
import android.content.Context; | |
import android.content.ContextWrapper; | |
import android.support.test.espresso.UiController; | |
import android.support.test.espresso.ViewAction; | |
import android.view.View; | |
import com.squareup.spoon.Spoon; | |
import org.hamcrest.Matcher; | |
import org.hamcrest.Matchers; | |
import static android.support.test.espresso.Espresso.onView; | |
import static android.support.test.espresso.matcher.ViewMatchers.isRoot; | |
public final class SpoonScreenshotAction implements ViewAction { | |
private final String tag; | |
private final String testClass; | |
private final String testMethod; | |
public SpoonScreenshotAction(String tag, String testClass, String testMethod) { | |
this.tag = tag; | |
this.testClass = testClass; | |
this.testMethod = testMethod; | |
} | |
@Override public Matcher<View> getConstraints() { | |
return Matchers.anything(); | |
} | |
@Override public String getDescription() { | |
return "Taking a screenshot using spoon."; | |
} | |
@Override public void perform(UiController uiController, View view) { | |
Spoon.screenshot(getActivity(view), tag, testClass, testMethod); | |
} | |
private static Activity getActivity(View view) { | |
Context context = view.getContext(); | |
while (!(context instanceof Activity)) { | |
if (context instanceof ContextWrapper) { | |
context = ((ContextWrapper) context).getBaseContext(); | |
} else { | |
throw new IllegalStateException("Got a context of class " | |
+ context.getClass() | |
+ " and I don't know how to get the Activity from it"); | |
} | |
} | |
return (Activity) context; | |
} | |
/** This must be called directly from your test method. */ | |
public static void perform(String tag) { | |
StackTraceElement[] trace = Thread.currentThread().getStackTrace(); | |
String testClass = trace[3].getClassName(); | |
String testMethod = trace[3].getMethodName(); | |
onView(isRoot()).perform(new SpoonScreenshotAction(tag, testClass, testMethod)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In Android 7+ the
getActivity(view)
code fails becauseview.getContext()
does not have access to the enclosing activity anymore.Instead, the code below works in Android 7+ and 6: