代码之家  ›  专栏  ›  技术社区  ›  Shruti

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

  •  0
  • Shruti  · 技术社区  · 6 年前

    我需要在执行完成后与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条件时。电子邮件被成功触发。请给我任何解决方案。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Maxim    6 年前

    你的if语句永远不会被触发。 根据你的信息,我想说,你必须在afterEach方法中设置一个变量,表示如果你的一个测试失败。 然后,你必须在最终的方法中使用它来进行if陈述。

    我使用cucumber和量角器,处理方式如下:

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