Search code examples
javamavenjunitwebpage-screenshotallure

Using Allure to capture a screenshot on fail


I've been playing around with the Allure framework and I think that if any error/failure happened (basically anything that isn't a pass) I'd like to capture a screenshot.

I'm using Java/Junit/Maven.

I've seen there are various ways of using @Rule to do this, but wasn't sure if this would save the screenshot within the report.

I had thought there would be a testcase status or something I could check as part of the tear down but counldn't find anything.

Anyone have any ideas?


Solution

  • JUnit rules it is the default way to save screenshots on fail. An example:

    @Rule
    public TestWatcher screenshotOnFailure = new TestWatcher() {
        @Override
        protected void failed(Throwable e, Description description) {
            makeScreenshotOnFailure();
        }
    
        @Attachment("Screenshot on failure")
        public byte[] makeScreenshotOnFailure() {
            return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
        }
    };