无法使用protractor.conf.js中的nodemailer发送电子邮件onComplete:function()

时间:2017-03-23 08:31:48

标签: protractor

无法使用protractor.conf.js onComplete:function()中的nodemailer发送电子邮件。使用下面的代码,它不执行onComplete块

onComplete: function() {    
    var nodemailer = require('nodemailer');
    var transporter = nodemailer.createTransport({
        host: 'smtp.gmail.com',
        port: 465,
        secure: true, // use SSL
        auth: {
            user: 'email',
            pass: 'password'
        }
    });
    var mailOptions = {
            from: '"TestMail" <>', // sender address (who sends)
            to: 'receiver's email', // list of receivers (who receives)
            subject: 'Hello through conf', // Subject line
            text: 'Hello world ', // plaintext body
            html: '<b>Hello world </b><br> This is the first email sent with Nodemailer in Node.js', // html body


    };

        transporter.sendMail(mailOptions, function(error, info){
            if(error){
                return console.log(error);
            }

            console.log('Message sent: ' + info.response);
        });

1 个答案:

答案 0 :(得分:2)

你需要返回一个Promise。只有这样onComplete()才会等到Promise得到解决 - 邮件被触发。

  

一次调用的回调函数就完成了。 onComplete可以
  可选地返回一个承诺,Protractor将在之前等待   关闭webdriver。         此时,测试将完成,但全局对象仍然可用。   onComplete?: () => void

一旦成功触发电子邮件,您需要将您的函数转换为返回Promise。请参阅此beautiful tutorial。他们在转换fs.readFile()以返回承诺

时有一个很好的例子

你可以这样做。

onComplete: function() {
    return new Promise(function (fulfill, reject){
        var transporter = nodemailer.createTransport({
            host: 'smtp.gmail.com',
            port: 465,
            secure: true, // use SSL
            auth: {
                user: 'email',
                pass: 'password'
            }
        });
        var mailOptions = {
            from: '"TestMail" <>', // sender address (who sends)
            to: 'receiver's email', // list of receivers (who receives)
            subject: 'Hello through conf', // Subject line
            text: 'Hello world ', // plaintext body
            html: '<b>Hello world </b><br> This is the first email sent with Nodemailer in Node.js', // html body
    };
        transporter.sendMail(mailOptions, function(error, info){
            if(error){
                reject(err);
            }
            fulfill(info);
        });
    });
}