Cypress JS Email Plugin —how to test with real email addresses! 🚀

How to send and receive emails in Cypress integration tests using Javascript and the free MailSlurp library.

What is Cypress Js?

Cypress is an amazing end-to-end testing framework. It’s popular, bundles its own chrome browser and is easily extended. Here is a screenshot:

Cypress lets you test many parts of a website or web application with a browser in an automated way. It’s like Selenium but a lot more user-friendly.

Testing with real email addresses

Many application use emails for account login and creation. Testing logins with Cypress is easy if you have a designated test user — but what about sign ups?

Email sign up has to be the most important aspect of any application and without access to unique, private email addresses end-to-end testing is difficult.

Luckily there’s MailSlurp, a free API that lets you create real, randomized email addresses on demand. It also lets you send and receive email programmatically — perfect for end-to-end testing user sign-up flows!

An example

To demonstrate Cypress testing we’ll use the MailSlurp Authentication Playground app. It’s a dummy React application that has a user sign-up process. To test this process end-to-end we will:

  • Load the playground in Cypress
  • Create a test email address with MailSlurp
  • Sign up for using the email address
  • Receiving a confirmation email
  • Extract the confirmation code and confirm the user

Setup Cypress

To setup make sure you have NodeJS installed then run:

npm install --save cypress mailslurp

Your package.json should look something like this:

Next we need to create a cypress.json file. We will set defaultCommandTimeout and requestTimeout to 30 seconds so that our tests have time to wait for emails to be sent or received. We'll also add some viewport dimensions to the Cypress browser.

Note for this example we also include our MailSlurp API Key in an env section of the cypress.json file. We can read that later in tests using Cypress.env("API_KEY").

Setup MailSlurp

In order to use MailSlurp with Cypress we will define some custom commands.

To do that create a cypress/support directory and add index.js and commands.js.

Inside index.js import the commands file.

Now inside commands let’s import and configure a MailSlurp client — this will let us send and receive emails. Make sure you have a free API Key.

Next we will add commands to create email addresses and receive emails:

Now in Cypress tests we can do the following:

Or:

Create test structure

So in order to test a sign-up flow end-to-end we need an integration test. Create a test spec in the cypress/integration folder structure:

The test should be located as follows:

Inside integration/example.spec.js let's write our first test.

Writing a test

Let’s start simple and write a test to load the MailSlurp playground app in Cypress.

That would look a little like this:

Next we want to click a sign up button to begin the sign up process.

The end result for our test looks like this:

Running tests

To run the test run npx cypress run. Cypress will open:

Click the spec runs our test. Our clicked Cypress loads the Playground app and asserts that a sign-in message is displayed on the page, it then clicks the sign-up link.

The output looks like this:

Testing with emails in Cypress

Now let’s integrate email address into our test using . MailSlurp requires an api key but it’s free for personal use so sign up to get one.

Once you’ve sign up we can extend Cypress with a command to creates new email address when we need them.

Generate test email accounts in Cypress

To extend Cypress first create a support folder in the cypress directory and place an index.js and a command.js inside it. Like so:

Inside index.js import the commands.

Now let’s set up a command called createInbox inside commands.js that calls MailSlurp and creates an email address on demand.

Using our createInbox command

Now that we have a Cypress command that returns a new email address we can put it all together in our sign up test. Inside the same describe method add the following:

Notice that we destructure the returned value from our helper command.

Now we can run our tests again

npx cypress run

Cypress loads our login screen and enters a real address that was generated with MailSlurp. We then see a confirm screen:

Receive confirmation email

Once we have submitted our new user sign-up the Playground will send a confirmation email with a code that we need to enter. We can receive this email using MailSlurp:

Running the tests shows that Cypress waits for and extracts the email content:

Confirm the user

Finally we can use the extracted email code to confirm the user:

Now we can use Cypress to login with our confirmed email address and password:

If successful the Playground will show a picture of a happy dog. Let’s test for that:

Next Steps

Testing with real emails means truly testing your app’s most important processes like user sign up and email verification.

For more information on receiving emails in tests see the developers sections.

Code Samples

As always you can find this and other examples at https://github.com/mailslurp/examples. Use MailSlurp free and take your end-to-end test coverage to 💯 !

Originally published at https://www.mailslurp.com.

Test Email API for end-to-end with real email addresses. Support for NodeJS, PHP, Python, Ruby, Java, C# and more. See https://www.mailslurp.com for details.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store