Puppetry
3.2.2
3.2.2
  • Welcome Puppetry
  • Getting Started
  • Project
  • Suite
  • Group
  • Target
    • Simple Target
    • iFrame Target
    • ShadowDOM Target
    • Chained Target
    • Shared Target
  • Test Case
  • Test Step / Action
    • Page Commands
    • Page Assertions
    • Target Commands
    • Target Assertions
  • Managing Records
  • Snippets
  • Running tests
    • Interactive Mode
    • Troubleshooting
  • Test Report
  • Export
    • Exporting as Jest Project (CI-friendly)
    • Exporting as Test Specification
  • Settings
  • Template Expressions
  • Testing Techniques
    • Testing Dynamic Content
    • Exhaustive Testing
    • Performance Testing
    • Visual Regression Testing
    • Testing Shadow DOM
    • Testing Google Analytics tracking code
    • Testing Chrome Extensions
    • Testing REST API
    • Mocking HTTP/S Requests
    • Testing Transactional Emails
      • Example with Restmail.net
      • Example with Mailinator
      • Example with IMAP bridge
      • Example with Google API
    • Testing Forms with Captcha
  • Version Control
  • Tips and Tricks
    • Embrace the Power of CSS
  • Command API
  • Test Application
Powered by GitBook
On this page

Was this helpful?

  1. Testing Techniques
  2. Testing Transactional Emails

Example with Google API

PreviousExample with IMAP bridgeNextTesting Forms with Captcha

Last updated 5 years ago

Was this helpful?

In the previous example we used IMAP client to connect to Gmail inbox. That can be also done by using Gmail API. You can find on GitHub a by Google, which implements OAuth authorization and retrieves list of labels from user's mailbox.

The example app is surprisingly easy to set up. You just need to follow .

As you run the app first time it asks you to follow a printed URL to Google Services where you will be prompted to register the app. At the end you receive a code, which hand back to the application.

But we do not need labels, but last received email addressed to a given user name alias. Well we can modify a bit . First let's get rid of fs.readFile('credentials.json', (err, content) => ... block. We rather get OAuth client with an asynchronous function instead of coping with callback hell:

/**
 * @returns {Promise} 
 */
function getOAuth2Client() {
  const credentials = fs.readFileSync( "credentials.json", "utf8" );
  return new Promise(( resolve ) => {
    authorize(JSON.parse( credentials ), ( auth ) => {
      resolve( auth );
    });
  });
}

Now implement a function that accepts and returns either raw body of the first matching the query message or nothing if no matches found:

/**
 * @param {google.auth.OAuth2} auth An authorized OAuth2 client.
 * @param {string} q - Google searchbox query
 * @returns {Promise} 
 */
async function getUnreadEmail( auth, q ) {
  const gmail = google.gmail({ version: 'v1', auth }),
        userId = "me";

  try {
    const listRsp = await gmail.users.messages.list({
      userId,
      q
    });

    if ( !listRsp.data.messages ) {
      return;
    }

    const [ msg ] = listRsp.data.messages,

          msgRsp  = await gmail.users.messages.get({
            userId,
            id: msg.id,
            format: "raw"
          }),

          rawBody = Buffer.from( msgRsp.data.raw || "", "base64" )
            .toString( "utf8" );

    return rawBody;
  } catch ( e ) {
    console.error( e );
  }
}

Well, now we can use these functions from the bridge code like that:

const oAuth2Client = await getOAuth2Client();
const rawBody = await getUnreadEmail( oAuth2Client, "to:joe+test1 is:unread" );
// Parse rawBody for the activation link
quick start example
the steps
index.js
a filter query