How to Read Emails in Cypress
Published: 2023-02-22 09:07:33



![](https://cdn-images-1.medium.com/max/1024/1*NftUAcNZWiY-J2zHFuX2mw.png)

### E2E Automated Sign up Test

Some people overlook the importance of testing the sign up flow. They might think that’s the first thing that they have developed, tested it over and over while developing other features, and it probably should be working.

However, it can happen that the sign up suddenly starts failing for various reasons. If it does fail, you would want to know it as soon as possible. The later you realize the failure, the more potential new users it can drive off. That is the reason we have an automated test for the sign up/registration flow which gives us a report if something is off, on a daily basis.

![](https://cdn-images-1.medium.com/max/683/0*VdGZWTuEPqZMpmKQ.jpeg)
_Photo by Markus Spiske on Unsplash_

### Test Flow

For this example, we’ll be going through Mediatoolkit’s sign up and registration page. Here’s how the test flow goes:

1. User enters their email on the Mediatoolkit sign up page ([https://www.mediatoolkit.com/signup](https://www.mediatoolkit.com/signup))
2. User opens a link received via email
3. User is redirected to the registration form
4. User fills out the registration form
5. User finishes registration and gets logged in the app
6. Delete the user (clean up after the test)

Apart from the standard registration flow, I have added the “delete user” step as well. So, after we confirm that the user is registered, we delete that user using API. That way, we get to the initial state which was before the test started. This ensures that the test does not make a lot of junk in the database and is able to run over and over as many times as we want.

### Test Goal

Our test asserts that:

- A user can enter an email address on the sign up page
- A user gets an email from Mediatoolkit
- The email contains a valid confirmation link
- A user can fill out the form and finish the registration on the link from the email
- A user gets logged in after finishing the registration
- A user gets deleted after all the previous requirements are successfully validated.

Since our test aims to validate that the email is being both sent and received, i.e. the same flow as the user would go through, I wanted to make it as similar as possible to the real user flow, so mocking this part was not an option for me.

### How do I read my emails in Cypress?

As you could see in the previous sections, in order to finish the registration, you need to visit the link that you received in the welcome email. But to do so, you need to access your email inbox, preferably via API. Luckily, I found a [blog](https://levz0r.medium.com/how-to-poll-a-gmail-inbox-in-cypress-io-a4286cfdb888) about the Gmail-tester library by Lev Gelfenbuim which made this test implementation very smooth.

Basically, what you need to do is:

- Install the gmail-tester (_npm install — save-dev gmail-tester)_
- Save the Google Cloud Platform OAuth2 Authentication file named credentials.json inside an accessible directory
- In terminal, run the following command: _node <node\_modules>/gmail-tester/init.js <path-to-credentials.json> <path-to-token.json> <target-email>_

The detailed instructions on how to install and configure the gmail-tester, and your Google account, you can find on the gmail-tester github [readme](https://github.com/levz0r/gmail-tester/blob/master/README.md#how-to-get-credentialsjson).

### Usage in the test

In my cypress-config.js file I have:

{% gist https://gist.github.com/ajdinmust/ffc77534d78570bc08535fc472b5b96e %}

And inside of the “module.exports = defineConfig”,

{% gist https://gist.github.com/ajdinmust/288a97eee7f910f3a6535e601e694558 %}

As you may have seen in the previously mentioned blog, this piece of code would be in the “cypress/plugins/index.js”, but since I am using Cypress 10 now, it goes to the cypress-config.js file. Also, the code did not work for me until I added “subject:”, “from:”, “to:” in front of “subject”, “from”, and “to”, which is also different in the blog.

Inside of the test file itself, you should have something like this:

{% gist https://gist.github.com/ajdinmust/63d0a478aa6191999b58b4c5b6f67624 %}

This task checks if there is an email existing in our inbox with the given parameters. Then you can console log the found email. The “html”, in my case, contains the html of the email that we have found in our Gmail inbox. Now, from that html you can take out the confirmation URL or any other information that you need.

![](https://cdn-images-1.medium.com/max/355/0*-lUfryoa9VKiM9zS)
_Email found assertion_

### Final Thoughts

Relying on external sources in your test is usually not a good idea. However, if you would like to confirm exactly the same flow as your user would go through, you can try using Gmail-tester which makes it very easy to access an email in your Gmail inbox.

Make sure you visit Gmail-tester [Github](https://github.com/levz0r/gmail-tester) repository for more information and the latest updates.

Feel free to [contact me](https://www.linkedin.com/in/ajdin-must/) if you have any questions regarding the implementation.