如何使用nodemailer为传递的测试用例和失败的测试用例发送包含不同主题行的电子邮件

时间:2018-05-28 13:08:21

标签: email protractor nodemailer subject

我需要在执行完成后通过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'
          }]
      };
     }
   };

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

1 个答案:

答案 0 :(得分:0)

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

我将黄瓜与量角器一起使用并按照这个例子进行处理:

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