android - 如何在 Espresso 测试失败的地方截屏?

标签 android testing automated-tests 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));
  }
};

关于android - 如何在 Espresso 测试失败的地方截屏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38519568/

相关文章:

testing - JUnit 是黑盒测试还是白盒测试?

testing - 由于密码测试,rails 测试电子邮件验证错误?

java - Jmeter - 双数据类型的计数器

unit-testing - 卡斯帕 : Can we organize test suites like phpunit for example "allTests"

android - RxJava 如何正确地重新订阅 onError

java - Android Studio : Display ArrayList of SQLite database rows using ListView

将base64字符串转换为位图的Android代码

Android 的notifyDataSetChanged()示例

java - EasyMock 'Expected 1, Actual 1'

python - 等待元素中的文本发生更改