How to use two Gmail account inboxs using cypress.io

Viewed 644

How to I can use 2 gmail accounts with cypress

I create 2 files credentialsclient.json and credentials.json and create 2 gmail token files.

Now I want to use get-messages function for two emails in other tests.

I am write selected code in my index.js file,


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

And for next gmail I want use another function

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

But in two tests using the same gmail.
How can I to separate the accounts. Thanks

2 Answers

I'm not really sure why you are using a task instead of a function, but you can simply add a property to the arguments you pass to the task:

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

And when you call your task you simply pass in the credentials type:

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

I use 2 args, like this.

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

And in command.js I write 2 functions

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

In my tests already I am using my functions


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

And for other test using

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