【问题标题】:TestNG where can I run code after Reporter? Want to send test-output/emailable-report.htmlTestNG 在 Reporter 之后我可以在哪里运行代码?想发送 test-output/emailable-report.html
【发布时间】:2013-09-25 10:35:16
【问题描述】:

我想运行代码来发送带有文件 emailable-report.html 的电子邮件。理想情况下,当我从 IDE 或 Maven 运行 TestNG 套件时,这将起作用。我认为这里提出了同样的问题: https://groups.google.com/forum/?fromgroups=#!topic/webdriver/fvJ-edHPJ3g

从 cmets 中,我试图了解一种按照建议执行的最小方法,即覆盖现有 EmailableReporter 中的方法,并添加代码以发送电子邮件。我试图避免完全实现我自己的 IReporter 侦听器,尽管主要是因为我很难遵循关于这样做的说明(有点超出我的 java-foo,欢迎提供一个完整的示例)。

我的班级看起来像:

public class TestMailSender extends EmailableReporter{
    TestMailer testMailer = new TestMailer();
    Message resultEmail;

@Override
public void generateReport(List<XmlSuite> arg0, List<ISuite> arg1, String arg2) {

    /* Use the parent class to do the work */
    super.generateReport(arg0, arg1, arg2);

    /* create email from utility class with address, subject line, message content */
    resultEmail = testMailer.makeEmail("someone@somewhere.com", "Build: " + "test"
            + "suite results", "results attached");

    /* fetch the hopefully completed default report */
    /* TODO: get report to common path for both IDE and Maven runs */
    File resultsFile = new File("./test-output/emailable-report.html");

    /* add file to the email with build referencing name, then send the email */
    resultEmail = testMailer.attachFile(resultEmail, resultsFile.getAbsoluteFile(),
            "build_" + "test" + "_emailable-report.html");
    testMailer.sendEmail(resultEmail);
}
}

当我将上述类作为侦听器添加到 TestNG 套件时,我得到一个空结果。我正在尝试在 testNG.xml 套件中执行此操作,如下所述: http://testng.org/doc/documentation-main.html#listeners-testng-xml

我想空结果可能是进度,因为它没有像我将代码放入 @AfterSuite 时那样发送先前的运行结果文件。在完成 emailable-report.html 文件后,我不能像尝试那样运行我的代码,因为通过电子邮件发送的文件是空的。如果我更改 resultsFile 的路径,我会得到一些东西,我会收到 FileNotFoundException,但我猜该文件在我尝试发送它的时候还没有刷新和关闭。

我可以将我的代码放在哪里,以便它可以找到并发送完成的 emailable-report.html 文件以用于当前运行?我必须实现自己的 IReporter 侦听器,还是有一些简单的方法可以获取生成 emailable-report.html 文件的默认侦听器的 (completed) 输出?

【问题讨论】:

  • 不知何故,我想覆盖已经运行的 EmailableReporter 的方法,而不是完全替换它。也许我需要更换它?要么这样,要么找一个更好的地方放置邮件发送代码。

标签: java testng


【解决方案1】:

我最终编辑了上面的代码示例以替代 generateReport 而不是 endHtml,但想法或多或少是相同的。我认为让我感到困惑的是,我尝试使用的路径不正确,一旦我切换到 arg2 而不是不正确的硬编码字符串,事情就开始起作用了。下面的代码块加上扩展类的定义作为我的 testNG.xml 中的监听器,并且 Bob 是我的叔叔。

public class TestMailSender extends EmailableReporter{
TestMailer testMailer = new TestMailer();
Message resultEmail;

@Override
public void generateReport(List<XmlSuite> arg0, List<ISuite> arg1, String arg2) {

    super.generateReport(arg0, arg1, arg2);

    String someBuildIdAsParameter = arg1.get(0).getParameter("build");
    String someEmailAsParameter = arg1.get(0).getParameter("notifyEmail");
    /* create email from utility class with address, subject line, message content */
    resultEmail = testMailer.makeEmail(someEmailAsParameter, "Build: " + someBuildIdAsParameter
            + " suite results", "results attached");

    /* fetch the hopefully completed default report */
    File resultsFile = new File(arg2 + "\\emailable-report.html");

    /* add file to the email with build referencing name, then send the email */
    resultEmail = testMailer.attachFile(resultEmail, resultsFile.getAbsoluteFile(),
            "build_" + someBuildIdAsParameter + "_emailable-report.html");
    testMailer.sendEmail(resultEmail);
    }
}

【讨论】:

    猜你喜欢
    • 2020-04-15
    • 1970-01-01
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多