如何让Spoon拍摄Espresso测试的屏幕截图? [英] How to get Spoon to take screenshots for Espresso tests?

查看:141
本文介绍了如何让Spoon拍摄Espresso测试的屏幕截图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试遵循将Spoon 1.1.14升级为截取Espresso测试失败的屏幕截图.

使用自定义Espresso FailureHandler进行配置的最佳方法是什么?

推荐答案

基于上述@Eric的方法,并带有ActivityTestRule ,我们可以在调用apply()函数时从description对象获取当前的测试方法名称和测试类名称.通过覆盖这样的apply函数

Based on @Eric's approach above, and with ActivityTestRule we can obtain the current test method name and test class name from description object when apply() function is called. By overriding the apply function like this

public class MyActivityTestRule<T extends Activity> extends ActivityTestRule<T> {

  @Override
  public Statement apply(Statement base, Description description) {
    String testClassName = description.getClassName();
    String testMethodName = description.getMethodName();
    Context context =  InstrumentationRegistry.getTargetContext();
    Espresso.setFailureHandler(new FailureHandler() {
      @Override public void handle(Throwable throwable, Matcher<View> matcher) {
        SpoonScreenshotAction.perform("failure", testClassName, testMethodName);
        new DefaultFailureHandler(context).handle(throwable, matcher);
        }
    });
    return super.apply(base, description);
  }

  /* ... other useful things ... */
}

我能够使用正确的测试方法和测试类来截取屏幕截图,以便可以将其正确地集成到最终的Spoon测试报告中.并记得通过添加

I was able to take screenshot with correct test method and test class so it can be correctly integrated into final Spoon test report. And remember to use the JUnit4 runner by adding

@RunWith(AndroidJUnit4.class)

您的测试班.

这篇关于如何让Spoon拍摄Espresso测试的屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆