javascript - 如何使用 cypress.io 使用两个 Gmail 帐户收件箱

标签 javascript node.js gmail-api cypress

如何在 cypress 上使用 2 个 Gmail 帐户

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

现在我想在其他测试中对两封电子邮件使用 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 的两次测试中。
怎样才能分开账户。 谢谢

最佳答案

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

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

关于javascript - 如何使用 cypress.io 使用两个 Gmail 帐户收件箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63224587/

相关文章:

node.js - TypeScript Node.js 应用程序的分析

Javascript 提取无法从 GMail API 批处理请求中读取多部分响应

javascript - Parse.com RESTful 一对多请求,相关子项已填充

javascript - 这个javascript日期差异计算函数有什么问题?

javascript - 通过目标页面上的 JavaScript 检测服务器端重定向流量

node.js - Mongoose 搜索产生神秘错误

javascript - 使用JS SDK获取点赞状态

node.js - 等待时运行其他代码

javascript - 类型错误 : Cannot read property 'gmail' of undefined

google-api - 获取用户的 google(gmail 个人资料图片或 google+)头像个人资料图片