3

Generally, I am using below code to take a screenshot and attach in allure report :

@Attachment(value = "Page Screenshot", type = "image/png")
public static byte[] saveScreenshotPNG(WebDriver driver) {
    return ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
}

But now my need is I have already some screenshot on my desktop and want to attach it with an allure report. is that possible?

Guy
  • 46,488
  • 10
  • 44
  • 88
Helping Hands
  • 5,292
  • 9
  • 60
  • 127

1 Answers1

2

You can take the existing image and convert it to byte[]. getScreenshotAs() decodes the screenshot string so you might need to do it as well

Java

@Attachment(value = "Page Screenshot", type = "image/png")
public static byte[] saveScreenshotPNG(String path) {
    File file = new File(path);
    BufferedImage bufferedImage = ImageIO.read(file);

    byte[] image = null;
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
        ImageIO.write(bufferedImage, "png", bos);
        image = bos.toByteArray();
    } catch (Exception e) { }

    // if decoding is not necessary just return image
    return image != null ? Base64.getMimeDecoder().decode(image) : null;
}

Python

with open(path, 'rb') as image:
    file = image.read()
    byte_array = bytearray(file)
    allure.attach(byte_array, name="Screenshot", attachment_type=AttachmentType.PNG)
supputuri
  • 13,644
  • 2
  • 21
  • 39
Guy
  • 46,488
  • 10
  • 44
  • 88
  • Thanks, It works. @Guy, Do you mind giving the same code for python also, please? I need same in python and allure as well. Currently, I am using `allure.attach(self.driver.get_screenshot_as_png(), name="Screenshot", attachment_type=AttachmentType.PNG)` – Helping Hands Feb 27 '20 at 09:44
  • 1
    @HelpingHands Updated. The Python solution is even tested. – Guy Feb 27 '20 at 09:57