2

I am using mail-listener2 to monitor an email account, as check that an email has been recieved as part of a test.

I have used the same implementation defined here: here:Fetching values from email in protractor test case

However, getLastEmail() returns an old email, rather than an email received after the mail-listener2 has started. It returns the first UNSEEN email.

I've looked at whether I can used different mail-listener2 configurations to solve this, but I haven't found anything. I've also tried to use a .last() on the mail returned, but this hasn't worked either.

Does anyone have a configuration solution, or a custom solution that would help to solve this problem?

Community
  • 1
  • 1
fuzzi
  • 1,967
  • 9
  • 46
  • 90

3 Answers3

3

I think this may help you, I implemented mail-listener2 using the same post you followed and it works great me for me. I just added a few extra parameters:

Under my config's onPrepare, I create a date:

var emailDate = new Date().getTime();

Then under my mailListener initialization:

    var mailListener = new MailListener({
        username: ...
        password: ...
        ...
        searchFilter: ["NEW", "UNSEEN", ["SINCE", emailDate]]
    });

This should configure mailListener to only look for emails delivered after the time of emailDate, which is created when your test is started. You can also specify an exact date, i.e. ['SINCE', 'May 20, 2010']

More info on the node-imap docs (which mailListener2 utilizes)

Gunderson
  • 3,263
  • 2
  • 16
  • 34
  • Thank you! This works, and is just what I needed. The link is also very useful. – fuzzi Jan 04 '17 at 21:22
  • I am using nightwatch for my e2e test. i am not sure how to use this mail-listener in nightwatch. because i do not have on prepare function in nightwatch. would you able to share the getLastEmail() ? so that i can have some ideas – James Jan 30 '20 at 00:30
0

I have this configuration in conf file in the mailListener configurations:

markSeen: true,

Every-time you read the email, it is marked as read and will not be fetched next time. This means that you will always read new emails.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
John Mburu
  • 11
  • 5
0

In my case was not enough the previous answer due differences in the server time with gmail server time. So a complete way could be:

    var currentDate =  new Date().toUTCString()
    
    var mailListener = new MailListener({
        ...
        markSeen: true,
        port: 993,
        tls: true,
        searchFilter: ["UNSEEN", ["SINCE", currentDate]]
    });

then your getLastEmail helper could be:

getLastEmail = function () {
var deferred = protractor.promise.defer();
console.log("Waiting for an email...");
const hrs = new Date().getUTCHours()
const minutes = new Date().getUTCMinutes()
mailListener.on("mail", function(mail){
    if( hrs=== new Date().getUTCHours("mail.headers.date") &&
        minutes- new Date().getUTCMinutes("mail.headers.date") <=1 ) {
        deferred.fulfill(mail);
    }
});

return deferred.promise;
}

This worked for me.

Cristian Zumelzu
  • 842
  • 10
  • 15