如何使用 nodemailer 为通过的测试用例和失败的测试用例发送具有不同主题行的电子邮件

How to send email with different subject lines for passed testcase and failed testcase using nodemailer

我需要在执行完成后用nodemailer发送邮件。在完成所有测试用例后,即使一个规范失败,电子邮件主题行也应说明测试用例已失败。如果所有测试用例都通过了,那么它应该说明所有测试用例都已成功通过。

this.specDone = function(result) {
      if (result.failedExpectations.length > 0) {
        let mailOptions = {
          from: '"Mathur, Shruti" <xxx@xx.com>',
          to: 'xxx@xx.com',
          subject: 'Liability Management automation Report-Test Suite Failure',
          text: 'Test case completed',
          html: 'Hi Team,<br><br> Test Automation for <b>Liability Management UI</b> through Protractor has been completed. There is <b>failure</b> for one or more than one test suites.<br>Please find the attached report for reference.',
          attachments: [{
           path: 'C:/Shruti/Protractor_Autodistribution/my-app/Test/report.zip'
            }]
        };
     }else{
      let mailOptions = {
        from: '"Mathur, Shruti" <xxx@xx.com>',
        to: 'xxx@xx.com',
        subject: 'Liability Management automation Report-All Test Suite Passed',
        text: 'Test case completed',
        html: 'Hi Team,<br><br> Test Automation for <b>Liability Management UI</b> through Protractor has been completed.All Test suites are <b>passed</b> successfully.<br>Please find the attached report for reference.',
        attachments: [{
         path: 'C:/Shruti/Protractor_Autodistribution/my-app/Test/report.zip'
          }]
      };
     }
   };

这是我的配置文件代码,当我执行它时,没有任何反应。没有电子邮件被触发。当我不使用 if 条件时。电子邮件被触发 successfully.Please 给我任何解决方案。

您的 if 语句永远不会被触发。 根据您的信息,我会说您必须在 afterEach 方法中设置一个变量,该变量表示您的一项测试是否失败。 然后你必须在你的 if 语句的 afterAll 方法中使用它。

我将黄瓜与量角器一起使用,并像这样处理它:

var failed = false;

After(function (scenario) 
{
    console.log("After " + scenario.pickle.name + " in " + scenario.sourceLocation.uri + ", result: " + scenario.result.status);
    if (scenario.result.status === Status.FAILED)
    {       
        //do stuff if one test failes
        failed = true;              
        const attach = this.attach;

        return browser.takeScreenshot().then(function(png)
        {
            return attach(new Buffer(png, "base64"), "image/png");
        });
    }
});

AfterAll(function(callback)
{
    console.log("AfterAll");
    if (failed)
    {       
        var mailOptions = {
            from: 'yourmail', // sender address (who sends)
            //to: process.env.reportlist,
            to: 'other mail',
            subject: (process.env.COMPUTERNAME || 'testrunner') + ': WARNING! your automated test result has found error(s)', // Subject line
            text: 'Your automated test has accured an error! Visit the report for more details: 

            /*attachments: [
            {
                filename: 'report.html',
                path: htmlReport,

            }]*/
        };

        transporter.sendMail(mailOptions, function(error, info)
        {
            if(error)
            {
                return console.log(error);
            } 
            console.log('Email sent: ' + info.response);
            console.log(info);
        });

    } else {
        fs.writeFile(errorLog + "reportLog.txt", "SUCCESS");
        let mailOptions = {
          from: '"Mathur, Shruti" <xxx@xx.com>',
          to: 'xxx@xx.com',
          subject: 'Liability Management automation Report-All Test Suite Passed',
          text: 'Test case completed',
          html: 'Hi Team,<br><br> Test Automation for <b>Liability Management UI</b> through Protractor has been completed.All Test suites are <b>passed</b> successfully.<br>Please find the attached report for reference.',
          attachments: [{
          path: 'C:/Shruti/Protractor_Autodistribution/my-app/Test/report.zip'
      }]
  };
 }