gpt4 book ai didi

javascript - 在 Protractor 测试用例中从电子邮件中获取值

转载 作者:可可西里 更新时间:2023-11-01 01:28:05 25 4
gpt4 key购买 nike

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

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

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

但是我如何获取该 token 以便我可以构建 url 或者我如何单击我的电子邮件中的那个按钮以便我可以完成流程?我正在使用 mailcatcher 来模拟电子邮件。

最佳答案

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

先决条件:

分步说明:

  1. 安装mail-listener2:

    npm install mail-listener2 --save-dev
  2. 在您的 Protractor 配置中初始化邮件监听器并使其在全局范围内可用:

    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();
    },
  3. 创建一个辅助 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;
    };
  4. 示例测试用例:

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

    });
    });
    });

关于javascript - 在 Protractor 测试用例中从电子邮件中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29311154/

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