Skip to content

How to use Gmail API for email testing in Cypress?

Published: at 08:40 PM

In software testing, especially in automation, email verification is crucial. While many third-party inbox providers make it easy to fetch email content in Cypress with a simple API call, they often come with hidden costs ⚠️ and added complexities 🤯. For a long time, Mailosaur was my go-to solution. However, handling sensitive email data through third parties introduces security risks, and their pricing can be unpredictable. What costs $75 a year today could unexpectedly jump to $500. Plus, managing extra vendors adds even more hassle.

So, why not use Gmail instead? You might think, “It’s not possible with Cypress due to cross-domain restrictions, and automating Gmail is a no-go.” But imagine being able to directly call Gmail’s API and fetch the latest email in Cypress — sounds impossible, right? Actually, it’s totally doable!
gmail-api

In this blog, I’ll walk you through how to use the Gmail API to automate tests that require email access. Let’s dive in!

What You Need 🔧

Before you start, gather the following:

How to Obtain Your Credentials 🌟

Setting Up Your Cypress Project ⚙️

  1. Clone the Repository: Begin by cloning the repository that contains our Cypress setup for email testing:
> git clone https://github.com/rojalbati/cypress-email-testing
> cd cypress-email-testing
> npm install
  1. Configure Your Credentials:
  1. Send test email

Manually send a test email to your newly created Gmail account. This will be the email that you’ll extract details from.
test-email
4. Run the Test:

npx cypress open

Final Thoughts 💭

By using the Gmail API with Cypress, you simplify your email testing process, avoid extra vendor management, and save on costs. The setup involves a few steps to get your credentials, but once done, you can seamlessly integrate email verification into your Cypress tests. Happy testing! 🚀

👉🏻 By:Rojal Bati