Configuring Google Email Account for Cypress/Playwright Tests

It’s a challenge sometimes to find the right steps to configure google email inbox directly from your tests. This article will explain step-by-step instructions to configure your google email account and access your inbox from tests directly.

Account Setup

Before using Google APIs, you need to turn them on in a Google Cloud project. In the Google Cloud console, enable the Gmail API. Enable the API

Setup of Authorize credentials for a desktop application (Needed for Gmail)

  1. In the Google Cloud console, go to Menu menu > APIs & Services > Credentials. Go to Credentials
  2. Click Create Credentials > OAuth client ID.
  3. Click Application type > Desktop app.
  4. In the Name field, type a name for the credential. This name is only shown in the Google Cloud console.
  5. Click Create. The OAuth client-created screen appears, showing your new Client ID and Client secret.
  6. Click OK. The newly created credential appears under OAuth 2.0 Client IDs.
  7. Save the downloaded JSON file as, and move the file to your working directory.

Open a project in visual code, it can be your cypress/Playwright project, and execute the following commands:

npm install --save-dev gmail-tester
  1. Save the Google Cloud Platform OAuth2 Authentication file named credentials.json inside an accessible directory (see instructions below).
  2. In the terminal, run the following command. Make sure you are on the correct path where the node_modules folder is there.
node node_modules/gmail-tester/init.js credentials.json path-to-token.json sample.account.123@gmail.com
  • <path-to-credentials.json> Is the path to the OAuth2 Authentication file.
  • <path-to-token.json> Is the path to OAuth2 token. If it doesn’t exist, the script will create it.
    The script will prompt you to go to google.com to activate a token. Go to the given link, and select the account for <target-email>. Grant permission to view your email messages and settings. At the end of the process you should see the token:
copy-token

Hit the copy button and paste it to init.js script. The process should look like this:

Run script

How to get credentials.json?

  1. Follow the instructions to Create a client ID and client secret. Make sure to select Desktop app for the application type.
  2. Once done, go to <a href="https://npmtrends.com/https://console.cloud.google.com/apis/credentials?project=(project-name)&folder&organizationId&folder&organizationId>) and download the OAuth2 credentials file, as shown in the image below. Make sure to replace (project-name) with your project name.
<p align="center">
  <img src="https://i.ibb.co/z5FL6YK/get-credentials-json.png" alt="Get credentials.json">
</p>

The `credentials.json` file should look like this:<p align="center">
<img src="https://i.ibb.co/1stgn28/credentials.png" alt="Credentials file">

</p>
  1. Make sure the Gmail API is activated for your account.

If everything is done right, the last output from the script should be:

gmail Found!

Congratulations! gmail-tester is ready to use.
⛔️ Never share or commit credentials.json nor token.json!!! Whoever has it will have full access to your inbox!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s