gpt4 book ai didi

javascript - mailListner - 无法读取未定义的属性 'on'

转载 作者:行者123 更新时间:2023-12-03 02:35:14 25 4
gpt4 key购买 nike

我正在使用以下mailListner用于 E2E Protractor 测试的库,也基于此 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;
}

当我运行测试时,我不断收到错误:- 失败:无法读取未定义的“on”属性

看起来 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("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()
}

知道如何解决和修复它吗?

最佳答案

我猜测它引用了这一行,mailListener.on("mail"...,所以你的mailListener未定义。

如果您最初关注 this answer并在您的config中实例化mailListener,您会注意到他们为mailListener创建了一个全局变量,在调用函数getLastEmail时可以使用它。

或者,如果您在规范中执行此操作(即在 beforeAll block 中),您仍然需要创建一个变量来引用 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) {...

关于javascript - mailListner - 无法读取未定义的属性 'on',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48562841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com