Skip to main content

Cucumber Allure Attach Screenshot on Failure

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:

public void af(Scenario scenario) throws InterruptedException, IOException, IllegalMonitorStateException
        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.

Submitted by tgoswami on September 13, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.


At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.