email - 如何在 Cypress 中发送带有测试报告的电子邮件
问题描述
我正在努力实现以下目标:
- 创建仅包含测试名称和状态(失败/通过)的简单测试报告
- 通过电子邮件将此报告作为基本 HTML 发送。
为此,我需要:
- 一个基本的记者而不是默认的
- 图书馆,可以发送电子邮件。我已经试过了
nodemailer
。但是,当我将它与赛普拉斯解决方案连接时,它不会发送任何电子邮件。我尝试了不同的邮箱帐户(nodemailer.createTestAccount()
一个来自我的公司,一个来自 SendGrid),但这不起作用(我没有收到任何电子邮件)
关于第 2 点,这是我使用的代码示例。这是 index.js 文件中的代码 - 我需要在所有测试后发送它:
after(() => {
var nodemailer = require('nodemailer');
var sgTransport = require('nodemailer-sendgrid-transport');
var options = {
auth: {
api_user: 'sendgrid_USER',
api_key: 'sendgrid_APIKEY'
}
}
var client = nodemailer.createTransport(sgTransport(options));
var email = {
from: 'FROM_MAIL.PL',
to: 'TO_MAIL.PL',
subject: 'Hello',
text: 'Hello world',
html: '<b>Hello world</b>'
};
client.sendMail(email, function(err, info){
if (err ){
console.log(error);
}
else {
console.log('Message sent: ' + info.response);
}
});
});
解决方案
Nodemailer是 Node.js 的一个模块,因此您需要在 Cypress 任务中运行它。
将此添加到您的/cypress/plugins/index.js文件中
const sendAnEmail = (message) => {
const nodemailer = require('nodemailer');
const sgTransport = require('nodemailer-sendgrid-transport');
const options = {
auth: {
api_user: 'sendgrid_USER',
api_key: 'sendgrid_APIKEY'
}
}
const client = nodemailer.createTransport(sgTransport(options));
const email = {
from: 'FROM_MAIL.PL',
to: 'TO_MAIL.PL',
subject: 'Hello',
text: message,
html: '<b>Hello world</b>'
};
client.sendMail(email, function(err, info) {
return err? err.message : 'Message sent: ' + info.response;
});
}
module.exports = (on, config) => {
on('task', {
sendMail (message) {
return sendAnEmail(message);
}
})
}
然后在测试中(或在/cypress/support/index.js中进行所有测试)
after(() => {
cy.task('sendMail', 'This will be output to email address')
.then(result => console.log(result));
})
这是此处示例的基本重构,您可以根据需要进行调整。
推荐阅读
- algorithm - 在彩色图中查找具有单个不同颜色边的循环
- python - 获取属性错误:类型对象“Word2Vec”在进行情绪分析时没有属性“load_word2vec_format”
- haskell - 无法创建幻影应用函子类
- javascript - 在 JavaScript 中从另一个数组中获取一个数组的元素
- mysql - 我想将第二个查询与第一个查询合并,并将产品媒体和类别媒体显示为 MY SQL Magento 2 中的 URL
- amazon-web-services - AWS CodeDeploy - 我可以在哪里配置 ASG 的部署选项?
- c++ - 这是一个在二叉树中插入数据但时间限制超过的程序
- python - 从 CSV 文件计算标题中带有单词 the 的电影
- sql - SQL GROUP BY 查找最大日期
- angular - 使用 CanActivate 时重定向到特定页面