Skip to content

Commit

Permalink
provide junit ScreenshotOnFailure TestWatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
EcljpseB0T authored and akurtakov committed Sep 29, 2024
1 parent 900c895 commit 93ba9bd
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.File;

import org.eclipse.core.runtime.Platform;
import org.junit.rules.TestWatcher;

/**
* Helper class to take screenshots from running tests.
Expand All @@ -24,6 +25,19 @@
*/
public final class Screenshots {

private static final class ScreenshotOnFailure extends TestWatcher {
@Override
protected void failed(Throwable e, org.junit.runner.Description description) {
String screenshot = Screenshots.takeScreenshot(description.getTestClass(), description.getMethodName());
e.addSuppressed((new Throwable("Screenshot written to " + screenshot)));
super.failed(e, description);
}
}

/** @since 3.21 **/
public static TestWatcher onFailure() {
return new ScreenshotOnFailure();
}
/**
* Takes a screenshot and writes the path to the generated image file to System.out.
* <p>
Expand Down

0 comments on commit 93ba9bd

Please sign in to comment.