Skip to main content

Add Screenshot in Cucumber Extent Report

There could be several ways to implement screenshot utility with Cucumber adapter. This is the one I think is a better Solution:

Extent report Byte screenshot

Step 1: Add method to capture screenshot

public byte[] getByteScreenshot() throws IOException 
{
    File src = ((TakesScreenshot) base.getDriver()).getScreenshotAs(OutputType.FILE);
    byte[] fileContent = FileUtils.readFileToByteArray(src);
    return fileContent;
}

Here,

  • TakeScreenshot is an interface that Indicates a driver that can capture a screenshot and store it in different ways. 
  • FileUtil is general file manipulation util. readFileToByteArray will read the content of image file in ByteArray. 

or You can directly convert to byte and return

public byte[] getByteScreenshot()
{
    return ((TakesScreenshot) base.getDriver()).getScreenshotAs(OutputType.BYTES);
}

Step 2: Add Screenshot configuration in extent.properties

screenshot.dir=test-output/screenshots/
screenshot.rel.path=../screenshots/

Step 3: In after hook call following line

@After
public void af(Scenario scenario) throws IOException, InterruptedException
{
    if(scenario.isFailed())
    {			
        scenario.attach(getByteScreenshot(), "image/png", scenario.getName());
    }
    base.getDriver().quit();
}

Submitted by tgoswami on August 30, 2020

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

About

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.