放置在量角器配置 afterLaunch() 块中时,nodemailer 不发送邮件
nodemailer not sending mail when placed in protractor config afterLaunch() block
nodemailer 放置在量角器配置 afterLaunch() 块中时不发送邮件。
它在放置在 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()没有被执行
我之前也遇到过类似的问题
试试这个:
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
}
};
nodemailer 放置在量角器配置 afterLaunch() 块中时不发送邮件。 它在放置在 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()没有被执行
我之前也遇到过类似的问题
试试这个:
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
}
};