Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.7k points)

When my test case fails, especially on our build server, I want to take a picture / screenshot of the screen to help me debug what happened later on. I know how to take a screenshot, but I was hoping for a way in JUnit to call my takeScreenshot() method if a test fails, before the browser is closed.

No, I don't want to go edit our bazillions of tests to add a try/catch. I could maybe, just possibly be talked into an annotation, I suppose. All of my tests have a common parent class, but I can't think of anything I can do there to solve this.


1 Answer

0 votes
by (62.9k points)

With the MethodRule of JUnit, we can prevent or stop failed unit tests before JUnit passes/dispatches the results. This is the perfect lifecycle event to take the opportunity to take a screenshot of why the test failed.


package com.memorynotfound.test;




import java.util.UUID;



import org.junit.rules.MethodRule;

import org.junit.runners.model.FrameworkMethod;

import org.junit.runners.model.Statement;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;


public class ScreenShotOnFailure implements MethodRule {


    private WebDriver driver;


    public ScreenShotOnFailure(WebDriver driver){

        this.driver = driver;



    public Statement apply(final Statement statement, final FrameworkMethod frameworkMethod, final Object o) {

        return new Statement() {


            public void evaluate() throws Throwable {

                try {


                } catch (Throwable t) {

                    // only when a test fails exception will be thrown.


                    // rethrow to allow the failure to be reported by JUnit

                    throw t;




            public void captureScreenShot(String fileName) throws IOException {

                File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

                fileName += UUID.randomUUID().toString();

                File targetFile = new File("/tmp/" + fileName + ".png");

                FileUtils.copyFile(scrFile, targetFile);





Apply The rule in Selenium Test

We can include our custom MethodRule by annotating it with @Rule. Then sit back and see the magic happen.

 package com.memorynotfound.test;


import org.junit.AfterClass;

import org.junit.BeforeClass;

import org.junit.Rule;

import org.junit.Test;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;



import static org.junit.Assert.assertTrue;


public class ScreenShotTest {


    private static WebDriver driver;



    public ScreenShotOnFailure failure = new ScreenShotOnFailure(driver);



    public static void setUp(){

        driver = new FirefoxDriver();




    public void testTakeScreenShot() throws IOException {






    public static void cleanUp(){

        if (driver != null){






Browse Categories