Java 如何在诱惑报告中附加自定义/现有屏幕截图?
通常,我使用以下代码截图并将其附在诱惑报告中:Java 如何在诱惑报告中附加自定义/现有屏幕截图?,java,selenium-webdriver,allure,Java,Selenium Webdriver,Allure,通常,我使用以下代码截图并将其附在诱惑报告中: @Attachment(value = "Page Screenshot", type = "image/png") public static byte[] saveScreenshotPNG(WebDriver driver) { return ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES); } 但现在我需要的是,我已经在我的桌面上有了一些截图,我想在上面附上一
@Attachment(value = "Page Screenshot", type = "image/png")
public static byte[] saveScreenshotPNG(WebDriver driver) {
return ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
}
但现在我需要的是,我已经在我的桌面上有了一些截图,我想在上面附上一份诱惑报告。这可能吗?您可以拍摄现有图像并将其转换为
字节[]
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)
谢谢,它起作用了@盖伊,你介意给python同样的代码吗?我需要同样的python和诱惑。目前,我正在使用
allure.attach(self.driver.get_screenshot\u as_png(),name=“screenshot”,attachment_type=AttachmentType.png)
@HelpingHands更新。Python解决方案甚至已经过测试。