【问题标题】:How do I email an HTML report (as an attachment) with images embedded?如何通过电子邮件发送嵌入图像的 HTML 报告(作为附件)?
【发布时间】:2017-02-08 14:09:42
【问题描述】:

我正在使用 ExtentReports 创建一个报告,以通过电子邮件发送给域外的团队成员。我使用截图方法(如下)来保存测试失败的截图。它们存储在 ExtentReports HTML 报告的子文件夹中。

我将报告附加到电子邮件中,在其中,具有文件夹权限的域中的团队成员可以正常显示图像。但是我不知道如何允许该文件夹权限之外的人查看报告中嵌入的图像。这是图像的 HTML,直接引用该文件。

<img class="report-img" data-featherlight="file:///\\domain.local\files\QA\Projects\AutomationReports\ExtentScreens\1486487870116.jpg" src="file:///\\domain.local\files\QA\Projects\AutomationReports\ExtentScreens\1486487870116.jpg">

这是我的截图方法。

public static String CaptureScreen(WebDriver driver) {
    String ImagesPath = "\\\\domain.local\\files\\QA\\Projects\\AutomationReports\\ExtentScreens\\"
            + new Date().getTime();

    TakesScreenshot oScn = (TakesScreenshot) driver;
    File oScnShot = oScn.getScreenshotAs(OutputType.FILE);
    File oDest = new File(ImagesPath + ".jpg");

    // System.out.println(ImagesPath);

    try {
        FileUtils.copyFile(oScnShot, oDest);
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
    return ImagesPath + ".jpg";
}

关于如何解决这个问题,我有 2 个不相关的想法。但是我需要一些帮助才能开始使用它们中的任何一个。我愿意接受其他建议。

  1. 将图像直接嵌入 HTML 报告或以某种方式发送包含 HTML 报告屏幕截图的文件夹。但是,HTML 仍会引用我的原始位置,并且图像将被破坏。

  2. 与所有人、访客和匿名用户共享包含图像的文件夹,以便域外的人可以打开引用此位置的 HTML。我不知道如何设置这些权限,我什至不确定这样做是否会允许外部用户查看引用该位置的 HTML。

【问题讨论】:

    标签: java html selenium report jakarta-mail


    【解决方案1】:

    请尝试使用 base64 编码,它肯定会工作。 另外,请检查您的浏览器支持。

    试试这个:

    <img src="data:image/jpeg;base64,/9j/4AAQxxxxxxxx...." />
    

    【讨论】:

    • 谢谢,现在可以使用了。有没有关系,或者我可以在编码为 base64 时更改文件类型?我没有看到任何定义编码文件类型的方法。
    • @dsidler 不客气。您能否更具体地说明您还想做什么?
    【解决方案2】:

    根据您需要support 的浏览器,您可以将图像嵌入base64。像这样:

    <img src="data:image/jpeg;base64, LzlqLzRBQ...<!-- base64 data -->" />
    

    Here is a tool to encode your images

    【讨论】:

      【解决方案3】:

      您可以将 base64 编码的图像直接嵌入到 HTML 文档中。

      <img src="data:image/jpeg;base64,/9j/4AAQxxxxxxxx...." />
      

      【讨论】:

        【解决方案4】:

        您可以创建多部分/报告消息,并将图像作为附加正文部分包含在消息中。 JavaMail FAQ 包含此示例代码:

            Multipart multipart = new MimeMultipart("related");
        
            MimeBodyPart htmlPart = new MimeBodyPart();
            // messageBody contains html that references image
            // using something like <img src="cid:XXX"> where
            // "XXX" is an identifier that you make up to refer
            // to the image
            htmlPart.setText(messageBody, "utf-8", "html");
            multipart.addBodyPart(htmlPart);
        
            MimeBodyPart imgPart = new MimeBodyPart();
            // imageFile is the file containing the image
            imgPart.attachFile(imageFile);
            // or, if the image is in a byte array in memory, use
            // imgPart.setDataHandler(new DataHandler(
            //      new ByteArrayDataSource(bytes, "image/whatever")));
        
            // "XXX" below matches "XXX" above in html code
            imgPart.setContentID("<XXX>");
            multipart.addBodyPart(imgPart);
        
            message.setContent(multipart);
        

        【讨论】:

        • 我正在使用多部分 javamail 发送报告,但它是一个预制的 html 报告,不能很好地放入电子邮件中。不过还是谢谢。
        • 不管怎样,您将不得不更改所有这些 img 标签,以便它们不引用本地文件。
        【解决方案5】:

        我们应该有一个如下的类文件

        公共类 GetScreenShort {

        public static String capture(WebDriver driver,String screenShotName) 抛出 IOException { TakesScreenshot ts = (TakesScreenshot)驱动程序;

            String dest = ts.getScreenshotAs(OutputType.BASE64);
        
            return "data:image/jpg;base64, " + dest ;
         }
        

        }

        必须调用相同的类,如下所示

        String screenShotPath = GetScreenShort.capture(webdriver, "screenShotName");

        【讨论】:

        • 这个函数中screenShotName在哪里使用?
        猜你喜欢
        • 1970-01-01
        • 2017-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-29
        • 2017-07-28
        相关资源
        最近更新 更多