Making screenshots from Selenium with JUnit @Rule's

30 Mar, 2010

When running Selenium tests from JUnit it’s very useful to be able to capture screenshots when something fails. Especially when you run it in a Continuous Integration environment which you aren’t monitoring. A screenshot combined with the stacktrace makes identifying and fixing the error easier. When you combine this with a JUnit @Rule you can make it transparant and use it for every testcase
Implementing the @Rule
For implementing rules you need to implement the MethodRule interface and implement the apply method. The base.evaluate method invokes the unittest. The screenshot is made in case of any exception which is re-thrown as a RuntimeException so that your testcase will fail. This class extends the SeleneseTestBase which helps you writing selenium unit tests. Capturing screenshots is a feature of selenium and is very easy. You might want to store the file differently, but that should be easy.
[code lang=”java”]
public class CapturingSelenium extends SeleneseTestBase implements MethodRule {
public Statement apply(final Statement base, FrameworkMethod method, Object target) {
return new Statement() {
public void evaluate() throws Throwable {
try {
setUp("", "*firefox");
catch (Exception e) {
selenium.captureEntirePageScreenshot("/tmp/selenium-error-" + new UUID().toString() + ".PNG", "");
throw new RuntimeException(e);
That’s it. Now you can just use the CapturingSelenium class and annotate it with @Rule. When any test fails with an exception the rule catches it and captures a screenshot of the browser at the time of the exception. Here’s a basic example:
[code lang=”java”]
public class SeleniumTest {
private CapturingSelenium selenium = new CapturingSelenium();
public void shouldCaptureScreenshotWhenFailing() throws Exception {"");"some-button-which-is-not-there");

Newest Most Voted
Inline Feedbacks
View all comments
12 years ago

great idea 🙂

Jae Kim
Jae Kim
11 years ago

Is it working? I tried to execute this code but selenium doesn’t have the open and click functions.

10 years ago

Yes I have the same problem.
eclipse says for example by The method open(String) is undefined for the type CapturingSelenium.
So it dosent works ; (
Now i try to reslove this or i have to use a diffrent way to take screenshots at an ERROR .
Somebody has an idea?

6 years ago

If you are using WebDriver instance, instead of DefaulSelenium used in the example above, then :
Replace with
and replace with
where driver is an instance of WebDriver and instantiated according to your need.

Explore related posts