I do have an E2E test for retrieving confirmation email after registration. I need the link in the confirmation email to login the user as the link in the email contains the token etc. for access.
The test runs fine, email is being sent to user, but when I try to get email and its content including link, it just doesn't invoke the function (callback) for it which causes the following error:
- Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
The test is based on mailListener
and following this posting which is quite helpful.
Here is the helper function which should be invoked and retrieve the mail with content. It's being embedded within spec.ts
file before starting the suite describe()
:
function getLastEmail() {
const deferred = protractor.promise.defer();
console.log("Waiting for an email...");
mailListener.on("mail", function(mail){
console.log('Email received, parsing...');
deferred.fulfill(mail);
});
return deferred.promise;
}
and here is the test where the function getLastEmail()
is being called:
it('should retrieve email link and login with a registration code sent to an email', function () {
browser.get("/login");
browser.controlFlow().wait(getLastEmail())
.then((email) => {
expect(email['subject']).toEqual("Email subject");
expect(email['headers'].to).toEqual("myName@yyyy.com");
console.log(email['text']);
});
});
And here is the initialization of the Mail Listener which is also embedded in the spec.ts
file in the top:
// for confirmation email - added on 31.01.18
var MailListener = require("mail-listener2");
// here goes your email connection configuration
var mailListener = new MailListener({
username: "myName@yyyy.com",
password: "myPassword",
host: "imap.gmail.com",
port: 993, // imap port
tls: true,
tlsOptions: { rejectUnauthorized: false },
mailbox: "INBOX", // mailbox to monitor
searchFilter: ["UNSEEN", "FLAGGED"], // the search filter being used after an IDLE notification has been retrieved
markSeen: true, // all fetched email willbe marked as seen and not fetched next time
fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`,
mailParserOptions: { streamAttachments: true }, // options to be passed to mailParser lib.
// attachments: true, // download attachments as they are encountered to the project directory
// attachmentOptions: { directory: "attachments/" } // specify a download directory for attachments
});
mailListener.start();
mailListener.on("server:connected", function(){
console.log("Mail listener initialized");
});
mailListener = mailListener;`
Can it be that the email credentials are not correct or the host name is wrong as in this case I would expect an error? What do I have to change to force the function: getLastEmail()
to be invoked?
var mailListener = new MailListener({....
and now I get an output saying:<= '* 55 EXISTS' => DONE <= 'IDLE OK IDLE terminated (Success)' => 'A5 UID SEARCH UNSEEN FLAGGED' <= '* SEARCH' <= 'A5 OK SEARCH completed (Success)' => 'IDLE IDLE' <= '+ idling'
, but to be honest I really can't figure out what the output exactly means and I still get the error:- Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
. Any help pls.? I'am stuck with this issue!