【问题标题】:How to use two Gmail account inboxs using cypress.io如何使用 cypress.io 使用两个 Gmail 帐户收件箱
【发布时间】:2020-11-23 05:36:42
【问题描述】:

如何在 cypress 中使用 2 个 gmail 帐户

我创建了 2 个文件 credentialsclient.json 和 credentials.json 并创建 2 个 gmail 令牌文件。

现在我想在其他测试中对两封电子邮件使用 get-messages 功能。

我在我的 index.js 文件中编写选定的代码,


module.exports = (on, config) => {
  on("before:browser:launch", (browser = {}, launchOptions) => {
    // `args` is an array of all the arguments that will
    // be passed to browsers when it launches
    console.log(launchOptions.args); // print all current args
    if (browser.family === "chromium" && browser.name !== "electron") {
      // auto open devtools
      launchOptions.args.push("--auto-open-devtools-for-tabs");
      // allow remote debugging
      // launchOptions.args.push("--remote-debugging-port=9221");
      // whatever you return here becomes the launchOptions
    } else if (browser.family === "firefox") {
      // auto open devtools
      launchOptions.args.push("-devtools");
    }
    return launchOptions;
  });
  on("task", {
    "gmail:get-messages": async args => {
      const messages = await gmail_tester.get_messages(
        path.resolve(__dirname, "credentialsclient.json"),
        path.resolve(__dirname, "gmail_tokenclient.json"),
        args.options
      );
      return messages;
    }
  });
};

对于下一个 gmail,我想使用另一个功能

on("task", {
    "gmail:get-messages": async args => {
      const messages = await gmail_tester.get_messages(
        path.resolve(__dirname, "credentials.json"),
        path.resolve(__dirname, "gmail_token.json"),
        args.options
      );
      return messages;
    }

但在使用相同 gmail 的两个测试中。
我怎样才能分开帐户。 谢谢

【问题讨论】:

    标签: javascript node.js gmail-api cypress


    【解决方案1】:

    我不太确定您为什么使用任务而不是函数,但您可以简单地将属性添加到传递给任务的参数中:

    on("task", {
        "gmail:get-messages": async args => {
          // Use the credentials property or an empty string if none is provided
          const credentialType = args.credentials || '';
    
          const messages = await gmail_tester.get_messages(
            path.resolve(__dirname, `credentials${args.credentials}.json`),
            path.resolve(__dirname, `gmail_token${args.credentials}.json`),
            args.options
          );
          return messages;
        }
    
    

    当你调用你的任务时,你只需传入凭据类型:

    cy.task('gmail:get-messages', { options: {}, credentials: 'client' });
    

    【讨论】:

    • 我在这里使用任务。 Cypress.Commands.add('checkEmail', (emailaddr, title,) => { const yestDate = Cypress.moment().add(-1, 'hours').format('YYYY, MM, DD'); cy .task("gmail:get-messages", { options: { from: emailaddr, subject: title, include_body: true, after: new Date (yestDate) // before: new Date(todaysDate) } }) })跨度>
    【解决方案2】:

    我使用 2 个参数,就像这样。

    on("task", {
        "gmail:get-messagescl": async args => {
          const messages = await gmail_tester.get_messages(
            path.resolve(__dirname, "credentialsclient.json"),
            path.resolve(__dirname, "gmail_tokenclient.json"),
            args.options
          );
          return messages;
        },
        "gmail:get-messages": async args => {
          const messages = await gmail_tester.get_messages(
            path.resolve(__dirname, "credentials.json"),
            path.resolve(__dirname, "gmail_token.json"),
            args.options
          );
          return messages;
        }
      });
    

    在 command.js 中我写了 2 个函数

    Cypress.Commands.add('checkEmail', (emailaddr, title,) => {
        const yestDate = Cypress.moment().add(-1, 'hours').format('YYYY, MM, DD');
        cy.task("gmail:get-messages", {
            options: {
              from: emailaddr,
              subject: title,
              include_body: true,
              after: new Date (yestDate) 
             // before:  new Date(todaysDate)
            }
          })
    })
    
    Cypress.Commands.add('checkEmailCl', (emailaddr, title,) => {
        const yestDate = Cypress.moment().add(-1, 'hours').format('YYYY, MM, DD');
        cy.task("gmail:get-messagescl", {
            options: {
              from: emailaddr,
              subject: title,
              include_body: true,
              after: new Date (yestDate) 
             // before:  new Date(todaysDate)
            }
          })
    })
    

    在我的测试中,我已经在使用我的函数了

    
    cy.checkEmail(emailadr, title ).then(emails => {
              assert.isAtLeast(
                emails.length,
                1,
                "Expected to find at least one email, but none were found!"
              );
              const body = emails[0].body.html;
              cy.log(body)
    
              assert.isTrue(
                body.indexOf(
                  "https://u15696639.ct.sendgrid.net/ls/click?upn="
                ) >= 0,
                "Verify your email"
              );
    

    其他测试使用

     cy.checkEmailCl(emailadr, title ).then(emails => {
    

    【讨论】:

      猜你喜欢
      • 2018-03-10
      • 2013-04-15
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 2015-10-18
      • 2016-09-12
      • 2014-03-01
      • 1970-01-01
      相关资源
      最近更新 更多