一尘不染

在量角器测试用例中从电子邮件中获取值

javascript

我需要测试一个量角器测试用例,在该用例中,用户注册,接收电子邮件,转到电子邮件中提供的链接,并在激活注册表单中填写他/她的详细信息。

问题是如何从电子邮件中获取兑换令牌。我的电子邮件具有指向激活页面的链接,该页面具有auth令牌,如下所示:

http://127.0.0.1:3000/#/signup/redeem/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJlOTRhYzY3MC1kYTNlLTQyYTUtODVkZS02NDU4ZjVmZGMwYjAiLCJzdWIiOiJ0ZXN0QGNvZWYuY28iLCJpYXQiOjE0Mjc0OTM5MDMsImV4cCI6MTQyODA5ODcwM30.

但是,如何获取该令牌以便构建URL或如何单击电子邮件中的该按钮以完成流程?我正在使用mailcatcher模拟电子邮件。


阅读 554

收藏
2020-05-01

共1个答案

一尘不染

这是我最近解决的问题。希望该解决方案也适用于您的用例。

先决条件:

  • mail-listener2
  • 对概念的理解 promises

分步说明:

  1. 安装mail-listener2
        npm install mail-listener2 --save-dev
  1. 在量角器配置中, 初始化Mail Listener 并使其全局可用:
        onPrepare: function () {
        var MailListener = require("mail-listener2");

        // here goes your email connection configuration
        var mailListener = new MailListener({
            username: "imap-username",
            password: "imap-password",
            host: "imap-host",
            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");
        });

        global.mailListener = mailListener;
    }),

    onCleanUp: function () {
        mailListener.stop();
    }, 
  1. 创建一个帮助getLastEmail()函数,该函数将等待email检索到:
        function getLastEmail() {
        var deferred = protractor.promise.defer();
        console.log("Waiting for an email...");

        mailListener.on("mail", function(mail){
            deferred.fulfill(mail);
        });
        return deferred.promise;
    };
  1. 测试用例示例:
        describe("Sample test case", function () {

        beforeEach(function () {
            browser.get("/#login");
            browser.waitForAngular();
        });

        it("should login with a registration code sent to an email", function () {
            element(by.id("username")).sendKeys("MyUserName");
            element(by.id("password")).sendKeys("MyPassword");
            element(by.id("loginButton")).click();

            browser.controlFlow().await(getLastEmail()).then(function (email) {
                expect(email.subject).toEqual("New Registration Code");
                expect(email.headers.to).toEqual("myemail@email.com");

                // extract registration code from the email message
                var pattern = /Registration code is: (\w+)/g;
                var regCode = pattern.exec(email.text)[1];

                console.log(regCode);

             });
        });
    });
2020-05-01