Unable to convert byte[] image to base64 using cucumber scenario api's - java selenium

211 views Asked by At

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!!

1

There are 1 answers

0
undetected Selenium On

Quoting @Grasshopper comment which worked like a charm, so the question isn't left unanswered.

You only need to add this property to your extent.properties file - 'extent.reporter.spark.base64imagesrc=true' and use the third hook code above without any base64 conversion. To add a thumbnail you will need to add 'thumbnailForBase64' xml property to true in the spark-config.xml else default image is displayed.