Helping Hands
Helping Hands

Reputation: 5396

How to attach custom/existing screenshot in allure report?

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?

Upvotes: 3

Views: 4213

Answers (1)

Guy
Guy

Reputation: 50809

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)

Upvotes: 2

Related Questions