gmail-out-inbox-tester
This version is based on: gmail-tester
A simple Node.js Gmail client which checks/returns email message(s) straight from any Gmail-powered account (both private and company).
There are two main functionalities this library provides:
check_inbox()
: Polls a mailbox for a given amount of time. At the end of the operation, the desired message is returned (if found).get_messages()
: Can be used to perform various assertions on the email objects (see example below).check_outbox()
: Polls a mailbox for a given amount of time. At the end of the operation, the desired messages is returned (if found).
P.S, I have written a story on medium, how using Cypress, we are testing our user registration process at Tastewise.
Usage
- Install using
npm
:
npm install --save-dev gmail-out-inbox-tester
- Save the Google Cloud Platform OAuth2 Authentication file named
credentials.json
inside an accessible directory (see instructions below). - In terminal, run the following command:
node <node_modules>/gmail-tester/init.js <path-to-credentials.json> <path-to-token.json> <target-email>
<path-to-credentials.json>
Is the path to 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.
- Once done, go to https://console.cloud.google.com/apis/credentials?project=(project-name)&folder&organizationId and download the OAuth2 credentials file, as shown in the image below. Make sure to replace
(project-name)
with your project name.
The credentials.json
file should look like this:
If everything is done right, the last output from the script should be
[gmail] Found!
- Congratulations!
gmail-tester
is ready to use.
API
get_messages(credentials_json, token_path, options)
credentials_json
: Path to credentials JSON file.
token_path
: Path to OAuth2 token file.
options
:
from
: String. Filter on the email address of the receiver.to
: String. Filter on the email address of the sender.subject
: String. Filter on the subject of the email.include_body
: boolean. Set totrue
to fetch decoded email bodies.before
: Date. Filter messages received after the specified date.after
: Date. Filter messages received before the specified date.
Returns:
An array of email
objects with the following fields:
from: "Human Friendly Name <sender@email-address>" receiver: "your@email-address" subject: "string" body: html: "string" text: "string" // ...;
Some senders will send you text/html
content, the others will send you plain/text
, and some will send you both. Make sure you are looking for the content in the right body field.
Example
check_inbox()
to look for a specific message:
Using const path = ;const gmail = ;const email = await gmail;if email console; else console;
get_messages()
to assert email body using Cypress
Using examples\cypress\plugins\index.js:
/// <reference types="Cypress" />const debug = ;const path = ;const gmail_tester = ; module { ; ;};
examples\cypress\integration\gmail.spec.js:
/// <reference types="Cypress" /> ;
Contributing
Please feel free to contribute to this project.
Credits
- Built using googleapis.