-->

nodemailer not sending mail when placed in protrac

2020-07-29 00:55发布

问题:

nodemailer not sending mail when placed in protractor config afterLaunch() block. It sends mail when placed in beforeLaunch() or onPrepare() blocks.

Problem: Send HTML mail after protractor test run is complete. The framework generates a HTML file once the execution is complete. nodemailer to read the html content and send email

config.js

exports.config = {
    specs: ['../test_lab/execute.spec.js'],

    capabilities: {
        browserName: 'chrome',
        seleniumAddress: 'http://localhost:4444/wd/hub',
    },

    beforeLaunch: function () {
        // beforeLaunch actions
    },

    onPrepare: function () {
        //  onPrepare actions
    },

    afterLaunch: function () {
        //  generate HTML report

        //  Send HTML Email
        var htmlFilePath = '../index.html';
        var htmlFileContent = String(fs.readFileSync(htmlFilePath));
        sendMail.sendHTMLMail(htmlFileContent);
    },

    framework: 'jasmine2',

    jasmineNodeOpts: {
        onComplete: null,
        showColors: true,
        defaultTimeoutInterval: 60000
    }
};

nodemailer - Send Mail helper function

var SendMail = function () {
    this.sendHTMLMail = function (htmlContent) {        
        var transporter = nodemailer.createTransport('smtps://testmail%40gmail.com:pwd@smtp.gmail.com');
        var mailOptions = {
            from: '"test mail" <testmail@gmail.com>',
            to: 'testmail2@gmail.com', 
            subject: 'Test Report',
            text: 'Test Report',
            html: htmlContent
        };
        transporter.sendMail(mailOptions, function (error, info) {
            if (error) {
                return console.log(error);
            }
            console.log('Mail sent: ' + info.response);
        });
    };
};

I verified that the code enters the sendHTML block, but transporter.sendMail() is not executed

回答1:

I have a similar problem before.

Try this:

config.js

var q = require('q');
exports.config = {
    specs: ['../test_lab/execute.spec.js'],

    capabilities: {
        browserName: 'chrome',
        seleniumAddress: 'http://localhost:4444/wd/hub',
    },

    beforeLaunch: function () {
        // beforeLaunch actions
    },

    onPrepare: function () {
        //  onPrepare actions
    },

    afterLaunch: function () {
       return q.fcall(function () {
        //  generate HTML report

        //  Send HTML Email
        var htmlFilePath = '../index.html';
        var htmlFileContent = String(fs.readFileSync(htmlFilePath));
        sendMail.sendHTMLMail(htmlFileContent);
       }).delay(1000);
    },

    framework: 'jasmine2',

    jasmineNodeOpts: {
        onComplete: null,
        showColors: true,
        defaultTimeoutInterval: 60000
    }
};