Videos
Check out our tutorial video series.
Test email sign-up, password verification and more with Cypress testing and MailSlurp. Capture OTP magic links and test auth flows end-to-end.
Cypress JS is a powerful open source web testing framework that allows you to write an automated test suite using real browsers. With MailSlurp you can test using real email accounts created on demand - for free.
In this example we will see how to test login and sign up using one-time disposable email addresses and a real web app.
You can use email with Cypress in two ways: using the mailslurp-client package that gives full access to the API OR using the official MailSlurp Cypress Plugin with Cypress convenience methods. This guide covers usage of Cypress with the npm package. Use the
plugin instead if you want the
extensions available in your project.
Cypress is an amazing end-to-end testing framework. It's popular, bundles its own chrome browser and is easily extended.
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. To get started create a new test folder and run to create a new NodeJS project. This will create a node modules folder and
for further steps.
When installing Cypress simple run in the command line and then run
to open the Cypress app. A Cypress welcome window will show that allows you to configure the browser you wish to use and scaffold a basic test file.
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!
To demonstrate the Cypress test runner we will 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'll write a test case that will:
To setup make sure you have NodeJS installed then run:
Your should look something like this:
Next we need to create a file. We will set
and
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.
In order to use MailSlurp with Cypress we will define some custom commands.
To avoid creating custom commands check out the MailSlurp Cypress Plugin instead.
To do that create a directory and add
and
if not already present.
Inside 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 MailSlurp API Key first.
The MailSlurp config we created requires an API KEY to be set with the environment variable.
On Linux/Mac run cypress commands like so:
For Windows environments run:
Now in Cypress tests we can do the following:
Or
You can also add your own custom commands for any functions you want to call on the MailSlurp email API.
So in order to test a sign-up flow end-to-end we need an integration test.
Create a test spec in the folder structure:
The test should be located as follows:
Inside let's write our first 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.
To run the test 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 test executed output looks like this:
Now let's integrate email address into our test using MailSlurp. 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.
commandNow 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 method add the following:
Now we can run our tests again
Cypress loads our login screen and enters a real address that was generated with MailSlurp. We then see a confirm screen:
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:
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:
Testing with real emails means truly testing your app's most important processes like user sign up and email verification. MailSlurp makes Cypress email testing easy!
For more information on receiving emails in tests see the developers sections.
As always you can find this and other examples in the MailSlurp examples repository. Use MailSlurp free and take your end-to-end test coverage to 💯 !
Check out our tutorial video series.
Email and SMS guides for automation and testing.
View github project code for multiple languages.
Latest posts from the MailSlurp team.
Test, build, and automate messaging with a free MailSlurp account.