Snapshots of Failing Builds

The problem with acceptance and regression testing is that the errors coming back from an automated build don't really help identify the problem. A stacktrace letting you know that some element wasn't present on the page doesn't necessarily tell you why that is. However, the stacktrace dumped out to the screen by the app server (or the wedged UI of your desktop app, or... you get the idea) tells you an awful lot.

Wouldn't it be nice to capture a screen-shot when a test fails? And did you know that it's ridiculously simple to do this using bog-standard Java? The code snippet (without error checking) you need goes something like:

public void grabScreenShot(String testName) {
    File dir = System.getProperty("place.to.dump.images", "/tmp/build");
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    BufferedImage image = new Robot().createScreenCapture(new Rectangle(screenSize));
    ImageIO.write(image, "jpeg", new File(dir + "/" + testName + ".jpeg"));
}

If you're using JUnit 3, just set up a new TestListener that does this when a test fails or has an error. If you're using some other test framework then you'll need to figure out what to do. How hard can it be?


Simon Stewart on Tuesday, 31 July, 2007

Posted in: /tech/java

You may comment...


Categories