Using the gmail-tester + Puppeteer to poll Gmail inbox

Serhiy Tymoshenko
3 min readApr 27, 2020

--

The E2E testing of your application in a comprehensive way usually means you need to check all the flow within the business logic, and leaving blind spots is not an option if you you want to sleep well. It is difficult to imagine web applications that do not involve mail functionality, so ensuring that users can send and receive their letters is a must. In my case, I have an ERP application to test, where mailing is not just a matter of registration and confirmation, but a huge part of user communication logic.

There are several tools and ways to test mailing functionality. The simplest and most independent way is having a local mail server, so you can configure your application to shoot mail bullets within your local environment, ex. using mailgun to send mail + mailhog as a mail server, and then letting your tests access letters via the server API. But what if you need to test real mails to test live environment or you have an application where you need to have access to your mail via SMTP or something like Gmail API with all whistles and tokens? Well, then it’s better to use more dedicated mail-testing tools like mailslurp or mailosaur, which are great and user-friendly APIs, and should be paid for subscription. Do we have any alternatives? — Fortunately, yes! Thanks to Lev Gelfenbuim and his article on Medium, I have found his gmail-tester Node.js client that handles Gmail polling as easily as a pie. He described his experience of coupling it with Cypress, and I’d like to share my experience bundling it with Puppeteer. Puppeteer is a Node.js library for automating web crawling as well as web testing, when used with some testing framework like Jest or Mocha.

Adding gmail-tester to you testing project

You can refer to gmail-tester readme file to activate Gmail client properly.

I’ll briefly list steps:

  • install gmail-tester in your project:
npm install --save-dev gmail-tester
  • once done run this command in the terminal:
node <node_modules>/gmail-tester/init.js <path-to-credentials.json> <target-email>

where <target-email> is gmail-tester@my-company.com in my case. 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. Copy the token at the end of the process and set it into the console prompt.

If everything went well you should see next output in the console:

[gmail] Found!

and gmail_token.json file should be created within credentials.json.

At this point, you’re good to go testing your app mailing functionality.

Using gmail-tester

  • First, I’ll create a helper with test methods to use in my tests. From readme, we know that we have two methods — check_inbox() — to poll for particular mail in the inbox within the given interval and get_messages() — to look for all mails filtered by dates.
  • Then, I can test some mailing scenario without knowing the to and from data, so I’ll use get_messages(), for which they aren’t required, ex.:
  1. Create an order
  2. Send an order confirmation
  • Here’s another scenario to use check_inbox(), ex.:
  1. Create a dialog within the chosen order in the application
  2. Send a dialog mail copy to the user

Thanks for reading this! I’ll be happy if this information helps someone to achieve their testing goals. Feel free to ask questions if any occur.

Also, many thanks to Lev Gelfenbuim for his great tool! 👏

--

--