I'am using the following mailListner
library for an E2E Protractor test and also based on the info from this posting which looks quite good.
The issue I'am facing is regarding the function getLastEmail()
:
import { mailListener } from 'mail-listener2';
function getLastEmail() {
const deferred = protractor.promise.defer();
console.log("Waiting for an email...");
mailListener.on("mail", function(mail){
deferred.fulfill(mail);
});
return deferred.promise;
}
When I run the test, I keep getting the error:
- Failed: Cannot read property 'on' of undefined
It looks like the mailListner
is undefined.
Here is the part where the function is being invoked:
describe('sales App', () => {
it('Should send confirmation email', () => {
browser.controlFlow().wait(getLastEmail())
.then((email) => {
expect(email['subject']).toEqual("Confirm Registration");
expect(email['headers'].to).toEqual("firstName@yyy.com");
const pattern = /Registration code is: (\w+)/g;
const regCode = pattern.exec(email['text'])[1];
console.log(regCode);
});
});
});
protractor.confi.js
:
onPrepare() {
require('ts-node').register({
project: 'e2e/tsconfig.e2e.json'
});
jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
var MailListener = require("mail-listener2");
// here goes your email connection configuration
var mailListener = new MailListener({
username: "myEmail@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.on("error", function(err) {
console.log(err)
})
mailListener.on("server:disconnected", function() {
console.log("imapDisconnected")
})
global.mailListener = mailListener
},
onCleanUp: function() {
mailListener.stop()
}
Any idea how to work around and fix it?