解决方法
我发现最简单的方法:
@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)); } };