I.Vol
I.Vol

Reputation: 31

How to take screenshot on test failure with junit 5

Can someone tell me please: how to take a screenshot when test method fails (jUnit 5). I have a base test class with BeforeEach and AfterEach methods. Any other classes with @Test methods extends base class.

Upvotes: 3

Views: 2705

Answers (3)

Alan
Alan

Reputation: 51

I use junit 5 and selenium. (take screenshot using junit 5 and selenium) I did that through extension (extension from junit 5, making screenshot using selenium), here is my test class:

public class MyTests2 {
    public static WebDriver driver;

    public static WebDriver getDriver(){
        return driver;
    }

    @RegisterExtension
    static RunnerExtentsion2 extentsion2 = new RunnerExtentsion2(MyTests2::getDriver);

    @BeforeEach
    private void setUp() {
        System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @AfterEach
    private void tearDown() {
        driver.quit();
    }

    @Test
    public void emptyBothNameAndPassAuthNegaTest() {
        driver.navigate().to("https://www.google.com/");
        throw new NoSuchElementException("my exception");
    }
}

Here is my screenshot taker class:

public class RunnerExtentsion2 implements AfterTestExecutionCallback {
    private Supplier<WebDriver> driverSupplier;

    public RunnerExtentsion2(Supplier<WebDriver> driverSupplier){
        this.driverSupplier = driverSupplier;
    }

    @Override
    public void afterTestExecution(ExtensionContext context) throws Exception {
        Boolean testResult = context.getExecutionException().isPresent();
        if (context.getExecutionException().isPresent()) {
            System.out.println("screenshot");
            driverSupplier.get().get(...);
            Thread.sleep(3000);
        }
    }
}

Upvotes: 1

Vasu
Vasu

Reputation: 22384

You don't need to take screen shots for JUnit test failes/passes, rather the recommended way is to generate various reports (Tests Passed/Failed Report, Code coverage Report, Code complexity Report etc..) automatically using the below tools/plugins.

You can use Cobertura maven plugin or Sonarqube code quality tool so that these will automatically generate the reports for you.

You can look here for Cobertura-maven-plugin and here for Sonarqube for more details.

You need to integrate these tools with your CI (Continuous Integration) environments and ensure that if the code is NOT passing certain quality (in terms of tests coverage, code complexity, etc..) then the project build (war/ear) should fail automatically.

Upvotes: 1

GhostCat
GhostCat

Reputation: 140407

Well, it is possible to write java code that takes screenshots, see here for example.

But I am very much wondering about the real problem you are trying to solve this way. I am not sure if you figured that yet, but the main intention of JUnit is to provide you a framework that runs your tests in various environments.

Of course it is nice that you can run JUnit within your IDE, and maybe you would find it helpful to get a screenshot. But: "normally" unit tests also run during nightly builds and such - in environments where "taking a screenshot" might not make any sense!

Beyond that: screenshorts are an extremely ineffective way of collecting information! When you have a fail, you should be locking for textual log files, html/xml reports, whatever. You want that failing tests generate information that can be easily digested.

So, the real answer here is: step back from what you are doing right now, and re-consider non-screenshot solutions to the problem you actually want to solve!

Upvotes: 2

Related Questions