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

时间:2018-02-01 12:30:11

标签: javascript email protractor e2e-testing

我正在使用以下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("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()
}

任何想法如何解决并修复它?

1 个答案:

答案 0 :(得分:1)

我猜测它引用了这一行mailListener.on("mail"...,因此您的mailListener未定义。

如果您最初关注this answer并在配置中实例化mailListener,则会注意到他们为mailListener创建了一个可供该函数使用的全局getLastEmail {1}}当它被调用时。

或者,如果您在规范中(即在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) {...