Cucumber Allure Attach Screenshot on Failure

Profile picture for user devraj

There could be several ways to implement screenshot utility with Cucumber allure adapter. You can use allure add attachment method which is an easy of attaching screenshot to your allure cucumber report.

Method 1: Cucumber Allure Attach Screenshot using Helper Method

This is the one I think is a simple one:

@After
public void af(Scenario scenario) throws InterruptedException, IOException, IllegalMonitorStateException
{	
    if(scenario.isFailed())
    {	
        Allure.addAttachment("Any text", new ByteArrayInputStream(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES)));
    }
}	

Above code will capture screenshot on cucumber test failure. Here scenario is an object of Scenario Class. isFailed() method will return true if Scenario failed. Allure is the class, Add attachment is the method. Then inside method we need to specify the name of the screenshot, you can give any random text. Screenshots will still remain unique for each scenario.

ByteArrayInputStream, Creates a ByteArrayInputStream for specified value. TakeScreenshot is an interface, Indicates a driver that can capture a screenshot and store it in different ways. getScreenshotAs, capture the screenshot and store it in the specified location. OutputType defines the output type for a screenshot.