Friday, August 30, 2019

Getting HTML from Gmail body using GMail API and protractor

Part of the automated tests we are building involves checking email, verify its contents and click on a link to continue the registration process.  To do that, i setup a new gmail account, and follow Google's Quickstart instruction to enable GMail API.  Well, all you really have to do is to click the "ENABLE THE GMAIL API" button on the page. But before you do that, make sure you have selected the correct google account on the upper right corner.

Now that we have enabled the API and downloaded the credentials json file.  We can follow the example on the quickstart instruction to authenticate to gmail API.  However, the getNewToken will simply display a URL on the console and you are supposed to manually go to the URL on a browser and copy the code back to the program.  But we're writing an automated test with protractor, let's automate that too!

It's mostly the same as the example, except that when token.json is not found, it'll open a new browser window and grab the code automatically.  Also, note that the code uses an "AppPo" class, it's just a simple utility class I use to check if the button exists before clicking it.

The gmail API's list method returns a list of emails, only with the message id and the thread id, we'll have to call the get method to retrieve the content. The structure it returned is too complicated to my taste, and after all, I just want to grab the HTML content, so I decided to just get the raw content and reconstruct the HTML. Now you have it, we can now call searchEmail and pass it's returns to getHtmlFromEmailBody to get the HTML content for all emails returned.  With cheerio, we can easily find all links like this.

const $ = gmailUtils.getHtmlFromEmailBody(...);
$('a').each( (i,a) => console.log( $(a).attr('href') ) );

6 comments:

  1. Hello! Great article and thank You for Providing Such a Unique and valuable information on The SERPHouse for your readers. I really appreciate it. You can also visit Google Image Api for more SERPHouse related information and knowledge.

    ReplyDelete
  2. I m having some problems with google-auth-library there seems to be some incompatibility.

    Argument of type 'import("c:/Repository/idrra-verification/node_modules/googleapis-common/node_modules/google-auth-library/build/src/auth/oauth2client").OAuth2Client' is not assignable to parameter of type 'import("c:/Repository/idrra-verification/node_modules/google-auth-library/build/src/auth/oauth2client").OAuth2Client'.
    Type 'OAuth2Client' is missing the following properties from type 'OAuth2Client': forceRefreshOnFailure, getIapPublicKeys, getIapPublicKeysAsync, processAndValidateRefreshHandler, addSharedMetadataHeaders

    ReplyDelete
  3. Thanks for publishing such excellent knowledge. You are doing such a good job. This info is very helpful for everyone. Keep it up. Thanks. Read more info about Gmail google voice

    ReplyDelete
  4. Very well written article. It was an awesome article to read. Complete rich content and fully informative. I totally Loved it. Read more info about Gmail voice call Vietnam

    ReplyDelete
  5. What is the Best Payout Game at Slots Empire 2021?
    Best Slots Empire 2021 – Free Slots Online — 카지노사이트 When it 바카라사이트 comes to choosing a free casino slots game, the biggest thing going to come out is who

    ReplyDelete
  6. Great article. Thank you for sharing. Very useful information. And also please visit our website :
    Buy Bulk Gmail Accounts

    ReplyDelete