我正在寻找一种在测试失败后和关闭前截取设备屏幕截图的方法。
最佳答案
我找到的最简单的方法:
@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/