我正在使用以下mailListner
库进行E2E量角器测试,并且还基于该posting中的信息,看起来不错。
我面临的问题与功能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;
}
运行测试时,我不断收到错误消息:
- Failed: Cannot read property 'on' of undefined
看来
mailListner
是未定义的。这是调用函数的部分:
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("[email protected]");
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: "[email protected]",
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()
}
任何想法如何解决和修复它?
最佳答案
我猜它是在引用mailListener.on("mail"...
这行,所以您的mailListener
是不确定的。
如果您最初遵循this answer并在配置中实例化mailListener,您会注意到它们为mailListener
创建了一个全局变量,该全局变量在调用getLastEmail
时可用。
或者,如果您在规范中(例如,在beforeAll
块中)执行此操作,则在实例化它时仍会创建一个引用mailListener
的变量,然后将其传递给函数。
// page objects
getLastEmail = function (serv) {
var deferred = protractor.promise.defer();
console.log("Waiting for an email...");
serv.on("mail", function(mail){
console.log('Email received, parsing...');
deferred.fulfill(mail);
});
return deferred.promise;
};
// specs
const MailListener = require("mail-listener2");
mailListener = new MailListener({...
browser.wait(Util.getLastEmail(mailListener), 30000).then(function (mail) {...