简体   繁体   中英

TestNG where can I run code after Reporter? Want to send test-output/emailable-report.html

I'd like to run code to send email with the file emailable-report.html. Ideally, this would work when I run a TestNG suite either from the IDE or from Maven. I think the same question is asked here: https://groups.google.com/forum/?fromgroups=#!topic/webdriver/fvJ-edHPJ3g

From the comments, I'm trying to understand a minimal way to do as suggested, override a method from the existing EmailableReporter with added code to send email. I'm trying to avoid fully implementing my own IReporter listener, although mostly because I'm having a hard time following notes about doing that (a little beyond my java-foo, would welcome a complete example).

My class looks like:

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);
}
}

I get an empty result when I add the above class to the TestNG suite as a listener. I'm attempting to do that in the testNG.xml suite as described here: http://testng.org/doc/documentation-main.html#listeners-testng-xml

I suppose the empty result may be progress in that it doesn't send the prior run results file as it was when I put my code in @AfterSuite. I must not be running my code as I'm attempting to, after completion of the emailable-report.html file, because the file that is emailed is empty. I getting something in that if I change the path for the resultsFile I get a FileNotFoundException, but I guess the file hasn't flushed and closed at the point where I'm trying to send it. 得到的的东西,如果我更改resultsFile我得到一个FileNotFoundException异常的路径,但我想该文件还没有刷新,并在这里我想给它发送点关闭。

Where can I put my code so that it can find and send the completed emailable-report.html file for the current run? Must I implement my own IReporter listener, or is there some simple way I can just grab the ( completed ) output of the default listener that produces the emailable-report.html file?

I ended up editing the above code example to be an override of generateReport rather than endHtml, but the idea is more or less the same. I think what was confusing me is that the path I tried to use wasn't correct, once I switched to arg2 rather than an incorrect hard coded string, things started working. The below code block plus the definition of the extended class as a listener in my testNG.xml, and Bob's my uncle.

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM