-
-
Save rponte/8055699 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyTest { | |
@Rule | |
public MethodRule screenshot = new ScreenshotOnFailureRule(); | |
@Test | |
public void myTest() { ... } | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ScreenshotOnFailureRule implements MethodRule { | |
@Override | |
public Statement apply(Statement base, FrameworkMethod method, Object target) { | |
if(allowsScreenshot(method)) { | |
String className = method.getMethod().getDeclaringClass().getSimpleName(); | |
String methodName = method.getName(); | |
return new ScreenshotOnFailureStatement(base, className, methodName); | |
} | |
else { | |
return base; | |
} | |
} | |
private boolean allowsScreenshot(FrameworkMethod method) { | |
return method.getAnnotation(NoScreenshot.class) == null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ScreenshotOnFailureRule implements MethodRule { | |
@Override | |
public Statement apply(Statement base, FrameworkMethod method, Object target) { | |
String className = method.getMethod().getDeclaringClass().getSimpleName(); | |
String methodName = method.getName(); | |
return new ScreenshotOnFailureStatement(base, className, methodName); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ScreenshotOnFailureStatement extends Statement { | |
private final Statement base; | |
private final String className; | |
private final String methodName; | |
public ScreenshotOnFailureStatement(Statement base, String className, String methodName) { | |
this.base = base; | |
this.className = className; | |
this.methodName = methodName; | |
} | |
@Override | |
public void evaluate() throws Throwable { | |
try { | |
base.evaluate(); | |
} | |
catch(Throwable e) { | |
MyScreenshooter.takeScreenshot(className, methodName); | |
throw e; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment