npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gmail-test

v1.0.5

Published

adjusted az-gmail-tester packadge for Codeceptjs

Downloads

27

Readme

gmail-test

Package based on az-gmail-tester with small changes

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:

  1. check_inbox(): Polls a mailbox for a given amount of time. At the end of the operation, the desired message is returned (if found).
  2. get_messages(): Can be used to perform various assertions on the email objects
  3. send(): Sends message using your gmail account

Usage

  1. Install using npm:
npm install --save-dev gmail-test
  1. Save the Google Cloud Platform OAuth2 Authentication file named credentials.json inside an accessible directory (see instructions below).
  2. In terminal, run the following command:
node node_modules/gmail-test/init.js cypress/support/gmail/credentials.json cypress/support/gmail/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?

  1. Follow the instructions to Create a client ID and client secret.
  2. 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!

  1. Congratulations! gmail-test 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:

  • include_body: boolean. Set to true 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

Using check_inbox() to look for a specific message:

const path = require("path");
const gmail = require("gmail-tester");
const email = await gmail.check_inbox(
  path.resolve(__dirname, "credentials.json"), // Assuming credentials.json is in the current directory.
  path.resolve(__dirname, "gmail_token.json"), // Look for gmail_token.json in the current directory (if it doesn't exists, it will be created by the script).
  "Activate Your Account", // We are looking for 'Activate Your Account' in the subject of the message.
  "[email protected]", // We are looking for a sender header which has '[email protected]' in it.
  "<target-email>", // Which inbox to poll. credentials.json should contain the credentials to it.
  10, // Poll interval (in seconds).
  30 // Maximum poll time (in seconds), after which we'll giveup.
);
if (email) {
  console.log("Email was found!");
} else {
  console.log("Email was not found!");
}

Implementation check_inbox() to get email body using Codeceptjs

// in Helper
const Helper = require('@codeceptjs/helper');
const gmail = require("gmail-test");

class CustomHelper extends Helper {

    async getMail(email, subject, from) {
        return await gmail.check_inbox(
            "credentials.json", // Assuming credentials.json is in the current directory.
            "token.json", // Look for gmail_token.json in the current directory (if it doesn't exists, it will be created by the script).
            subject, // We are looking for 'Activate Your Account' in the subject of the message.
            from, // We are looking for a sender header which has '[email protected]' in it.
            email, // Which inbox to poll. credentials.json should contain the credentials to it.
            10, // Poll interval (in seconds).
            60, // Maximum poll time (in seconds), after which we'll giveup.
            { // optional parameter
                include_body: true, // to return body of the mail
                limit: 3 // to look through recent quantity of mails
            }
        );
    }
}

module.exports = CustomHelper;

Usage check_inbox() to get email body using Codeceptjs

//in test
Scenario('Login with url from email', async () => {
    I.amOnPage('/someUrl');
    I.fillField('loginField', 'some email address');
    I.fillField('passwordField', 'some password');
    I.click('registerButton');
    let mail = await I.getMail('some email address', 'registration topic', 'some sender email');
    let link = mail.body.html.match(/<a[^>]+href="([^\"]+)"+>link<\/a> e.g. Some link href/);
    I.amOnPage(link[1]);
    I.waitForText('You are logged in!')
});

Credits