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.
- Create a Gmail account e.g.
- Node.js & npm installed.
- A Google Cloud project.
- A Google account with Gmail enabled.
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)
- In the Google Cloud console, go to Menu menu > APIs & Services > Credentials. Go to Credentials
- Click Create Credentials > OAuth client ID.
- Click Application type > Desktop app.
- In the Name field, type a name for the credential. This name is only shown in the Google Cloud console.
- Click Create. The OAuth client-created screen appears, showing your new Client ID and Client secret.
- Click OK. The newly created credential appears under OAuth 2.0 Client IDs.
- 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
- Save the Google Cloud Platform OAuth2 Authentication file named
credentials.jsoninside an accessible directory (see instructions below).
- 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 firstname.lastname@example.org
<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:
Hit the copy button and paste it to
init.js script. The process should look like this:
How to get credentials.json?
- Follow the instructions to Create a client ID and client secret. Make sure to select
Desktop appfor the application type.
- 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>
- Make sure the Gmail API is activated for your account.
If everything is done right, the last output from the script should be:
gmail-tester is ready to use.
⛔️ Never share or commit
token.json!!! Whoever has it will have full access to your inbox!