I am trying to convert byte[] image to base64 using cucumber APIs to attach it to an Extent report, however the base64 version does not attach, but the byte[] one attaches.
When I convert to base64 and attach the output image is broken.
Cucumber version
<cucumber.version>6.9.0</cucumber.version>
Extent version:
<artifactId>extentreports-cucumber6-adapter</artifactId>
<version>2.8.0</version>
Code that does not work:
@After(order = 1)
public void tearDown(Scenario scenario) {
if (scenario.isFailed()) {
// take screenshot:
String screenshotName = scenario.getName().replaceAll(" ", "_");
byte[] sourcePath = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
String base64Img = Base64.getEncoder().encodeToString(sourcePath);
scenario.attach(base64Img, "image/png", screenshotName);
}
}
Code that does not work
@After(order = 1)
public void tearDown(Scenario scenario) {
if (scenario.isFailed()) {
// take screenshot:
String screenshotName = scenario.getName().replaceAll(" ", "_");
String sourcePath = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
scenario.attach(sourcePath, "image/png", screenshotName);
}
}
Code that works
@After(order = 1)
public void tearDown(Scenario scenario) {
if (scenario.isFailed()) {
// take screenshot:
String screenshotName = scenario.getName().replaceAll(" ", "_");
byte[] sourcePath = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
scenario.attach(sourcePath, "image/png", screenshotName);
}
}
Any input/guidance could really help!!
Quoting
@Grasshoppercomment which worked like a charm, so the question isn't left unanswered.