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();
}