<-- All posts


Testing emails with Cypress

Testing emails with Cypress

Hey, welcome to the guide about testing incoming emails with Cypress. In this guide we will learn how to:


  1. Install Cypress
  2. Install ProxiedMail plugin
  3. Launch opening website + receiving sign up email test

About ProxiedMail

But let's learn a little bit about ProxiedMail as too before we begin. ProxiedMail is a tool that brings the email experience to a new level because it was built around the privacy first concept that enhances using a unique email each time which makes it a second password, but also allows you more control over your correspondence.

Additionally, it gives you the advantage of moving to another email provider just in a few seconds. Because we have this kind of system we also aim to bring more into the experience of development using emails.


Install Cypress

Cypress is a testing framework that is used to test web applications. It is a very powerful tool that allows you to write tests in JavaScript and run them in a browser. It is very easy to install and use. You can install it using npm. To install Cypress, run the following command in your terminal:

npm install cypress --save-dev

To launch Cypress, run the following command in your terminal:

npx cypress open

In opened UI you will be able to write your first test. To learn more about writing your first test please visit: Your First Test with Cypress.


Install ProxiedMail plugin

So, it's time to learn how to install ProxiedMail plugin for email testing.

ProxiedMail plugin destributes as cypress-test-email package. It provides the following features:


  • Creating a new email address
  • Receiving letter contents that were sent to generated emails
  • Receiving subject, body, html, headers of email

To install the plugin run the following command from your project location:

npm install --save-dev cypress-test-email

You can also visit Github repository of the plugin the see more details.


Cypress plugin Configuration

It's necessary to specify your API token in PROXIEDMAIL_API_KEY.

You can configure this plugin by putting to env variable PROXIEDMAIL_API_KEY to cypress.config.js.

const { defineConfig } = require("cypress");

module.exports = defineConfig({
    "env": {
        "PROXIEDMAIL_API_KEY": "YOUR API KEY"
    },
    e2e: {
        setupNodeEvents(on, config) {
            // implement node event listeners here
        },
    },
});

You can also pass the variable in a command line:

PROXIEDMAIL_API_KEY=your-api-key cypress run

Obtaining API key

You can obtain API key by signing up on ProxiedMail. Basic usage of the product is free up to 10 mailboxes + some API limits. If you want to get the best result please think about upgrading your account.

See pricing on ProxiedMail

Timeouts

ProxiedMail requires timeouts in order to get your emails. Please adjust the timings where you have the email testings up to 10s (1000ms).

Launch test that opening your website

After we have prepared everything let's write the test that allows us to test that after sign up we're going to receive the confirmation code email. It plays the critical role for our business, so we want to make sure that everything works correctly.

In this example we're going to test the sign up on ProxiedMail website and email receiving afterwards. It's crucial to test the email validity.



So, for email creating we have used

      cy.then(
          () => {
            return new Promise(resolve => {
              proxiedmail.createProxyEmail(proxyEmail => {
                resolve(proxyEmail)
              })
            })
      )


For email waiting and received we have used:

      cy.then(() => {
        return new Promise(resolve => {
              const interval = setInterval(() => {
                proxiedmail.getReceivedEmails(global.proxyEmailId, (resp) => {
                  if (resp.data.length > 0) {
                    resp.data[0].getDetails(function (details) {
                      clearInterval(interval);
                      resolve(details)
                    })
                  }
              })}, 3000);
              return interval;
            })
          }
)


In the end we have tested the email subject and saw that content contains needed phrased.

Conclusion

You have learned how to test emails with Cypress using ProxiedMail plugin. Using ProxiedMail you can create endless email addresses and test your application with them. It provides webhook and browsing received emails implementation.


Also for your privacy I do suggest to consider using ProxiedMail for your personal and business needs.

In case of any questions let us know on pmjshelp@pxdmail.com.


Quick links

Let us help you. Explain your problem and we will contact you to solve it.
Name
Email Address
Message
Thank you! We'll contact you soon
Our managers will contact you via email to solve the problem.