android – 如何在Espresso测试失败时截取屏幕截图?

前端之家收集整理的这篇文章主要介绍了android – 如何在Espresso测试失败时截取屏幕截图?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找一种方法来在测试失败之后和关闭之前截取设备的屏幕截图.

解决方法

我发现最简单的方法
@Rule
public TestRule watcher = new TestWatcher() {
  @Override
  protected void Failed(Throwable e,Description description) {
    // Save to external storage (usually /sdcard/screenshots)
    File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
        + "/screenshots/" + getTargetContext().getPackageName());
    if (!path.exists()) {
      path.mkdirs();
    }

    // Take advantage of UiAutomator screenshot method
    UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    String filename = description.getClassName() + "-" + description.getMethodName() + ".png";
    device.takeScreenshot(new File(path,filename));
  }
};
原文链接:https://www.f2er.com/android/317612.html

猜你在找的Android相关文章