nodeLiler在放置在lauunch()块后的量角器配置中时不发送邮件

时间:2016-10-14 00:36:41

标签: npm jasmine protractor nodemailer

nodeLiler在放置在lauunch()块后的量角器配置中时不发送邮件。 它放在beforeLaunch()或onPrepare()块时发送邮件。

问题:量角器测试运行完成后发送HTML邮件。一旦执行完成,框架就会生成一个HTML文件。 nodemailer读取html内容并发送电子邮件

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 - 发送邮件助手功能

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

我确认代码进入了sendHTML块,但是没有执行transporter.sendMail()

1 个答案:

答案 0 :(得分:2)

我之前遇到过类似的问题。

试试这个:

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