解决方法:
@H_403_0@这就是我现在这样做的方式:@H_403_0@public class MainScreenTest extends BaseStatelessBlackBoxEspressoTest<LaunchActivity> { public MainScreentest() { super(LaunchActivity.class); } public voID testMainScreen() { // Unfortunately this must be explicitly called in each test :-( setUpFailureHandler(); onVIEw(withID(R.ID.main_circle)). check(matches(isdisplayed())); }}
@H_403_0@我的基础Espresso测试类设置自定义FailureHandler(我喜欢使用基类来保存许多其他常用代码):@H_403_0@public abstract class BaseStatelessBlackBoxEspressoTest<T extends Activity> extends BaseBlackBoxTest<T> { public BaseStatelessBlackBoxEspressoTest(Class clazz) { super(clazz); } @Before public voID setUp() throws Exception { super.setUp(); getActivity(); } public voID setUpFailureHandler() { // Get the test class and method. These have to match those of the test // being run, otherwise the screenshot will not be displayed in the Spoon // HTML output. We cannot call this code directly in setUp, because at // that point the current test method is not yet in the stack. StackTraceElement[] trace = Thread.currentThread().getStackTrace(); String testClass = trace[3].getClassname(); String testMethod = trace[3].getmethodname(); Espresso.setFailureHandler(new CustomFailureHandler( getInstrumentation().getTargetContext(), testClass, testMethod)); } private static class CustomFailureHandler implements FailureHandler { private final FailureHandler mDelegate; private String mClassname; private String mMethodname; public CustomFailureHandler(Context targetContext, String classname, String methodname) { mDelegate = new DefaultFailureHandler(targetContext); mClassname = classname; mMethodname = methodname; } @OverrIDe public voID handle(Throwable error, Matcher<VIEw> vIEwMatcher) { try { mDelegate.handle(error, vIEwMatcher); } catch (Exception e) { SpoonScreenshotAction.perform("espresso_assertion_Failed", mClassname, mMethodname); throw e; } } }}
@H_403_0@…这是从the Gist posted by Square略微修改的截图代码:@H_403_0@/** * Source: https://github.com/square/spoon/issues/214#issuecomment-81979248 */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; } public static voID perform(String tag, String classname, String methodname) { onVIEw(isRoot()).perform(new SpoonScreenshotAction(tag, classname, methodname)); }}
@H_403_0@我很乐意找到一种避免在每次测试中调用setUpFailureHandler()的方法 – 如果您对如何避免这种情况有一个好主意,请告诉我! 总结 以上是内存溢出为你收集整理的android – 如何让Spoon为Espresso测试拍摄截图?全部内容,希望文章能够帮你解决android – 如何让Spoon为Espresso测试拍摄截图?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)