on Sep 9th, 2010Capture screenshot on failing SWTBot tests
Sometimes functional tests fail. If they do, I not only want to see the test and the error message, I also want a screenshot of the application in the state during the failing test.
I was astonished to find nothing in the SWTBot code or documentation that suggests how to automatically capture a screenshot when a SWTBot test fails. This is why I came up with a JUnit4 Rule that does this.
In your SWTBot test class include the field
public class MyTest {
@Rule
public CaptureScreenshotOnFailure screenshot = new CaptureScreenshotOnFailure();
@Test public void mytestmethod() {
SWTBot bot = new SWTBot(...);
// swtbot clicks and types
}
So, if now mytestmethod fails, a screenshot will be put in the current directory with the name mypackage.MyTest.mytestmethod.png
This is the rule, put it somewhere where your test classes can access it:
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
public class CaptureScreenshotOnFailure implements MethodRule {
public final Statement apply(final Statement base,
final FrameworkMethod method, Object target) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
base.evaluate();
} catch (Throwable onHold) {
String fileName = constructFilename(method);
new SWTWorkbenchBot().captureScreenshot(fileName);
throw onHold;
}
}
private String constructFilename(final FrameworkMethod method) {
return "./"
+ method.getMethod().getDeclaringClass()
.getCanonicalName() + "."
+ method.getName() + ".png";
}
};
}
}
I think this makes writing tests with SWTBot more comfortable. Do you see ways to improve this rule?
Related posts:
- Using JUnit’s “Assume” for faster tests
- Continuous Integration Tests for REST APIs with Maven, Jetty and restfuse
- Introducing restfuse – a JUnit Extension to test REST APIs
- An almost perfect Test Suite
- Effective Mockito Part 5



Hi,
The @RunWith(SWTBotJunit4ClassRunner.class) annotation makes failing tests capture a screenshot in a ‘screenshots’ directory in your project.
regards,
Vlad
I like it!
It would be very cool if you can show the taken screenshot in the JUnit-Plugin of Eclipse
SWTBot has always had a screencapture feature. I’ve not done a good job documenting it.
Just annotate your test with @RunWith(SWTBotJunitClassRunner.class) and you’ll get screenshots on failures, for free
Hi,
First of all, nice article.
I have a problem with the @RunWith(SWTBotJunitClassRunner.class) screenshot. If I have an @After method, that reverts to an initial state (closing opened windows and deleting projects from workspace), the screenshot is taken “after” this method is executed which makes the screenshot useless because I will always get a screenshot of the clean workspace instead of the place where it fails.
1. Is the @Rule applied before or after the @After method is executed?
2. In the html report generated by JUnit I can see all the screenshots taken via @RunWith(SWTBotJunitClassRunner.class). If I use this @Rule approach, will I have the screenshot available in the html report?
Any help on this two issues would be greatly appreciated.
Thank you.