1

I am using Protractor-Cucumber framework with protractor 5.2.2 and cucumber 3.2. I have a requirement of posting in no.of locations. So I have written a script in a loop for it. But it randomly fails before completing the loop. So when the script ends abnormally, is there like an exception handling section that gets control before exiting.The script can be fail due to any of the reasons like web driver issue,NoSuchElementError,ElementIsNotIntractable,ElementIsNotVisible etc.So whatever be the issue I have to handle that, and if it fails, I have to do an email notification. I have tried try catch, as given below, but it does not work for me.

When(/^I login$/, function () {
  try{
    element(by.css(".signin")).click();
    var count=post_details.length ; 
    for (var i=0; i<count; i++){ 
      post();
     }
   }
  catch(e){
    console.log("failed");
  }
});

How we can do this in protractor-cucumber.Thanks in advance

Reza Mousavi
  • 4,420
  • 5
  • 31
  • 48
Devleena
  • 453
  • 9
  • 25

1 Answers1

0

For the exception problem you can try this. ignoreUncaughtException

For the email part create a hooks.js file. Here you can setup the After() function, to check your scenario fails or not. Cucumber Docs.

Example:

After(function (scenario) {
  if (scenario.result.status === Status.FAILED)
  {     
    failed = true;              
    const attach = this.attach;

    //creates a screenshot for the report
    return browser.takeScreenshot().then(function(png) {
        return attach(new Buffer(png, "base64"), "image/png");
    });
  }
});

Then you can use nodemailer to send messages. Nodemailer

In your AfterAll() function you can handle the send part.

Example:

AfterAll(function(callback){
  console.log("AfterAll");
  if (failed)
  {     
    var transporter = nodemailer.createTransport(
    {
        host: 'host.com',
        port: xx,
        secure: false,
        //proxy: 'http://10.10.10.6:1098',
        auth: {
            user: userMail,
            pass: pw
        }
    });

    var mailOptions = {
        from: 'xx', // sender address (who sends)
        to: xxxxxx@mail.com',
        subject: 'your subject', // Subject line
        text: 'Your test failed....', // plaintext body
        /*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 {
    //do your stuff
}
setTimeout(callback, 2000);
});
Maxim
  • 132
  • 5
  • Can you please explain more about using of ignoreUncaughtException.I have included this in my config file, and the message displaying in the command prompt is given below. I/launcher - 0 instance(s) of WebDriver still running I/launcher - chrome #01 failed 1 test(s) I/launcher - overall: 1 failed spec(s) E/launcher - Process exited with error code 1. So as explained in my question,even the error occurred during the execution of a for loop, it should skip that, and should move to the next loop-count. – Devleena Sep 28 '18 at 08:48
  • **Uncaught Exceptions** If your process abruptly stops with an exit code 199 then your steps most likely threw an uncaught exception. Protractor is capturing these and exiting the process in this situation. The solution is to upgrade to at least protractor version 4.0.10 and add the following to your protractor conf... `ignoreUncaughtExceptions: true` This allows cucumber to handle the exception and record it appropriately. – Maxim Oct 01 '18 at 07:49
  • You could give a try to the protractor "try/catch" method. Maybe it will help you. It´s explained here [link](https://stackoverflow.com/a/34775486/8931368) – Maxim Oct 01 '18 at 07:55
  • Maxim, can we use this Nodemailer for sending email from azure environment.I am able to successfully send email from my system. But when i am using the same code in azure environment, the mail sending is not working(not receiving any mail). – Devleena Nov 12 '18 at 04:48
  • I have not yet used nodemailer under Azure. Maybe this two links can help you: https://stackoverflow.com/a/48455683/8931368 or https://stackoverflow.com/questions/23580754/mail-for-nodemailer-didnt-work-on-azure-server/49315347#49315347 – Maxim Nov 12 '18 at 07:58