This is my onPrepare() function in conf.js file.
onPrepare: function () {
var mailListener = new MailListener({
username: "email_address",
password: "password",
host: "imap.gmail.com",
port: 993, // imap port
tls: true,
// connTimeout: 10000, // Default by node-imap
// authTimeout: 5000, // Default by node-imap,
debug: console.log, // Or your custom function with only one incoming argument. Default: null
tlsOptions: { rejectUnauthorized: false },
mailbox: "INBOX", // mailbox to monitor
searchFilter: ["UNSEEN", ["FROM", "test123@mail.com"],["SUBJECT","test mail"]], // 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(); // start listening
mailListener.on("server:connected", function(){
console.log("Mail listener initialized");
});
global.mailListener = mailListener;
},
onCleanUp: function () {
mailListener.stop();
},
I have a helper function written in mail.js file
'use strict';
const { browser } = require("protractor");
var Mail = function () {
this.getLastEmail = function () {
var deferred = protractor.promise.defer();
console.log("Waiting for an email...");
mailListener.on("mail", function (mail) {
console.log("inside maillistener....")
deferred.fulfill(mail);
});
return deferred.promise;
}
}
module.exports = new Mail();
I have a feature to test which sends an email after form submission. And I want to test if the email is received by the recipient. The test is as follows:
describe("Form submission.", function () {
it("should test whether an email is received.", function () {
/**
*
* Here comes the code up to the plan submission
*/
mail.getLastEmail().then(function(email) {
expect(email.subject).toEqual("Critical Security alert");
});
});
});
So usually the test should fail with error message
expected "test mail" to equal "Critical Security alert"
but it gets passed even if the email is not received.
Console Output
Jasmine started
[connection] Connected to host
<= '* OK Gimap ready for requests from 106.195.12.92 l20mb169733167ioj'
=> 'A0 CAPABILITY'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=OAUTHBEARER AUTH=XOAUTH'
<= 'A0 OK Thats all she wrote!'
=> 'A1 LOGIN "email_address_here" "password_here"'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT LIST-EXTENDED LIST-STATUS LITERAL- SPECIAL-USE APPENDLIMIT=35651584'
<= 'A1 OK test@gmail.com authenticated (Success)'
=> 'A2 NAMESPACE'
<= '* NAMESPACE (("" "/")) NIL NIL'
<= 'A2 OK Success'
=> 'A3 LIST "" ""'
<= '* LIST (\\Noselect) "/" "/"'
<= 'A3 OK Success'
=> 'A4 SELECT "INBOX" (CONDSTORE)'
<= '* FLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen $NotPhishing $Phishing)'
<= '* OK [PERMANENTFLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen $NotPhishing $Phishing \\*)] Flags permitted.'
<= '* OK [UIDVALIDITY 1] UIDs valid.'
<= '* 3 EXISTS'
<= '* 0 RECENT'
<= '* OK [UIDNEXT 5] Predicted next UID.'
<= '* OK [HIGHESTMODSEQ 7415]'
<= 'A4 OK [READ-WRITE] INBOX selected. (Success)'
Mail listener initialized
=> 'A5 UID SEARCH UNSEEN FROM "test123@mail.com" SUBJECT "test mail"'
<= '* SEARCH'
<= 'A5 OK SEARCH completed (Success)'
=> 'IDLE IDLE'
<= '+ idling'
Outside call
Waiting for an email...
Form submission.
✓ should test whether an email is received.
Executed 1 of 1 spec SUCCESS in 48 secs.
=> DONE
[17:44:24] I/launcher - 0 instance(s) of WebDriver still running
[17:44:24] I/launcher - chrome #01 passed
I believe you are missing a return keyword here
describe("Form submission.", function () {
it("should test whether an email is received.", function () {
...
return mail.getLastEmail().then(function(email) {
expect(email.subject).toEqual("Critical Security alert");
});
});
});