首页 > 解决方案 > 如何检查主题中的“测试邮件”?我想写一个量角器测试来验证是否收到电子邮件

问题描述

这是我在 conf.js 文件中的 onPrepare() 函数。

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();
    },

我有一个用 mail.js 文件编写的辅助函数

'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();

我有一个功能要测试,它会在提交表单后发送电子邮件。我想测试收件人是否收到了电子邮件。测试如下:

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");
        });
        
    });
});

所以通常测试应该失败并显示错误消息

expected "test mail" to equal "Critical Security alert"

但即使没有收到电子邮件,它也会通过。

控制台输出

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

标签: javascriptprotractores6-promise

解决方案


我相信您在这里缺少 return 关键字

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");
        });
        
    });
});

推荐阅读