本文介绍了如何在 Espresso 测试失败时截取屏幕截图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种在测试失败后和关闭之前截取设备屏幕截图的方法.
I am looking for a way to take a screenshot of device after test failed and before get closed.
推荐答案
我找到的最简单的方法:
Easiest way that I found:
@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));
}
};
这篇关于如何在 Espresso 测试失败时截取屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!