Skip to content

Instantly share code, notes, and snippets.

@mandrachek
Last active August 29, 2015 14:23
Show Gist options
  • Save mandrachek/d8048795f600515c25a5 to your computer and use it in GitHub Desktop.
Save mandrachek/d8048795f600515c25a5 to your computer and use it in GitHub Desktop.
capture dialog
import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
import android.view.View;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import static android.support.test.espresso.Espresso.onView;
public final class SpoonDialogAction implements ViewAction {
private final String tag;
private final String testClass;
private final String testMethod;
private final boolean useUiAutomator;
public SpoonDialogAction(String tag, String testClass, String testMethod, boolean useUiAutomator) {
this.tag = tag;
this.testClass = testClass;
this.testMethod = testMethod;
this.useUiAutomator = useUiAutomator;
}
@Override
public Matcher<View> getConstraints() {
return Matchers.any(View.class);
}
@Override public String getDescription() {
return "Taking a screenshot using spoon.";
}
@Override public void perform(UiController uiController, View view) {
uiController.loopMainThreadUntilIdle();
Spoon.screenshot(view, tag, testClass, testMethod, useUiAutomator);
}
@Deprecated
/** this version is deprecated - if you call it early on in your test, your screenshot will be empty - you need to wait for one of your views to be displayed **/
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 ForkDialogAction(tag, testClass, testMethod, true));
}
public static void perform(String tag, Matcher<View> matcher) {
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
String testClass = trace[3].getClassName();
String testMethod = trace[3].getMethodName();
onView(matcher).perform(new ForkDialogAction(tag, testClass, testMethod, true));
}
public static void perform(String tag, Matcher<View> matcher, Boolean useUiAutomator) {
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
String testClass = trace[3].getClassName();
String testMethod = trace[3].getMethodName();
onView(matcher).perform(new ForkDialogAction(tag, testClass, testMethod, useUiAutomator));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment