DannyDainton / newman-reporter-htmlextra

A HTML reporter for Postman's Command Line Runner, Newman. Includes Non Aggregated Runs broken down by Iterations, Skipped Tests, Console Logs and the handlebars helpers module for better custom templates.
Apache License 2.0
357 stars 139 forks source link

Email sending #12

Closed ghost closed 5 years ago

ghost commented 5 years ago

Hi @DannyDainton

This is not an issue .Need your inputs if you can help me with email triggering of report generated with newman integration .Is there any plugin or helpers which would serve this need.

DannyDainton commented 5 years ago

@Subhashini-1

I would have a look into creating a node script and incorporating this functionality in there. There will be plenty of modules that could give you some help with this. I think nodemailer could probably help and seems easy to get a script together.

A basic script might look like this - I've never done this before so this could be wrong.

const newman     = require('newman')
const nodemailer = require('nodemailer')

newman.run({
    collection: '<yourCollection>',
    reporters: ['htmlextra']
}).on('done', function (err) {

    let transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: '<yourname>@gmail.com',
            pass: '<yourpassword>'
        }
    });

    let mailOptions = {
        from: '<yourname>@gmail.com',
        to: '<someoneElse>@gmail.com',
        subject: 'Newman Report Created',
        text: 'Here is the report....'
    };

    transporter.sendMail(mailOptions, function(error, info){
    if (error) {
        console.log(error)
    } else {
        console.log('Email sent: ' + info.response)
    }
    });
})

More details on how that module works can be found in this post https://codeburst.io/sending-an-email-using-nodemailer-gmail-7cfa0712a799

I'm closing this as it's not an issue with the reporter.

kajol1701 commented 4 years ago

Hi @DannyDainton I used the code you provided above. And sent the report as an attachment. The mail is triggered and the user gets it too. But when I open it from the mail itself without downloading it, it appears in a non-formatted way. But when I download it and open the file it opens up properly. Just wanted to know why is this happening. Because of some Gmail default settings like the Jenkins have?

  newman
      .run({
        collection: require("../../../../c.json"),
        environment: require("../../../../e.json"),
        reporters: ["htmlextra"],
        reporter: {
          htmlextra: {
            export:
              "/templates/TestReport.html"
          }
        }
      })
      .on("start", function(err, args) {
        // on start of run, log to console
        console.log("running a collection...");
      })
      .on("done", function(err, summary) {
        if (err || summary.error) {
          console.error("collection run encountered an error.");
        } else {
          console.log("collection run completed.");

          sendEmail(["example@gmail.com"], "Test Report", "Hi", true, [
            {
              path: `/templates/TestReport.html`
            }
          ]);
        }
      });
DannyDainton commented 4 years ago

Hey @kajol1701

Thanks for posting your question here, it's easier to manage than via email 😁

So the report templete's styling is coming from CDNs to specific resources rather than the code being embedded in the template itself. The report is already huge under certain circumstances (very large collections and multiple x1000 iterations) and adding that in they would increase the final report size too.

I think that also Gmail blocks external CDNs, I don't know this for sure but I was having a look and searching for some information.

Found this reply on SO - seems to be security related and makes sense why:

https://stackoverflow.com/a/56910981/6028443

kajol1701 commented 4 years ago

Thanks for replying:smile: I get it now.

DannyDainton commented 4 years ago

No worries, it's a shame that it doesn't work in the way that you hoped but that seems to be on the email client rather than the reporter. 😔

Sounds like sending as an attachment seems to work, after downloading it, so at least there's a workaround 😁

kajol1701 commented 4 years ago

Yep, sending as an attachment did work and after downloading the file opened perfectly. Again, Thanks a lot! You are too kind to help me out. Also, will be worrying you again as using Newman a lot for my work!

kajol1701 commented 4 years ago

Hi @DannyDainton Is there a way to automatically download the collection from the postman? Actually downloading the collection manually while pre-commit or pre-push, every time to generate the report is a bit tedious.

DannyDainton commented 4 years ago

Hey @kajol1701

Are you exporting it as a JSON file each time?

You can use either the Collections sharable Public link or the most up to date version of the collection using the Postman API - Both of these can be used in the newman run command.

https://learning.postman.com/docs/collaborating-in-postman/sharing/#sharing-by-link

https://documenter.getpostman.com/view/631643/JsLs/?version=latest

  newman
      .run({
        collection: "<Collection URL>",
        environment: require("../../../../e.json"),
        reporters: ["htmlextra"],
        reporter: {
          htmlextra: {
            export:
              "/templates/TestReport.html"
          }
        }
      })
      .on("start", function(err, args) {
        // on start of run, log to console
        console.log("running a collection...");
      })
      .on("done", function(err, summary) {
        if (err || summary.error) {
          console.error("collection run encountered an error.");
        } else {
          console.log("collection run completed.");

          sendEmail(["example@gmail.com"], "Test Report", "Hi", true, [
            {
              path: `/templates/TestReport.html`
            }
          ]);
        }
      });
github-actions[bot] commented 2 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.