如何在魅力报告中附上 custom/existing 截图?

How to attach custom/existing screenshot in allure report?

一般来说,我使用下面的代码截屏并附在魅力报告中:

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

但现在我需要的是我已经在 my desktop 上有一些截图,并想附上一份魅力报告。这可能吗?

您可以使用现有图像并将其转换为 byte[]getScreenshotAs() 解码屏幕截图字符串,因此您可能也需要这样做

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)